套接字连接接收字节python

时间:2015-10-27 21:40:16

标签: python sockets tcp buffer

我试图通过Python中的套接字发送数据流。到目前为止,我设法创建一个dummy_data_gen.py,它将包含4个浮点数的行发送到server.py。但是,我仍然在所有设置的稳定性方面存在问题。

server.py:

import sys
import time
import socket
import numpy as np

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 5002)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

# Create a list to store the incoming data
data = []

while True:

    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        while True:

            incoming_data = connection.recv(48).split(',')
            print incoming_data

            event = float(incoming_data[0]), float(incoming_data[1]), float(incoming_data[2]), float(incoming_data[3])

            data += [event]

            time.sleep(0.01)

    finally:
        # Clean up the connection
        connection.close()

dummy_data_gen.py

import sys
import time
import socket

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 5002)
sock.connect(server_address)

file = '../data/myfile.txt'

# Simulating a real-time data stream at 100 Hz
try:
    with open(file) as f:
        for line in f:
            sock.sendall(line)
            time.sleep(0.01)

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

我的问题是,有时候通信工作正常,但是,我遇到的情况是每行收到的数据比我应该多。在下面的输出示例中,前7行是正确的,但是以下行不正确,因此存在问题:

['391910745379', '24.134277', '-1.9487305', '-117.373535', '\n']
['391920745379', '24.434082', '-1.3491211', '-117.373535', '\n']
['391930745379', '23.68457', '-0.5996094', '-116.62402', '\n']
['391940745379', '24.434082', '-1.0493164', '-115.57471', '\n']
['391950745379', '24.134277', '-1.0493164', '-116.47412', '\n']
['391960745379', '23.234863', '-1.0493164', '-116.47412', '\n']
['391970745379', '24.583984', '-0.89941406', '-116.92383', '\n']
['391980745379', '23.384766', '-0.2998047', '-116.62402', '\n39']
['1990745379', '23.68457', '-0.5996094', '-115.72461', '\n39200']
['0745379', '23.834473', '-0.44970703', '-115.87451', '\n392010']
['745379', '23.534668', '-1.0493164', '-114.9751', '\n392020745']
['379', '23.384766', '-1.7988281', '-115.72461', '\n39203074537']
['9', '22.935059', '-0.44970703', '-114.9751', '\n392040745379', '']

我尝试使用connection.recv字节,但我还是面临这个问题。

EDIT1 :根据一些建议我修改了server.py,如下所示:

del_message = '\n'
del_stream = ','

while True:
        _buffer += connection.recv(1)
        if del_message in _buffer:
            incoming_data = _buffer.split(del_stream)
            event = float(incoming_data[0]), \
                        float(incoming_data[1]), \
                            float(incoming_data[2]), \
                                float(incoming_data[3])

这种方法似乎解决了我的问题,但性能非常慢。我的文件包含大约6300行,这些行实际上是在70秒内发送的(在我的虚拟数据生成器上关闭套接字的时间间隔)。但是,我花了差不多10分钟才收到所有6300行。似乎我在开始时每秒收到更多的样本,而不是在流的末尾。

1 个答案:

答案 0 :(得分:1)

如果您的消息协议使用换行符终止消息,则需要编写一些代码来实现该协议。它不会通过魔法工作。

你需要一个"收到一条消息"功能,其中"消息"被定义为"由换行符"分隔的字节序列。您从未编写任何此类功能,因此您不会接收消息,只会接收您正在发送的字节块。