[java] TCP Sockets and Data Streams

Started by
5 comments, last by MattS423 18 years, 9 months ago
Hey. I created a server-client pair that connect to each other through a TCP socket and they're supposed to be able to talk to each other....only they can't. The server program just stops and does nothing whenever the server attempts to read from the socket data stream. Here's the relevent code:


 PrintWriter out = new PrintWriter(ClientSocket.getOutputStream(), true);
            String InData = "InDataInitString";
            out.println("Handshake"); // <---- This goes through to client just fine
            
           
            BufferedReader in = new BufferedReader(
                        new InputStreamReader(ClientSocket.getInputStream()));
                    
            try
            {
            InData = in.readLine(); // Error occurs here
            System.out.println("Check");
            System.out.println(InData);
            }
            catch(IllegalBlockingModeException e)
            {
                out.println("end");
                System.err.println(e.getMessage());
                System.exit(1);
            }

The catch statements are never reached. the
 in.readLine() 
call just seems to take forever. its obvious that the sockets are connected correctly, because i can send data from server to client just fine. The client is deffinately sending data to the server...the server is just not reading it. Any ideas? I'm kinda new at Java AND new at network programming, so any help would be great. Thanks! Matt
Programmers of the world, UNTIE!
Advertisement
is a newline character ever being sent by the client? readline() blocks until it reads a newline.
The client and server send data the same way, though a PrintWriter. I don;t know if this sends the newline character or not, but to make sure, I sent one explicitly and the results were the same. Intrestingly enough, the client CAN get the text from the server but the server can't get the data from the client, even though they are sent in the same way.

Here's the client code:

 public Client() {                Socket kkSocket = null;        BufferedReader in = null;        PrintWriter out = null;        try{            kkSocket = new Socket("DB62T411",4444);            in = new BufferedReader( new InputStreamReader( kkSocket.getInputStream()) );            out = new PrintWriter(kkSocket.getOutputStream());        }        catch(UnknownHostException e)        {            System.err.println(e.getMessage());            System.exit(-1);        }        catch(IOException e)        {             System.err.println(e.getMessage());             System.exit(-2);                    }                        try{                          String InLine = " ";                          System.out.println("Begin Read\n");             out.println("Begin Read\r\n");  //this line is not received by server             while((InLine = in.readLine())  != null)       //works.            {                System.out.println(InLine);                out.println(InLine);                if(InLine == "end")               {                    break;               }                                               }//end while.                         System.out.println("Disconnect");                          out.println("end");             out.close();                              in.close();           kkSocket.close();       }       catch(IOException e)       {           System.err.println(e.getMessage());            System.exit(-3);       }                           }


any other ideas?
Programmers of the world, UNTIE!
How's the server code? Is it "looping" properly, I mean, aren't you running on deadlocks? You're using a multithreaded approach, right?
a.k.a javabeats at yahoo.ca
here's the server code, with some comments:

  PrintWriter out = new PrintWriter(ClientSocket.getOutputStream(), true);            String InData = "InDataInitString";            out.println("Handshake\n");  //client receives this line.                                   BufferedReader in = new BufferedReader(                        new InputStreamReader(                            ClientSocket.getInputStream()));                                              InData = in.readLine(); //Hangs here.  It MUST be blocking somehow.                                    //no error, but it just sits.            System.out.println("Check"); //line never reached.            System.out.println(InData);                                                                      System.out.println("Starting Loop");           out.println("Starting Loop");//the Loop            while( (InData = in.readLine()) != null)  //same error would occur here if it ever got there.            {                              System.out.println("Looped and data present"); //to ensure proper looping.  Never reached.               //process InData               //Write the line               System.out.println("Received: " + InData );               out.println("Received: " + InData);                              if(InData.equalsIgnoreCase("end"))               {                   break;               }                                                            }                      System.out.println("Stream Closing");           out.close();           in.close();            



As you can see, the in.readLine() call makes it sit and spin.
Programmers of the world, UNTIE!
what does the client code look like?
Oh, i thought i had the entire client up there

 public Client() {                Socket kkSocket = null;        BufferedReader in = null;        PrintWriter out = null;        try{            kkSocket = new Socket("DB62T411",4444);            in = new BufferedReader( new InputStreamReader( kkSocket.getInputStream()) );            out = new PrintWriter(kkSocket.getOutputStream());        }        catch(UnknownHostException e)        {            System.err.println(e.getMessage());            System.exit(-1);        }        catch(IOException e)        {             System.err.println(e.getMessage());             System.exit(-2);                    }                        try{                          String InLine = " ";                          System.out.println("Begin Read\n");             out.println("Begin Read\n");             while((InLine = in.readLine())  != null)                   {                System.out.println(InLine);                out.println(InLine + "\n");               if(InLine == "end")               {                    break;               }                                               }                         System.out.println("Disconnect");                          out.println("end\n");             out.close();                                     in.close();           kkSocket.close();       }       catch(IOException e)       {           System.err.println(e.getMessage());            System.exit(-3);       }                           }


Any ideas? I'm getting despeate here...
Programmers of the world, UNTIE!

This topic is closed to new replies.

Advertisement