哪些因素导致数据无法通过 python(或 python/node.js)中的套接字? [英] What factors cause data not to go through in sockets in python (or python/node.js)?

查看:60
本文介绍了哪些因素导致数据无法通过 python(或 python/node.js)中的套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(关于改写问题的建议?)

(Suggestions for rephrasing questions?)

我使用客户端/服务器模式通过套接字发送数据.什么时候我运行python(在pycharms中)接收端的输出没有得到数据.但是,当我使用重新运行图标(在 pycharms 中)时,数据会通过.

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.

这是客户端代码.它正在使用 net (node.js)

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);
});

更新:添加了 server.js 代码

update: added server.js code

推荐答案

我对问题有所猜测.

客户端调用sendall发送一些数据,然后调用recv得到响应.

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

但是服务器在任何地方都没有任何代码来发送响应.所以客户端将永远等待.这意味着它永远不会关闭套接字.这意味着服务器中的 socket.on('close') 回调将永远不会被调用.由于这是您打印正文的地方,因此不会打印任何内容.

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.

当您点击重新运行"图标时,大概它会在启动新客户端之前杀死客户端的前一个实例.杀死客户端将导致套接字关闭,因此服务器将最终到达 console.log(body) 调用.

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.

如果是前者,只需从客户端中删除 data = client.recv(50) 行(以及最后的 print).

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

如果是后者,请在服务器的某处添加一行,将某些内容发送回客户端.您很可能希望在 on('data') 回调中使用它,但这不是唯一它可以想象的有意义的地方(例如,您可以在套接字连接回调的顶部).你也可以添加一个回调,这样你就可以看到它成功了.所以:

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...');
    });
});

这篇关于哪些因素导致数据无法通过 python(或 python/node.js)中的套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆