?
Solved

socket programming tutorial question

Posted on 2008-10-04
4
Medium Priority
?
428 Views
Last Modified: 2013-11-23
I am wanting to test this Java tutorial on socket porgramming:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html

This is the code that I am using:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketServer.java
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketClient.java

However when I try to run the SocketClient thing the applet comes up for 2 seconds then goes away and on the console I see the following error: Unknown host: kq6py.eng

I am not sure what this means. Is it the port number that is the issue? Is it that it cannot create the kq6py onject? Why is it throwing an unknown host exception? How can I fix this?

And just so I can test my understanding what is happening here is that we are meant to start the server and then the client and send text from the client and we should see that appearing on the server applet - is that right?
0
Comment
Question by:ArtemisF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 22643673

1. You should change the line in the SocketClient.java:

This:

socket = new Socket("kq6py", 4444);

Should be this:

socket = new Socket("localhost", 4444);

That is, if you are running both server and client on the same machine. If not, you should set the server ip here.

2. Now compile both classes and then start the SocketServer first and then SocketClient.

3. Now you enter text into the textbox and it should show on the server when you click the button.


BTW: There are no applets here, just the server and the client application.
0
 

Author Comment

by:ArtemisF
ID: 22643924
Ah indeed that does work!
Oops I meant those interface things - wysiwyg?

Can I also ask..I want it to be so that when the client starts up it sends a particular set of words, e.g. "hello" and when this particular set of words is sent to server the server should display the client's hostname/IP address. How can I get this Ip address of the client? Also where can I put the code about sending a hello message to server from client when client starts so that it only happens once during start up?

Thanks for any advice you can give me. Not familiar with all this much.
0
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 2000 total points
ID: 22644121

>> I want it to be so that when the client starts up it sends a particular set of words, e.g. "hello"

To send a particular set of words, simply change this line (in SocketClient):

out.println(text);

into

out.println("hello");


>> and when this particular set of words is sent to server the server should display the client's hostname/IP address.

You can display the clients address using methods on the Socket object: getAddress(), getHostName()

>> Also where can I put the code about sending a hello message to server from client when client starts so that it only happens once during start up?

You have to put the code in the main() method or in the constructor.



Check some good examples for socket communication:

http://www.kodejava.org/examples/216.html

Its difficult to explain all this at once, since you are new to everything. You should really learn the basics of Java first. Take a look at this book:

http://www.mindview.net/Books/TIJ/




0
 

Author Comment

by:ArtemisF
ID: 22644474
Thank you for your help.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question