Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

have troulbe when learn java tutorial's network socket part

Thread view: 
jieshuang@gmail.com - 19 Apr 2006 06:03 GMT
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.
hiwa - 19 Apr 2006 06:34 GMT
<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.
jieshuang@gmail.com - 19 Apr 2006 07:23 GMT
" 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.
Thomas Weidenfeller - 19 Apr 2006 08:45 GMT
> " 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
jieshuang@gmail.com - 19 Apr 2006 09:55 GMT
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.
Thomas Weidenfeller - 19 Apr 2006 10:53 GMT
> have to work and have
> to learn to cheat for job

Don't do that. There are more than enough bad behaving network
applications out there. Some of them create real problems (e.g. recently
 http://people.freebsd.org/~phk/dlink/).

Or see http://www.norvig.com/21-days.html

/Thomas
Roedy Green - 20 Apr 2006 01:45 GMT
On 19 Apr 2006 01:55:43 -0700, "jieshuang@gmail.com"
<jieshuang@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

mlody277 - 19 Apr 2006 10:05 GMT
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
jieshuang@gmail.com - 20 Apr 2006 04:33 GMT
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~~~


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.