为什么主机中止连接? [英] Why is host aborting connection?

查看:75
本文介绍了为什么主机中止连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自学Python网络,回想起我在自学线程时,遇到过此页面,所以我复制了脚本,为Python 3.1.1更新了脚本并运行了它们.他们工作得很好.

I'm teaching myself Python networking, and I recalled that back when I was teaching myself threading, I came across this page, so I copied the scripts, updated them for Python 3.1.1 and ran them. They worked perfectly.

然后我做了一些修改.我的目标是做一些简单的事情:

Then I made a few modifications. My goal is to do something simple:

  1. 客户端腌制一个整数并将其发送到服务器.
  2. 服务器接收腌制的整数,对其进行腌制,将其加倍,然后腌制并将其发送回客户端.
  3. 客户收到腌制(加倍)的整数,对其进行腌制并输出.

这是服务器:

import pickle
import socket
import threading

class ClientThread(threading.Thread):
    def __init__(self, channel, details):
        self.channel = channel
        self.details = details
        threading.Thread.__init__ ( self )

    def run(self):
        print('Received connection:', self.details[0])
        request = self.channel.recv(1024)
        response = pickle.dumps(pickle.loads(request) * 2)
        self.channel.send(response)
        self.channel.close()
        print('Closed connection:', self.details [ 0 ])

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 2727))
server.listen(5)

while True:
    channel, details = server.accept()
    ClientThread(channel, details).start()

这是客户:

import pickle
import socket
import threading

class ConnectionThread(threading.Thread):
    def run(self):
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect(('localhost', 2727))

        for x in range(10):
            client.send(pickle.dumps(x))
            print('Sent:',str(x))
            print('Received:',repr(pickle.loads(client.recv(1024))))

        client.close()

for x in range(5):
    ConnectionThread().start()

服务器运行良好,当我运行客户端时,它成功连接并开始发送整数,并按预期将接收到的整数加倍.但是,很快它就排除了:

The server runs fine, and when I run the client it successfully connects and starts sending integers and receiving them back doubled as expected. However, very quickly it exceptions out:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python30\lib\threading.py", line 507, in _bootstrap_inner
    self.run()
  File "C:\Users\Imagist\Desktop\server\client.py", line 13, in run
    print('Received:',repr(pickle.loads(client.recv(1024))))
socket.error: [Errno 10053] An established connection was aborted by the softwar
e in your host machine

服务器继续运行并可以正常接收连接;只有客户端崩溃.是什么原因造成的?

The server continues to run and receives connections just fine; only the client crashes. What's causing this?

我让客户端使用以下代码:

I got the client working with the following code:

import pickle
import socket
import threading

class ConnectionThread(threading.Thread):
    def run(self):
        for x in range(10):
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            client.connect(('localhost', 2727))
            client.send(pickle.dumps(x))
            print('Sent:',str(x))
            print('Received:',repr(pickle.loads(client.recv(1024))))
            client.close()

for x in range(5):
    ConnectionThread().start()

但是,我仍然不知道发生了什么.这不是只是多次打开和关闭插座吗?对此是否应该有时间限制(关闭后不久就不能打开插座)?

However, I still don't understand what's going on. Isn't this just opening and closing the socket a bunch of times? Shouldn't there be time limitations to that (you shouldn't be able to open a socket so soon after closing it)?

推荐答案

您的客户端现在是正确的-您要打开套接字,发送数据,接收答复,然后关闭套接字.

Your client is now correct - you want to open the socket send the data, receive the reply and then close the socket.

错误原始错误是由服务器在发送第一个响应后关闭套接字导致的,当客户端尝试在同一连接上发送第二条消息时,该消息导致客户端收到连接关闭消息.

The error original error was caused by the server closing the socket after it sent the first response which caused the client to receive a connection closed message when it tried to send the second message on the same connection.

但是,我还是不明白 这是怎么回事.这不只是 打开和关闭插座一堆 时间?

However, I still don't understand what's going on. Isn't this just opening and closing the socket a bunch of times?

是的.这是可以接受的,即使不是性能最高的处理方式.

Yes. This is acceptable, if not the highest performance way of doing things.

应该没有时间 限制(你不应该 能够在不久之后打开插座 关闭它)?

Shouldn't there be time limitations to that (you shouldn't be able to open a socket so soon after closing it)?

每次打开一个套接字,您都可以尽快打开一个客户端套接字,您将获得一个新的本地端口号,这意味着连接不会受到干扰.在上面的服务器代码中,它将为每个传入的连接启动一个新线程.

You can open a client socket as quickly as you like as every time you open a socket you will get a new local port number, meaning that the connections won't interfere. In the server code above, it will start a new thread for each incoming connection.

每个IP连接(source_address,source_port,destination_address,destination_port)有4个部分,并且必须不断更改此四边形(众所周知).对于客户端套接字,除了source_port之外的所有内容都是固定的,因此操作系统会为您更改.

There are 4 parts to every IP connection (source_address, source_port, destination_address, destination_port) and this quad (as it is known) must change for ever connection. Everything except source_port is fixed for a client socket so that is what the OS changes for you.

打开服务器套接字比较麻烦-如果您想快速打开新的服务器套接字,您的

Opening server sockets is more troublesome - if you want to open a new server socket quickly, your

server.bind(('', 2727))

然后,您需要阅读SO_REUSEADDR.

Above then you need to read up on SO_REUSEADDR.

这篇关于为什么主机中止连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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