?
Solved

Can't receive reply on a socket.

Posted on 2003-03-14
10
Medium Priority
?
189 Views
Last Modified: 2010-07-27
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
Comment
Question by:akoifman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 8141095
> 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
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8141678
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
 

Author Comment

by:akoifman
ID: 8142524
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142551
Sorry, it should be like this

Thread.sleep(some milis);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
0
 

Author Comment

by:akoifman
ID: 8142598
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
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142619
[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
 

Author Comment

by:akoifman
ID: 8142662
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
 
LVL 92

Expert Comment

by:objects
ID: 8144653
I don't see any need for a sleep.
0
 

Author Comment

by:akoifman
ID: 8147545
Thanks, adding the send did it!
0
 
LVL 92

Expert Comment

by:objects
ID: 8147826
excellent :)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question