Python TCP 套接字数据有时会丢失部分。套接字溢出?

标签 python python-2.7 sockets tcp buffer-overflow

简短描述:

客户端通过 TCP 套接字发送服务器数据。数据长度不一,是由分隔符“~~~*~~~”分割的字符串

在大多数情况下,它工作正常。一阵子。几分钟后,数据到处都是。所以我开始跟踪问题,但数据最终出现在错误的位置,因为完整的事情还没有通过。

所有内容都进入服务器脚本,并由不同的分隔符 -NewData-* 解析,然后放入队列中。这是代码:

是的,我知道缓冲区很大。不,我不会一次发送那种大小的数据,但我一直在玩弄它。

class service(SocketServer.BaseRequestHandler):
    def handle(self):
        data = 'dummy'

        #print "Client connected with ", self.client_address
        while len(data):
            data = self.request.recv(163840000)
            #print data
            BigSocketParse = []
            BigSocketParse = data.split('*-New*Data-*')

            print "Putting data in queue"
            for eachmatch in BigSocketParse:
                #print eachmatch
                q.put(str(eachmatch))

            #print data
            #self.request.send(data)

        #print "Client exited"
        self.request.close()


class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

t = ThreadedTCPServer(('',500), service)
t.serve_forever()

然后我有一个线程在运行而不是 q.empty(): 它通过另一个分隔符“~~~*~~~”解析数据

所以这会工作一段时间。我发送的数据类型示例:

2016-02-23 18:01:24.140000~~~*~~~Snowboarding~~~*~~~Blue Hills~~~*~~~Powder 42
~~~*~~~Board Rental~~~*~~~15.0~~~*~~~1~~~*~~~http://bigshoes.com
~~~*~~~No Wax~~~*~~~50.00~~~*~~~No Ramps~~~*~~~2016-02-23 19:45:00.000000~~~*~~~-15

但是事情开始破裂了。所以我拿了一些控制数据并循环发送。会工作一段时间然后结果开始在错误的地方结束。这出现在我的队列中:

2016-02-23 18:01:24.140000~~~*~~~Snowboarding~~~*~~~Blue Hills~~~*~~~Powder 42
~~~*~~~Board Rental~~~*~~~15.0~~~*~~~1~~~*~~~http://bigshoes.com
~~~*~~~No Wax~~~*~~~50.00~~~*~~~No Ramps~~~*~~~2016-02-23 19:45:00.000000~~~*~

删除最后一个“~~-15”。

因此,完全相同的数据可以工作,但后来却不能。这对我来说意味着某种溢出。

客户端连接是这样的:

class Connect(object):

    def connect(self):
        host = socket.gethostname() # Get local machine name
        #host = "127.0.0.1"
        port = 500                # Reserve a port for your service.
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #print('connecting to host')
        sock.connect((host, port))
        return sock

    def send(self, command):
        sock = self.connect()
        #recv_data = ""
        #data = True

        #print('sending: ' + command)
        sock.sendall(command)
        sock.close()
        return

它不等待响应,因为我不希望它徘徊等待响应。但它会关闭套接字,并且(据我所知)我不需要刷新套接字缓冲区或任何它应该在连接关闭时自行清除的内容。

非常感谢对此的任何帮助。在这一点上,它让我有点空闲。

更新:

我在我的本地机器和一个非常强大的服务器上运行它,我不得不相信这是一个硬件问题。服务器/客户端都在本地运行,并且使用套接字作为它们进行通信的一种方式,因此我认为延迟不会是原因。

我一直在研究 TCP 通信的问题。我觉得我很快就会超出我的理解范围,但我开始怀疑它是否不是溢出而是拥塞之王。

如果客户端上的 sendall 不能确保发送所有内容,则可能在服务器端进行某种计时器/检查以确保没有更多内容。

最佳答案

基本问题是您:

data = self.request.recv(163840000)

line 保证一次接收所有数据(不管你的缓冲区有多大)。

为了正常运行,您必须处理不能一次获取所有数据的情况(您需要跟踪您的位置,并附加到它)。请参阅 Python docs on using a socket 中的相关示例:

Now we come to the major stumbling block of sockets - send and recv operate on the network buffers. They do not necessarily handle all the bytes you hand them (or expect from them), because their major focus is handling the network buffers. In general, they return when the associated network buffers have been filled (send) or emptied (recv). They then tell you how many bytes they handled. It is your responsibility to call them again until your message has been completely dealt with.

关于Python TCP 套接字数据有时会丢失部分。套接字溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35589126/

相关文章:

python - 从 python 中的 gzip 文件中读取

VB.net TCPListner 窗口服务

c++ - SSL_connect() 产生证书验证失败

python - 如何通过将其索引视为键来展平包含列表的字典?

python - 自定义列表遍历和修改

python - 从数据框中删除匹配项

windows - 属性错误 : StringIO instance has no attribute 'encoding'

c++ - 通过套接字持续丢失数据(但在使用本地主机连接时不会)

python - 内存问题 : Should I be writing to file/database if I'm using swap? (Python)

python - 字典中的元组