简体   繁体   English

仅当client.py运行时,Python Socket errno 10054

[英]Python Socket errno 10054 only when client.py runs

There are some other posts about this issue but none did help me with mine. 关于这个问题还有其他一些帖子,但没有一个对我有帮助。 I'm trying to build a total simple server - client relationship in python 我正在尝试在python中构建一个简单的服务器 - 客户端关系

server.py server.py

#!/usr/bin/python
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1",8889))
s.listen(1)   
try:

    while True:
        client, add = s.accept()
        data = client.recv(1024)
        if not data:
            print 'No data'
        print data

finally:
    s.close()

client.py client.py

#!/usr/bin/python
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1",8889))


try:
    while True:
        message = 'Foo'
        s.send(message)
        ans = s.recv(1024)
        print ans
finally:
    s.close()

I start by running the server first, but when I try to run the client I'm getting this Errno 10054 --> An existing connection was forcibly closed by the remote host While request the browser with the ip and the related port, I receive some data. 我首先运行服务器,但是当我尝试运行客户端时,我得到这个Errno 10054 --> An existing connection was forcibly closed by the remote host在请求带有ip和相关端口的浏览器时,我收到一些数据。 I'm quiet new to networking, so please explain what might be obvious wrong in my code. 我对网络很陌生,所以请解释我的代码中可能出现的明显错误。

EDIT* Main issue is, that the client is somehow wrong, because it returns also an empty string on recv 编辑*主要问题是,客户端在某种程度上是错误的,因为它在recv上也返回一个空字符串

Thank you in advance 先感谢您

I am guessing: 我在猜测:

The server accepts one socket and then does 服务器接受一个套接字然后执行

    client, add = s.accept()
    data = client.recv(1024)
    ...
    client, add = s.accept()

The client does this in the mean time: 客户端同时执行此操作:

s.send(message)
ans = s.recv(1024) # blocks until timeout

If now an other client connects to the server then client is replaced, the socket garbage collected and closed. 如果现在其他客户端连接到服务器,则替换client ,收集并关闭套接字垃圾。 s.recv(1024) will then tell that the connection is reset. 然后, s.recv(1024)将告知连接已重置。

Have a look at import select or twisted (google around) to handle multiple connections at once. 看看import select或扭曲(谷歌周围)一次处理多个连接。

Main issue is, that the client is somehow wrong, because it returns also an empty string on recv

The client isn't receiving anything from the server because the server is not sending anything. 客户端没有从服务器接收任何内容,因为服务器没有发送任何内容。

On the server side, after print data , adding client.send(data) will send the string back to the client. 在服务器端,在print data ,添加client.send(data)会将字符串发送回客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM