10 Replies Latest reply: Mar 8, 2012 11:39 PM by 922439 RSS

    Simple Socket Program error

    922439
      I got no no output in either the server/client terminal for the following pair of server/client code. Can anyone please point where have I made a basic mistake in the flow of things...

      Server Code:-

      import java.io.*;
      import java.net.*;

      public class TCP_File_Server
      {
           public static void main(String st[])
           {
                try
                {
                     String msg="start";
                     ServerSocket listen_socket=new ServerSocket(7001);
                     
                     while(true)
                     {                    
                          Socket conn_socket=listen_socket.accept();
                          BufferedReader in_client=new BufferedReader(new InputStreamReader(conn_socket.getInputStream()));
                          DataOutputStream out_server=new DataOutputStream(conn_socket.getOutputStream());
                          msg=in_client.readLine();
                          System.out.println(msg);
                          out_server.writeBytes("Wassup ?!");
                          conn_socket.close();                    
                     }
                }
                catch(IOException e)
                {
                     System.out.println(e.toString());
                }
                
           }
      }


      Client Code:

      import java.io.*;
      import java.net.*;

      public class TCP_File_Client
      {
           public static void main(String st[])
           {
                try
                {
                     if(st.length==1)
                     {
                     String path=st[0];
                     Socket clientSocket=new Socket("localhost",7001);
                     DataOutputStream out_server=new DataOutputStream(clientSocket.getOutputStream());
                     BufferedReader in_server=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                     out_server.writeBytes(path);     
                     System.out.println(in_server.readLine());
                     clientSocket.close();
                     }
                }
                catch(IOException e)
                {
                     System.out.println(e.toString());
                }
           }
      }
        • 1. Re: Simple Socket Program error
          jtahlborn
          you generally want to call flush() after you write a message to a socket stream to ensure that it actually gets sent. note, you are mixing Readers and Streams which will likely get you into trouble soon. you want the same classes on either end (e.g. BufferedReader/BufferedWriter or DataOutputStream/DataOutputStream), otherwise you will most likely end up corrupting your data.

          Edited by: jtahlborn on Mar 7, 2012 3:27 PM
          • 2. Re: Simple Socket Program error
            EJP
            Apart from what jtahlborn correctly says, you are reading lines but you aren't writing them, so readLine() will block forever waiting for the missing newline.
            • 3. Re: Simple Socket Program error
              922439
              So you mean to say if I write data using DataOutputStream in server(suppose) then I should read it using DataInputStream in client and likewise for other types of streams ??
              • 4. Re: Simple Socket Program error
                gimbal2
                You may not need DataInputStream/DataOutputStream specifically, but it is a best practice to deal with either a Reader/Writer (characters) or an InputStream/OutputStream (bytes). The main idea is to treat data as the same unit on both ends of the line.
                • 5. Re: Simple Socket Program error
                  922439
                  Have my concepts of Streams thorough bro...! Socket transmission always takes place by byte stream... and even if i use bufferedreader, it has an underlying inputstreamreader which converts byte to char stream, so shouldn't be a problem... :)

                  the main problem as pointed above was i was using readLine() method which searches for newline char but hadn't written newline char on output stream...

                  Anyways Thanks to both of you. :)
                  • 6. Re: Simple Socket Program error
                    gimbal2
                    919436 wrote:
                    Have my concepts of Streams thorough bro...!
                    ... the beginning of the end is nigh - OTN has been brozoned.
                    • 7. Re: Simple Socket Program error
                      EJP
                      Have my concepts of Streams thorough bro...! Socket transmission always takes place by byte stream... and even if i use bufferedreader, it has an underlying inputstreamreader which converts byte to char stream, so shouldn't be a problem... :)
                      Guaranteed to be a problem unless you can guarantee that the bytes you wrote will turn into the characters you expect via the charset used by that implicit InputStreamReader.
                      • 8. Re: Simple Socket Program error
                        922439
                        Java API provides a wide variety of charset for that, so that is like almost guaranteed...
                        • 9. Re: Simple Socket Program error
                          EJP
                          Exactly. So many charsets, and only one is the right one.

                          The rule is simple. Use a Reader if (a) the sender is using a Writer or you know that the data is character and (b) you know the charset. Otherwise, which is most of the time, use an InputStream. And in general, use a DataOutputStream with a DataInputStream, an ObjectOutputStream with an ObjectInputStream, etc.
                          • 10. Re: Simple Socket Program error
                            922439
                            Okay point taken... Wasn't here to argue, just to find a solution and that I found...