如何发送/接收多个数据? python套接字?

时间:2019-12-19 14:37:59

标签: python sockets client-server

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

这是我的代码:

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 - 

1 个答案:

答案 0 :(得分:0)

主要问题是必须先将数据大小发送给客户端,然后才能开始接收。

我假设服务器发送了一个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')