[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

socket programming tutorial question

Posted on 2008-10-04
4
Medium Priority
?
432 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 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

649 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