我用 python 创建了一个客户端/服务器代码。服务器运行良好并侦听 8000 端口,但是当我通过客户端连接到它然后尝试向服务器发送消息时,出现以下错误:
Traceback (most recent call last):
File "C:\Users\milad\workspace\NetworkProgramming\client.py", line 26, in <module>
if __name__ == "__main__" : main()
File "C:\Users\milad\workspace\NetworkProgramming\client.py", line 20, in main
TcpSocket.send(sendData)
TypeError: 'str' does not support the buffer interface
我不知道如何解决有关客户端代码的问题。下面我放了客户端代码。我是用Python语言写的。
#!/usr/bin/python3
import socket
from builtins import input
def main():
serverHostNumber = input("Please enter the ip address of the server: \n")
serverPortNumber = input("Please enter the port of the server: \n")
# create a socket object
TcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connection to hostname on the port.
TcpSocket.connect((serverHostNumber, int(serverPortNumber)))
while True:
data = TcpSocket.recv(1024)
print("Server : ", data)
sendData = str(input("Client : "))
TcpSocket.send(sendData)
TcpSocket.close()
if __name__ == "__main__" : main()
TcpSocket.send(sendData)
好像send只接受bytes
实例。尝试:
TcpSocket.send(bytes(sendData, "ascii")) #... or whatever encoding is appropriate
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)