tags:

views:

44

answers:

1

Hi,

I have a c++/windows program that receives data from another c++ program via a WM_COPYDATA message. It is then supposed to use Sockets/winsock to send this message on to a server written in Java. The client connects to the server fine, but it doesn't seem to be able to send messages in a timely fashion. However, once the client is closed down, all the messages it should have been sending get sent in one big lump. Here is an example of the terminal output of the Java server:

Server Starting up.
Client Accepted.
hi from clienttesttesttesttesttesttesttesttesttesttesttesttesttesttest

the first two lines are output by the Java server when those events happen. The last line is messages from the client. The client sends "hi from client" right after winsock is initialized, and then "test" at various points later in the program as it receives data from the other c++ program via WM_COPYDATA messages.

Here is the Java server code:

BufferedReader in = new BufferedReader(new InputStreamReader(
                                            clientSocket.getInputStream()));
String incomingLine;
while((incomingLine = in.readLine()) != null)
    System.out.println(incomingLine);

Here is the c++ function where the messages are sent:

void sendDataWinsock(char* text){    
    int result = send(ConnectSocket,text,(int)strlen(text),0);
}

And here is a section of WndProc where the WM_COPYDATA messages are processed:

case WM_COPYDATA: 
    sendDataWinsock("test");
    break;

Does anyone know why it is doing this? It is as if the client program is adding all these messages to a queue of things it should be sending, but is too busy to send them immediately, and so only sends them as the program is closing down, when it no longer has to process Windows messages. Or, I suppose, the error could actually be in the Java code - I am fairly new to this.

+1  A: 

You are reading lines on the server, but you are not sending lines.

That means your server sits there, receiving data but waiting to return a line of text back to your program from readLine() , which does not happen since no newlines , \n, gets sent. When the client exits, readLine() gives you back the data it read thus far.

nos
Whoops, I really should have caught that. Thanks.
Oliver
That's undoubtedly the reason that all the messages get combined (recipient-side), but the Nagle algorithm will cause writes to be delayed (sender-side) for a short time in order to send more data in fewer packets.
Ben Voigt
The Nagle algorithm *can* cause writes to be delayed under certain circumstances. It doesn't delay all writes.
EJP