0

I have a problem with my client - server apps which I developed in JAVA. I use Socket and ServerSocket from java.net* package. When client connect to the server, client sends message for example 7200 bytes. In server I use InputStream. Sometimes I receive whole message (7200 bytes), but many times I receive less than 7200 bytes. Is there any way in JAVA to receive whole message and close connection? Maybe should I use other library to tcp/ip connection in JAVA?

1
  • Instead of describing your code consider posting actual (but simplified to minimum) code which will let us reproduce your problem, or at least see how you organized your solution. Commented Mar 18, 2015 at 18:41

2 Answers 2

2

Probably this will help: InputStream.read() documentation.

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer.

This method will not block until the entire byte buffer filled up or stream end reached. Instead, it returns as much data as available at the moment.

This is not likely to happen when reading from files, but quite normal for sockets.

The actual number of bytes, which were written into byte-buffer is returned, so you can you that to decide if there's enough data.

You can use read(buf, start, len), to start not from beginning of buffer but continue data block. For example, if you are reading exactly 7200 bytes, do:

byte [] buf = new byte[7200];
int len = 7200;
int pos=0;

while(len > 0) {
  int rd = is.read(buf, pos, len);
  if(rd<0) {
      //premature EOF
      break;
  }
  pos += rd;
  len -= rd;
}

Otherwise if you do not know message length up front you have several options. Among them:

  1. Send message length as first 4 bytes, then always read 4 bytes first, then allocate buffer of necesary size and read into it
  2. Read into buffer until you receive "END-OF-MESSAGE" marker. Like, for example "END-OF-LINE". When you find it - stop reading and process message.
Sign up to request clarification or add additional context in comments.

4 Comments

Ok this is simple when I know message length. But I don't know anything about message length...
So why I can't receive whole message without length bytes and special marker?
@karoluch Message is a concept you defined within the scope of your code. Socket has no concept of a "Message", it just provides a Stream of data from point to point. There is no difference for the socket if you call write(byte[1]) 7200 times or write(byte[7200]) 1 time.
In simple words: for a file, you can find it's size. For a "stream" size is not known up front.
0

I suggest jgroups (http://www.jgroups.org/manual/html/index.html) from jboss to tcpip communications

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.