TCP客户端如何连接到服务器并发送到服务器端?获取错误且无法成功 运行 代码。

How do you connect the TCP client to the server and send to the server side? Getting errors and cannot successfully run code.

我正在学习 TCP,并且正在尝试一个 client/server 程序。我的服务器端可以正常工作,但是在我的客户端,我无法成功连接到服务器端。

我客户端的错误代码

第一个错误:

回溯(最近调用最后): 文件 "tcpclient.py",第 13 行,位于 clientSocket.connect((服务器名称, 服务器端口)) 类型错误:应为 str、bytes 或 bytearray,而不是 list

第二个错误:

回溯(最近调用最后): 文件 "tcpclient.py",第 34 行,位于 clientSocket.send(clientSentence.encode(), 服务器地址) 类型错误:需要一个整数(得到类型元组)

服务器代码: 此代码运行成功。

import sys
from socket import*

serverSocket = socket(AF_INET, SOCK_STREAM)
serversys = sys.argv

if len(serversys) == 1: 
    serverPort = 9999
else:
    serverPort = serversys[1]
    serverPort = int(serverPort)

serverSocket.bind(('', serverPort)) 
serverSocket.listen(1)
print ("The server is ready to receive.")

while True:
    connectionSocket, addr = serverSocket.accept()
    print ("Connection accepted from: ", addr)

    clientSentence = connectionSocket.recv(1024).decode()


    clientSentence, clientAddress = serverSockset.recv(1024)
    clientSentence.decode()
    connectionSocket.close()

    if clientSentence[0].decode() == 'U':
        serverSentence = clientSentence.upper()
        connectionSocket.send(serverSentence[1:].encode(), clientAddress)
        connectionSocket.close()
    elif clientSentence[0].decode() == 'L':
        serverSentence = clientSentence.lower()
        connectionSocket.send(serverSentence[1:].encode(), clientAddress)
        connectionSocket.close()
    elif clientSentence[0].decode() == 'I':
        serverSentence = clientSentence.title()          
        connectionSocket.send(serverSentence[1:].encode(), clientAddress)
        connectionSocket.close()

客户端:这就是两个错误的地方。

import sys
from socket import*

clientSocket = socket(AF_INET, SOCK_STREAM)

clientsys = sys.argv
serverName = clientsys[1:]
serverPort = clientsys[2:]

clientSocket.connect((serverName, serverPort)) #First Error

serverPort = [int(serverPort) for serverPort in serverPort]
serverAddress = (serverName, serverPort)

sentenceChoice = input("Enter U for UPPERCASE SENTENCE, L for lowercase sentence, \nor I for Initial Caps Sentence: ")
sentence = input("Enter sentence: ")

clientSentence = sentenceChoice+sentence

clientSocket.send(clientSentence.encode(), serverAddress) #Second Error
serverSentence = clientSocket.recv(1024)
print('From Server: ', serverSentence.decode())
clientSocket.close()

您确定它应该是 clientsys[2:] 而不是 clientsys[2] 吗?

发生错误的原因是您正在使用列表切片从 sys.argv:

中查找服务器和端口
clientsys[1:]
clientsys[2:]

列表切片操作的结果始终是一个列表:

l = [1, 2, 3]
>>>l[1:]
[2, 3]
>>>print(l[2:])
>>>[3]

改为使用索引:

serverName = clientsys[0]
serverPort = clientsys[1]

您的第一个错误是由于您使用列表切片作为服务器名称和端口造成的。更改为:

serverName = clientsys[1]
serverPort = clientsys[2]

你的第二个错误是因为 socket.send() 不接受 serverAddress 元组参数(你在哪里想到 socket.sendto()?无论哪种方式,你都可以通过简单地删除参数来修复:

clientSocket.send(clientSentence.encode())

Link 到 socket.send 文档:https://docs.python.org/3/library/socket.html#socket.socket.send