2

I have a problem with nodejs. I have a python twisted tcp server and when I am trying to do this:

var socket = server.net.createConnection(server.controllerPort, 
                                         server.controllerHost);

socket.on('data', function(data) {
    log('RESPONSE: ' + data);
});

socket.on('connect', function() {

    socket.write("1 message \r\n");
    socket.write("2 message \r\n");

    socket.destroy();
});

instead of 2 messages, twisted receives "1 message \r\n 2 message \r\n")

Of course, I tested by using python, php, c++ and it works. But I am having trouble doing the same in nodejs.

2
  • Can you show a piece of your server side code? Possibly also a piece of the other clients? Commented May 12, 2014 at 11:27
  • twisted i took from examples(echo server) and it works fine. Commented May 12, 2014 at 13:34

1 Answer 1

1

TCP does not necessarily send data line-wise. So if you have on your server side the something like

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        ---do something with data---

then TCP makes no guarantees how often dataReceived will be called. It might, in principle, be called just once with "1 message \r\n2message \r\n", twice with "1 message \r\n" and "2 message\r\n", but also split differently, e.g., twice with "1 mes" and "sage \r\n2message".

You can force node.js to "flush" the write buffer to the socket after each call to socket.write as follows:

socket.setNoDelay(true);

(must be called before the socket.write, of course).

The wikipedia article on TCP provides more details on TCP, especially relevant is the section "Forcing data delivery". Details on socket.setNoDelay() in the node.js documentation.

Sign up to request clarification or add additional context in comments.

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.