Solved

socket programming tutorial question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Securing Jmx Console and web console 2 64
simple java question 3 44
Basic Java Case or If-Else statement... 3 43
windows explorer path to command prompt 5 32
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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:

920 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

13 Experts available now in Live!

Get 1:1 Help Now