tags:

views:

814

answers:

4

I am using the SocketChannel Java class for a client/server application. I can send and retrieve data. But if my data contains '\n' then I do not read the correct data. For example

sending data = "Hi dear"
receiving data = "Hi dear"

sending data = "Hi dear\n\n i am fine"
receiving data = "Hi dear"

sending data = "Hi dear\\n\\n i am fine"
receiving data = "Hi dear\n\n i am fine"


ByteBuffer byteBuffer = ByteBuffer.allocate(BUFSIZE);
int nbytes = socketChannel.getChannel().read(byteBuffer);

I am using US-ASCII decoding. How can I overcome this type of problem?

Thanks, Deepak

A: 

I think you're having an issue with the ByteBuffer on the allocation.

I've had issues previously with SocketChannel where data just seemed to get held up. I would recommend doing some sysouts on ByteBuffer

while(ByteBuffer.hasRemaining(){
    socketChannel.write(buffer) //writing data
    System.out.printline(socketChanel.remaining() + " : bytes to go");

}

This may be a step in the right direction to issolating the issue

PSU_Kardi
+1  A: 

The read operation doesn't guarantee to read all of the available data, nor read any data at all if the SocketChannel is in non blocking mode. In the case above, you may need more than one call to read all of the data transmitted.

You need some sort of way for the receiving code to know when it has read all of the data. This means either having the sender send information about the content of the data (length etc.) as part of an agreed protocol, or by using the Channel for only one chunk of data and reading the Channel until it is closed.

Neal Maloney
Should also mention you can specify if the Channel is in non-blocking mode....
PSU_Kardi
Yes, Channel is in non- blocking mode.Please go through my example once again.
Deepak
It really depends on what kind of socket it is. A stream socket (f.e., TCP) has no boundary preservations, like you alluded to. However, a datagram socket (f.e., UDP) is very much about packets transmitted and received with explicit boundaries.
Brad Wilson
It is TCP Socket.I can read data if there is \\n rather than \n.I think it is the issue of escape characters. So how to overcome from this type of problem.
Deepak
Deepak: You "overcome" the problem by continuing to read until the socket says there is no more data left. You will not ever be guaranteed to get all the data in one pass, even if it seems like it works that way most of the time.
Brad Wilson
+1  A: 

Actually It is the problem for escape characters. So whether any type of decoding stanard will help me or not?

Deepak
A: 

If you are sending data as binary as you suggest, there is no reason that \n would be treated differently to any other byte. If it were things like images would get corrupted all the time. The encoding shouldn't make any difference if you have using characters <= 127.

I would suggest your application isn't working the way you think it is and that you are reading strings which assume a \n termination somewhere in your code.

Can you post the code for writing and reading the string. i.e. how do you determine when the string ends?

Peter Lawrey