Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

C socket to Java Socket, detecting the end of a message

843790Jun 30 2009 — edited Jul 1 2009
The data comes in via a Socket in a C application (I have no control over this code, and limited visibility into it).

I control the Server side (listener).
I want this code to be in Java (I really do not want to resort to the use of jni to pass the data to the java layer).

My Problem is, all of my attempts to determine the end of the message stream are failing.

The sending side is NOT
- closing the connection after the message is sent, or in any way sending an end of stream signal. Therefore loop until reading -1 does not work
- Using any sort of character at the end, that I can use to determine the end of message (I expected maybe a null terminator, but this is not there).
- Sending messages of a consistent length

The sending side IS
- placing the length of the message into the C send command.
i.e. they do this:
send(sock, msgbuffer, msglength, 0);


The send is inside a while loop, that loops back to send the next message after it receives an acknowledge for the message.
However, the connect, is outside of that loop.

My java code is using a bufferedreader and read, not a recv (as I would use in C).

Is there a recv command, for a java socket, that works like a recv in C?
All the examples I am finding use the bufferedreader approach. Most of them use readln, which requires the carriage return, a few use read, loop until -1.

Is there a way for me to get the msg length, sent with the C send command, and use that value?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 29 2009
Added on Jun 30 2009
6 comments
720 views