tags:

views:

134

answers:

2

I have a TCP socket client receiving messages (data) from a server. messages are of the type length (2 bytes) + data (length bytes), delimited by STX & ETX characters.

I'm using a bufferedReader to retrieve the two first bytes, decode the length, then read again from the same bufferedReader the appropriate length and put the result in a char array.

most of the time, I have no problem, but SOMETIMES (1 out of thousands of messages received), when attempting to read (length) bytes from the reader, I get only part of it, the rest of my array being filled with "NUL" characters. I imagine it's because the buffer has not yet been filled.

char[] bufLen = new char[2];
_bufferedReader.read(bufLen);

int len = decodeLength(bufLen);

char[] _rawMsg = new char[len];
_bufferedReader.read(_rawMsg);

return _rawMsg;

I solved the problem in several iterative ways:

  • first I tested the last char of my array: if it wasn't ETX I would read chars from the bufferedReader one by one until I would reach ETX, then start over my regular routine. the consequence is that I would basically DROP one message.

  • then, in order to still retrieve that message, I would find the first occurence of the NUL char in my "truncated" message, read & store additional characters one at a time until I reached ETX, and append them to my "truncated" messages, confirming length is ok.

it works also, but I'm really thinking there's something I could do better, like checking if the total number of characters I need are available in the buffer before reading it, but can't find the right way to do it...

any idea / pointer ?

thanks !

+2  A: 

The InputStream read method may return short reads; you must check the return value to determine how many characters were read, and continue reading in a loop until you get the number you wanted. The method may block, but it only blocks until some data is available, not necessarily all the data you requested.

Most people end up writing a "readFully" method, like DataInputStream, which reads the amount of data expected, or throws an IOException:

static public int readFully(InputStream inp, byte[] arr, int ofs, int len) throws IOException {
    int                                 rmn,cnt;

    for(rmn=len; rmn>0; ofs+=cnt, rmn-=cnt) {
        if((cnt=inp.read(arr,ofs,rmn))==-1) { 
            throw new IOException("End of stream encountered before reading at least "+len+" bytes from input stream"); 
            }
        }
    return len;
    }
Software Monkey
+1  A: 

Here is a sample server that I have used for testing The main rcv is structured like

while((chars_read = from_server.read(buffer)) != -1)
{
    to_user.write(buffer,0,chars_read);
    to_user.flush();
}

The actual whole server is below ...

   public static void main(String[] args) throws IOException
   {
      try
      {
         if (args.length != 2)
            throw new IllegalArgumentException("Wrong number of Args");

         String   host = args[0];
         int      port = Integer.parseInt(args[1]);

         Socket   s = new Socket(host,port);

         final Reader from_server = new InputStreamReader(s.getInputStream());
         PrintWriter to_server = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

         BufferedReader from_user = new BufferedReader(new InputStreamReader(System.in));
         final PrintWriter to_user = new PrintWriter(new OutputStreamWriter(System.out));

         to_user.println("Connected to " + s.getInetAddress() + ":" + s.getPort());

         Thread t = new Thread()
         {
            public void run()
            {
               char [] buffer = new char[1024];
               int chars_read;
               try
               {
                  while((chars_read = from_server.read(buffer)) != -1)
                  {
                     to_user.write(buffer,0,chars_read);
                     to_user.flush();
                  }
               }
               catch(IOException e)
               {
                  to_user.println(e);
               }

               to_user.println("Connection closed by server");
               to_user.flush();
               System.exit(0);
            }
         };

         t.setPriority(Thread.currentThread().getPriority() + 1);
         t.start();

         String line;
         while ((line = from_user.readLine()) != null)
         {
            to_server.println(line);
            to_server.flush();
         }

         //t.stop();
         s.close();
         to_user.println("Connection closed by client");
         to_user.flush();
      }
      catch(Throwable e)
      {
         e.printStackTrace();
         System.err.println("Usage : java TCPClient <hostname> <port>");
      }
   }
Romain Hippeau