Server Client Communication Python

与世无争的帅哥 提交于 2019-12-13 13:27:57

问题


Server

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

CLIENT

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

CLIENT Side

Traceback (most recent call last): File "Client.py", line 21, in s.send(sys.argv[1]) TypeError: 'str' does not support the buffer interface

Server Side

File "Listener.py", line 23, in data=s.recv(100000) socket.error: [Errno 10057] A request to send or receive data was disallowed bec ause the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied


回答1:


In the server, you use the listening socket to receive data. It is only used to accept new connections.

change to this:

conn,addr =s.accept()

data=conn.recv(100000)  # Read from newly accepted socket

conn.close()
s.close()



回答2:


Your line s.send is expecting to receive a stream object. You are giving it a string. Wrap your string with BytesIO.




回答3:


Which version of Python are you using? From the error message, I guess you are unintentionally using Python3. You could try your program with Python2 and it should be fine.




回答4:


try to change the client socket to:

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)


来源:https://stackoverflow.com/questions/10005851/server-client-communication-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!