Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何发送/接收多个数据?python套接字?_Python_Sockets_Client Server - Fatal编程技术网

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

如何发送/接收多个数据?python套接字?,python,sockets,client-server,Python,Sockets,Client Server,我正在尝试发送/接收多个数据。服务器完美地发送文件名和文件数据。问题出在客户身上。我创建了一个名为filename和filedata的两个变量。filename变量接收到带有文件数据的filename。filedata没有收到任何内容,因为filename收到了所有的filename和filedata。我不知道为什么filename收到filename+数据?我如何解决这个问题 这是我的密码: server.py client.py 输出服务器.py 输出客户端.py 主要问题是,您必须在客户端

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

这是我的密码:

server.py client.py 输出服务器.py 输出客户端.py
主要问题是,您必须在客户端开始接收数据之前将数据大小发送给客户端

我假设服务器发送一个32位(4字节)的无符号整数来表示大小

参见client.py中的soc.recv(4)

server.py
从套接字导入*
导入操作系统
导入ctypes
主机='localhost'
端口=9999
地址=(主机、端口)
BUF_大小=1024
路径='/上传'
soc=插座(AF_INET,SOCK_STREAM)
soc.bind(地址)
听一听(5)
打印('侦听连接…')
con,addr=soc.accept()
打印('从中获取连接',地址)
打印(“---------------------------”)
def sendFile(套接字,路径):
filename=os.path.basename(路径)
filename\u bytes=filename.encode('utf-8')
filename\u size=ctypes.c\u uint32(len(filename\u字节))
file_size=ctypes.c_uint32(os.stat(path).st_size)
socket.send(字节(文件名大小))#send uint32(4字节)
socket.send(文件名\字节)
socket.send(字节(文件大小))#send uint32(4字节)
打开(路径“rb”)作为f:
filecontent=f.read(基本大小)
而文件内容:
socket.send(文件内容)
filecontent=f.read(基本大小)
files=os.listdir(路径)
num_files=ctypes.c_uint32(len(files))
con.send(字节(num_文件))
对于文件中的文件:
sendFile(con,os.path.join(path,file))
client.py
从套接字导入*
导入操作系统
导入ctypes
主机='localhost'
端口=9999
地址=(主机、端口)
BUF_大小=1024
路径='./下载'
soc=插座(AF_INET,SOCK_STREAM)
soc.connect(地址)
打印('连接到',地址)
def recvFile(套接字):
tmp=soc.recv(4)
filename\u size=int.from\u字节(tmp,byteorder='little')
打印(“文件名大小=”,文件名大小)
filename=soc.recv(filename\u size).decode('utf-8')
打印('filename=',filename)
tmp=soc.recv(4)
文件大小=int.from字节(tmp,byteorder='little')
打印(“文件大小=”,文件大小)
将open(os.path.join(path,filename),'wb')作为f:
当文件大小>0时:
filecontent=soc.recv(最小值(BUF_大小,文件大小))
f、 写入(文件内容)
文件大小-=len(文件内容)
打印('数据记录')
tmp=soc.recv(4)
num_files=int.from_字节(tmp,byteorder='little')
对于范围内的i(num_文件):
打印(“{}/{}.”格式(i+1,num_文件))
记录文件(soc)
soc.停机(停机)
soc.close()
打印('套接字已关闭')

我感谢你的回答。但我不明白你们为什么用无符号变量?请向我解释一下。目的是发送大小以通知客户端有多少字节。所以我选择无符号整数,因为大小总是>=0。您还可以根据范围选择uint16或其他类型的uint。
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()
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()
listining for connection...
Got connection from ('127.0.0.1', 36886)
connection closed!
connected to ('localhost', 9999)
filename - file1.txtthis is file 1file2.txtthis is file 2
data -