What factors cause data not to go through in sockets in python (or python/node.js)?
Question: (Suggestions for rephrasing questions?)

I'm sending data over a socket with client/server pattern. When I run python
(in pycharms) the output on the receiving end doesn't get data. However, when
I use the re-rerun icon (in pycharms) the data goes through.

I'm confused to be honest by this behavior and not sure what to ask besides
telling you what I observe.

Here is the client code. It's talking to server setup with `net` (node.js)

**client.py**

    
    
    import socket   // python version 2.7.*
    
    if __name__ == "__main__":
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect(('127.0.0.1', 7000))
        client.sendall("test data to transmit") 
    
        data = client.recv(50)
        client.close()
    
        print 'Received', repr(data)
    

**server.js**

    
    
    var net = require('net');  // node v0.10.21 (latest)
    var PYTHON = {HOST :'127.0.0.1', PORT :7000};
    
    net.createServer(function(socket) {
        console.log('CONNECTION for Python: ' + socket.remoteAddress +':'+ socket.remotePort);
    
        var body = '';
        socket.on('data', function(data) {
            console.log('DATA ' + socket.remoteAddress );
            body += data;
        });
    
        socket.on('close', function(err) {
            console.log('finish transmitting data... ');
            console.log(body);
        });
    }).listen(PYTHON.PORT, PYTHON.HOST, function() {
        console.log('---->socket to talk to python '
                        + PYTHON.HOST + ':' + PYTHON.PORT);
    });
    

**update** : added server.js code


Answer: I have a guess at the problem.

The client calls `sendall` to send some data, then calls `recv` to get a
response.

But the server doesn't have any code anywhere to send a response. So the
client will wait forever. That means it will never close the socket. Which
means the `socket.on('close')` callback in your server will never get called.
Since that's where you're printing the body, nothing will get printed out.

When you hit the "re-run" icon, presumably it kills the previous instance of
your client before launching a new one. Killing the client will cause the
socket to close, so the server will finally get to the `console.log(body)`
call.

And it's definitely possible (but certainly not guaranteed) for the new client
to start up and connect before the server can finish processing the old
client's close, so these could show up in either order.

* * *

So, how can you fix this?

Well, I'm not sure whether the problem is that the client is trying to receive
data when it shouldn't be expecting any, or that the server isn't sending data
when it should be.

If it's the former, just remove the `data = client.recv(50)` line (and the
`print` at the end) from the client.

If it's the latter, add a line somewhere in the server that sends something
back to the client. You most likely want it in the `on('data')` callback, but
that's not the _only_ place it could conceivably make sense (for example, you
could do it right at the top of the socket connection callback). And you might
as well add a callback so you can see it succeeding. So:

    
    
    socket.on('data', function(data) {
        // your existing code here
        socket.write("Here's a response", function() {
            console.log('finish writing...');
        });
    });
    

