Solved

socket programming tutorial question

Posted on 2008-10-04
4
420 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now