如何send/receive多个数据? python插座?

how to send/receive multiple data? python socket?

我正在尝试 send/receive 多个数据。服务器完美发送文件名和文件数据。问题出在客户端。我创建了一个名为文件名和文件数据的两个变量。文件名变量接收文件名和文件数据。 filedata 没有收到任何东西,因为 filename 收到了所有文件名和文件数据。不知道为什么filename收到的是filename+data?我该如何解决这个问题?

这是我的代码:

server.py

import socket

def send():
    host = 'localhost'
    port = 9999
    address = (host, port)

    sock = socket.socket()
    sock.bind(address)
    sock.listen(5)
    print('listining for connection...')

    con,addr = sock.accept()
    print('Got connection from',addr)

    file1name = 'file1.txt'
    con.send(file1name.encode('utf-8'))
    file1data = 'this is file 1'
    con.send(file1data.encode('utf-8'))

    file2name = 'file2.txt'
    con.send(file2name.encode('utf-8'))
    file2data = 'this is file 2'
    con.send(file2data.encode('utf-8'))

    con.close()
    sock.shutdown(1)
    sock.close()
    print('connection closed!')

send()

client.py

import socket

host = 'localhost'
port = 9999
address = (host, port)

sock = socket.socket()
sock.connect(address)
print('connected to', address)

while True:
    filename = sock.recv(60).decode('utf-8')
    if not filename:
        break
    print('filename -', filename)
    data = sock.recv(60).decode('utf-8')
    print('data -', data)


sock.shutdown(1)
sock.close()

输出server.py

listining for connection...
Got connection from ('127.0.0.1', 36886)
connection closed!

输出client.py

connected to ('localhost', 9999)
filename - file1.txtthis is file 1file2.txtthis is file 2
data - 

主要问题是您必须在客户端开始接收之前将数据大小发送给客户端。

我假设服务器发送一个 unsigned int 32 位(4 字节)来表示大小。

参见 client.py

中的 soc.recv(4)

server.py

from socket import *
import os
import ctypes

HOST = 'localhost'
PORT = 9999
ADDRESS = (HOST, PORT)
BUF_SIZE = 1024
PATH = './uploads'

soc = socket(AF_INET, SOCK_STREAM)
soc.bind(ADDRESS)
soc.listen(5)
print('listen for connection...')
con,addr = soc.accept()
print('got connection from', addr)
print('-------------------------')

def sendFile(socket, path):
    filename = os.path.basename(path)

    filename_bytes = filename.encode('utf-8')
    filename_size = ctypes.c_uint32(len(filename_bytes))
    file_size = ctypes.c_uint32(os.stat(path).st_size)

    socket.send(bytes(filename_size)) # send uint32 (4 bytes)
    socket.send(filename_bytes)
    socket.send(bytes(file_size)) # send uint32 (4 bytes)

    with open(path, 'rb') as f:
        filecontent = f.read(BUF_SIZE)
        while filecontent:
            socket.send(filecontent)
            filecontent = f.read(BUF_SIZE)

files = os.listdir(PATH)
num_files = ctypes.c_uint32(len(files))
con.send(bytes(num_files))
for file in files:
    sendFile(con, os.path.join(PATH, file))

client.py

from socket import *
import os
import ctypes

HOST = 'localhost'
PORT = 9999
ADDRESS = (HOST, PORT)
BUF_SIZE = 1024
PATH = './downloads'

soc = socket(AF_INET, SOCK_STREAM)
soc.connect(ADDRESS)
print('connected to', ADDRESS)

def recvFile(socket):
    tmp = soc.recv(4)
    filename_size = int.from_bytes(tmp, byteorder='little')
    print('filename_size =', filename_size)
    filename = soc.recv(filename_size).decode('utf-8')
    print('filename =', filename)
    tmp = soc.recv(4)
    file_size = int.from_bytes(tmp, byteorder='little')
    print('file_size =', file_size)

    with open(os.path.join(PATH, filename), 'wb') as f:
        while file_size > 0:
            filecontent = soc.recv(min(BUF_SIZE, file_size))
            f.write(filecontent)
            file_size -= len(filecontent)
    print('data recvd')

tmp = soc.recv(4)
num_files = int.from_bytes(tmp, byteorder='little')

for i in range(num_files):
    print('{}/{}'.format(i+1, num_files))
    recvFile(soc)

soc.shutdown(SHUT_RDWR)
soc.close()
print('socket closed')