1

I have a java client program that sends a command to server and server sends back an acknowledgement and a response string.

My client program gets the input stream length from client socket as 0. But, I used wireshark to investigate. Wireshark logs show that the server has sent back a response to my ip. Somehow, my client is unable to read it.

My Client

public class Client {
    private static final String SERVER_ADDRESS = "192.168.64.79";
    private static final int TCP_SERVER_PORT = 6669;

    public void connect(String command) {
        BufferedReader in;
        try {
            // Socket skt = new Socket("50.128.128.254", 6669);//ip,port
            Socket clientSocket = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);// ip,port


            System.out.println(" client Socket created ..Enter command : ");


            PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);


            String ToServer = command;
            outToServer.println(ToServer);

             in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            System.out.print("Received string : length "+clientSocket.getInputStream().available()+"\n response:");

//          while (!in.ready()) {
//          }
            System.out.println(in.readLine()); // Read one line and output it

//          System.out.print("'\n");
            in.close();
            clientSocket.close();
            System.out.print("connection closed");

        } catch (Exception e) {
            System.out.print("Whoops! It didn't work!\n");
            e.printStackTrace();
        }

    }

}

WireShark results:

9bytes of data sent from server to my client

enter image description here

Client output

Java client receives inputstream length 0

enter image description here

zubergu
  • 3,646
  • 3
  • 25
  • 38
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • Anything to do with: http://stackoverflow.com/questions/3695372/what-does-inputstream-available-do-in-java ? Also, you try to readLine but your server seems not to end its data with new line character. – zubergu Jan 14 '16 at 10:13
  • @zubergu When I change it to in.read() the it returns -1 – Rachita Nanda Jan 14 '16 at 10:50
  • maybe try dropping getting the input stream the second time for .available() call... just guessing. Also, if you own server, post its code as well. – zubergu Jan 14 '16 at 11:19

0 Answers0