have troulbe when learn java tutorial's network socket part

J

jieshuang

Hi everyone:
Try fewest word: from Java Turotial->Custom Networking->All About
Sockets->Reading from and Writing to a socket, the following program:
import java.io.*;
import java.net.*;

public class EchoClient {
public static void main(String[] args) throws IOException {

Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}

out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}

But it says "Don't know about host: taranis". If I change it to my
computer's name, it says "Couldn't get I/O for the connection to:
taranis.";
So how can I open echo service on a computer? Thanks.
 
H

hiwa

<quote>
the host name taranis. This is the name of a hypothetical machine on
our local network.
</quote>
Try "localhost" instead.
Also, the echo server should be running on your host.
 
J

jieshuang

" Also, the echo server should be running on your host."

I'm sorry but how can I set up the echo server on my host? I'm using
Windows XP.
 
T

Thomas Weidenfeller

" Also, the echo server should be running on your host."

I'm sorry but how can I set up the echo server on my host? I'm using
Windows XP.

You maybe better as this on a Windows group. AFAIK Windows Server
edition has an echo server as part of the "Simple TCP/IP Services",
which one can configure somewhere. I have no idea if there is something
similar in XP.

If you can't find anything, you can maybe try the very simple server
given at the end of
http://www.javaworld.com/jw-12-1996/jw-12-sockets-p2.html

It listens on a different port, but it should be easy to change this in
your client.

A general remark: Sun's networking tutorial is rather bad. it is maybe
ok for some first steps, but it is fare from complete, and has some ugly
errors. So, you probably want to read some more about TCP/IP in general.

/Thomas
 
J

jieshuang

Thank you. Echo server is not so important to me. I just want to learn
basic network programming in serveral weeks. Your link is useful,
although it seems that the "echo server" in it does not have port 7,
and it still use InputDataStream which is reported as too old by javac.
I know to be expert in network, some books like Steven's is essential.
But you know, English is my second language and I have to work and have
to learn to cheat for job(network and database is hot, so I have to
learn basic of them and do several project). I have not much time.
 
M

mlody277

Hallo I am new at this group but in my opinion :
First of all You have to create a server application at the same port
like a client and start it than You can start the EchoClient class.
You receive the exceptions because there is no machine( server ) which
can serve requests from EchoClient
The other problem is that in the line:
echoSocket = new Socket("taranis", 7);
you use the post = 7. The ports between 0 and 1024 are reserved for the
system, so please use some biger number.
If you want to lunch this cod locally at Your computer use "localhost"
not "taranis"
I believe that this post help You.
Best regarst
mlody277
 
R

Roedy Green

English is my second language and I have to work and have
to learn to cheat for job

Some proverbs would be appropriate here.

This approach simply does not work in computers. A brilliant
psychologist Chris Rainey (who suddenly dropped dead of a brain tumor)
proved this to many years ago when I was absolutely frazzled trying to
meet deadlines.

He persuaded me to, just as an short term experiment, be lazy and
deliberately take too much time to complete each task.

Of course in this relaxed environment I made very few errors and
everything worked first time. My productivity soared. I did not have
to do anything over. I wrote easy-to-maintain code. I stopped wasting
time flapping my arms muttering "I HAVE to get this finished." Without
the adrenalin, I could think clearly and logically.
 
J

jieshuang

Thanks you. Yes, troubles are caused by the unexisting echo server,
whose port is 7 (but "use some biger number" if it will create a
server, not a client, I think). I now create server myself in program
for study purpose. Gone away echo server with port 7~~~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top