Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Can't receive reply on a socket.

Hi,

I want to implement a two-way socket communication.  My Clinet send a message to Server.  The Server receives the message and sends the reply back.  But the Client never gets it.  Sometimes I get BackingStoreException.  Which I can't Catch  (the Client code does not compile).

Please, help.


Here is the Client piece:

        try {
            socket = new Socket(server, port);
            System.out.println("Connected with server " + socket.getInetAddress() + ":" + socket.getPort());
        }
        catch (UnknownHostException e) {
            System.out.println(e);
            System.exit(SOCKET_ERROR);
        }
        catch (IOException e) {
            System.out.println(e);
            System.exit(SOCKET_ERROR);
        }
       
        try {
            output = new PrintWriter(socket.getOutputStream(), true);
            output.println("system");
           
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("after BufferedReader");   // I SEE THIS PRINTOUT
            dataStr = input.readLine();
            System.out.println("dataStr=" + dataStr);   // I DO NOT SEE THIS PRINTOUT
            }
        }
        catch (IOException e) {    System.out.println("Exception: " + e);     }
        //catch (BackingStoreException e1) {    System.out.println("Exception: " + e1);     }
       
        try {   socket.close();   }
        catch (IOException e) {    System.out.println(e);       }


This is the Server:

        int port;
        ServerSocket server_socket;
        BufferedReader input, cmdOut;
        PrintWriter output;
        Process process = null;
        String cmdStr = null;
       
        try {
            port = Integer.parseInt(args[0]);
        }
        catch (Exception e) {
            System.out.println("port = 7777 (default)");
            port = 7777;
        }
       
        try {            
            server_socket = new ServerSocket(port);
            System.out.println("Server waiting for client on port " +
            server_socket.getLocalPort());
           
            // server infinite loop
            while(true) {
                Socket socket = server_socket.accept();
                System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
                input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // print received data
                try {
                    while(true) {
                        String message = input.readLine();
                        if (message==null) break;
                        System.out.println("Server: " + message);
                        String uname = doUname();  
                       
                        output = new PrintWriter(socket.getOutputStream(), true);
                    }
                }
                catch (IOException e) {   System.out.println(e);   }
               
                // connection closed by client
                try {
                    socket.close();
                    System.out.println("Connection closed by client");
                }
                catch (IOException e) {  System.out.println(e);   }
            }            
        }
       
        catch (IOException e) {   System.out.println(e);   }
    }
0
akoifman
Asked:
akoifman
  • 4
  • 3
  • 3
1 Solution
 
objectsCommented:
> The Server receives the message and sends the reply back.  

I don't see anywhere in your server code that sends a reply back.
You also create a new PrintWriter everytime which is incorrect.

try {
  output = new PrintWriter(socket.getOutputStream(), true);

   while(true) {
     String message = input.readLine();
     if (message==null) break;
     System.out.println("Server: " + message);
     String uname = doUname();                        
     // you maybe should be sending a reply here
     out.println("reply");
                   }

0
 
umangjoshiCommented:
object is right also wait for some amount of time till the server send something, for that add a line in ur client code as follows..

Thread.sleep(some no of milis);
output = new PrintWriter(socket.getOutputStream(), true);
output.println("system");
0
 
akoifmanAuthor Commented:
Hi, thanks for your reply.  I have to check "object's" suggestion at work.  I have no testbed at home.

umangjoshi: Why do I have to Sleep before sending a message?  Should I sleep in the client before receiving a reply?  Something like:

Thread.sleep(some no of milis);
dataStr = input.readLine();  // problem here !!!

Thanks,

Alex.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
umangjoshiCommented:
Sorry, it should be like this

Thread.sleep(some milis);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
0
 
akoifmanAuthor Commented:
Umangjoshi, thanks for your reply.  Why do I need to sleep?  Isn't it a blocking receive?  You mention that Object recommended this, I do not see it in his post.

Thanks,  Alex.
0
 
umangjoshiCommented:
[1] client send some message (socket) to the server...
[2] server receive it at the other end
[3] then server write something in to the socket
[4] that ur client receive

this process take time... to get someting (reply from the server) at client side... so i suggested the sleep
0
 
akoifmanAuthor Commented:
Umangjoshi, thanks for your reply.  Why do I need to sleep?  Isn't it a blocking receive?  You mention that Object recommended this, I do not see it in his post.

Thanks,  Alex.
0
 
objectsCommented:
I don't see any need for a sleep.
0
 
akoifmanAuthor Commented:
Thanks, adding the send did it!
0
 
objectsCommented:
excellent :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now