我目前正在开发一个python项目,需要通过Python套接字从客户端传输到服务器.这是我当前的代码,但是不传输整个文件,但根据文件大小,总会丢失一些或者额外的字节.
_con和con是通过python套接字的连接包装器连接.
客户:
def _sendFile(self, path):
    sendfile = open(path, 'rb')
    data = sendfile.read()
    self._con.sendall(data)
    self._con.send(bytes('FIN', 'utf8'))
    # Get Acknowledgement
    self._con.recv(6)
def _recieveFile(self, path):
    # Recieve the file from the client
    writefile = open(path, 'wb')
    i = 0
    while (1):
        rec = self.con.recv(1024)
        if (rec.endswith(b'FIN')):
            break
        writefile.write(rec)
    self.con.send(b'ACK')
虽然您遇到的第一个问题是没有将收到的最后一块数据写入输出文件,但您还有其他一些问题.
您可以通过将if语句更改为以下内容来解决当前问题:
if (rec.endswith(b'FIN')):
    writefile.write(rec[:-3]) # Ignore the last 3 bytes
    break
你还有其他问题:
如果文件包含字符FIN,则1024中大约有1个字符将成为读取缓冲区中的最后3个字符,并且您的代码将错误地将其读作结束标记,并提前终止.
在1024中FIN有两个机会,标记将被分成两次调用read(),rec结尾为F或者FI.
这两个问题都是由TCP作为基于流的协议引起的,在用户级别没有分组的概念.
一个明显的解决方法是在文件传输之前使用固定大小的长度指示,接收器读取此值,然后读取正确的字节数.
像这样的东西:
def _sendFile(self, path):
    sendfile = open(path, 'rb')
    data = sendfile.read()
    self._con.sendall(encode_length(len(data)) # Send the length as a fixed size message
    self._con.sendall(data)
    # Get Acknowledgement
    self._con.recv(1) # Just 1 byte
def _recieveFile(self, path):
    LENGTH_SIZE = 4 # length is a 4 byte int.
    # Recieve the file from the client
    writefile = open(path, 'wb')
    length = decode_length(self.con.read(LENGTH_SIZE) # Read a fixed length integer, 2 or 4 bytes
    while (length):
        rec = self.con.recv(min(1024, length))
        writefile.write(rec)
        length -= sizeof(rec)
    self.con.send(b'A') # single character A to prevent issues with buffering
当然,在发送/接收长度时,您需要知道长度字段中的字节顺序.
| 归档时间: | 
 | 
| 查看次数: | 4283 次 | 
| 最近记录: |