问题描述
服务器
import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host= 'VAC01.VACLab.com'
port=int(2000)
s.bind((host,port))
s.listen(1)
conn,addr =s.accept()
data=s.recv(100000)
s.close
客户
import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host="VAC01.VACLab.com"
port=int(2000)
s.connect((host,port))
s.send(str.encode(sys.argv[1]))
s.close()
我希望服务器接收客户端发送的数据.
I want the server to receive the data that client sends.
尝试此操作时出现以下错误
I get the following error when i try this
客户侧
回溯(最近通话最近): 在第21行的文件"Client.py"中 s.send(sys.argv [1])TypeError:"str"不支持缓冲区接口
Traceback (most recent call last): File "Client.py", line 21, in s.send(sys.argv[1])TypeError: 'str' does not support the buffer interface
服务器端
文件"Listener.py",第23行,在 数据= s.recv(100000)socket.error:[Errno 10057]不允许发送或接收数据的请求因为未连接套接字,并且(当使用sendto呼叫)未提供地址
File "Listener.py", line 23, in data=s.recv(100000)socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using asendto call) no address was supplied
推荐答案
在服务器中,您使用 listening 套接字接收数据.它仅用于接受新连接.
In the server, you use the listening socket to receive data. It is only used to accept new connections.
更改为此:
conn,addr =s.accept()
data=conn.recv(100000) # Read from newly accepted socket
conn.close()
s.close()
这篇关于服务器客户端通信Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!