Solved

socket programming tutorial question

Posted on 2008-10-04
4
422 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
  • 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 500 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap bin 2 34
fibonacci ten numbers 4 38
servlet example 17 30
eclipse buid path vs tomcat lib path 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

770 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