0

I try to communicate with a java application to a µController in wifi (Flyport).

I have a problem with the java application : It first create a socket to communicate with the Flyport server, then send a message and receive the Flyport answer.

Everything work fine until the read part. I'm polling the read() function of the BufferedReader until it return -1, but it doesn't. The first read works fine, all the answer are red, but the application stay stuck when it tries to read again.

My code is very simple : Java application :

try (
    Socket clientSocket = new Socket(hostName, portNumber);
    PrintWriter out =
        new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in =
        new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
)

{
    ...//Connection and sending message works fine
}
char[] buffer = new char[500];
while ((in.read(buffer)) != -1) { // first read() works fine, second read() stay stuck...
    System.out.println(buffer);    // display all answer sent by flyport
}

The code in the flyport :

while(isClientConnected){
    //check if client is still connected
    ...

    //read client message
   while((RxLen=TCPRxLen(sock))>0)
    {
      TCPRead(sock,bff,RxLen);
      strcat(msg,bff);
    }
    //write back to the client that the order is received
    TCPWrite(sock, msg, strlen(msg));

   //process the client order
    ...

   //Write to the client that the process is done
   TCPWrite(sock, msg2, strlen(msg2));
}

The java application read msg and msg2 with the first read(). msg and msg2 have "\r\n" at the end.

Doesn't somebody can tell me where I am wrong ? Is there a function from BufferedReading that tells how much data there is left to read ?

Thanks and regards.

NB : I try with a small buffer in the java application, the problem is the same, read() is stuck when there is nothing left to read...

4
  • did you tried with while(in.ready()) e.g. while (in.ready()) System.out.println(in.readLine()); Commented Aug 12, 2014 at 9:53
  • @ThusithaThilinaDayaratne Why? available() and ready() don't have many critical uses, and this certainly isn't one of them.
    – user207421
    Commented Aug 12, 2014 at 10:01
  • @EJP Because then JVM handle the buffer for us rather than we manually handling the buffer. Isn't it more appropriate? Commented Aug 12, 2014 at 10:02
  • @ThusithaThilinaDayaratne No. The JVM doesn't 'handle the buffer for us'. You don't seem to know what this method actually does. Have a look at the Javadoc.
    – user207421
    Commented Aug 12, 2014 at 10:03

1 Answer 1

0

You're reading from the socket until end of stream, and you're never causing end of stream, as you are never closing the socket at the sender. Either close the socket or don't read until end of stream.

1
  • That my problem ! Thanks, I will had an "end of transmission" String in my protocol and stop reading the socket when I have it in my buffer.
    – Max
    Commented Aug 12, 2014 at 11:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.