Python套接字的奇怪问题

问题描述

使用简单的python客户端/服务器示例,我面临一个奇怪的问题。 首次建立连接后,如果我尝试发送消息“获取ID”,则服务器不会收到任何答复,并且客户端卡住了(我正在开发一个应用程序,通过它与测量系统进行通信以太网而不是RS232,“ get id”是我们使用的命令之一)。 但是,如果我首先发送“ a”或“ 1”,甚至是“ hello world”,则服务器会回答,然后如果我发送“ get id”,它将起作用!

现在,如果我运行服务器代码并使用Putty作为客户端,则可以发送“ get id”作为第一条消息,服务器可以毫无问题地回答!

我想了解发生了什么...

这是客户端代码

import socket

host = '192.168.0.102'
port = 12800

connection_with_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

connection_with_server.connect((host,port))
print("Connexion established on port {}".format(port))
msg = '1'

while msg != b'stopall\r':
    msg = input('> ')
    msg = msg+'\r'
    msg = msg.encode('utf-8')
    connection_with_server.send(msg)
    received_message = connection_with_server.recv(1024)
    print('received message: ',received_message.decode())

print('connection closed')

和服务器代码

import socket
import select

def openEthernetConnection(host,port):
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        s.bind((host,port))
        s.listen(1)
        print("Server is now listening on port {}".format(port))
        return(s)
        
if __name__ == '__main__':
    
    protocole = 'ethernet'
    encode_format = 'utf-8'
       
    if protocole == 'ethernet':
        # open ethernet connection
        host = ''
        port = 12800
        mysocket = openEthernetConnection(host,port)
        clients_connected = []        

    while True:
    
        if protocole == 'ethernet':
            # Check for new clients
            new_connections,wlist,xlist = select.select([mysocket],[],0.05)
            for connection in new_connections:
                connexion_with_client,infos_connexion = connection.accept()
                # On ajoute le socket connecté à la liste des clients
                clients_connected.append(connexion_with_client)
            # Listen the list of connected clients
            # select return clients who must be read with recv
            # wait 50ms
            # select.select in a try loop if we want to raise an exception if no clients are connected
            clients_to_read = []
            try:
                clients_to_read,xlist = select.select(clients_connected,0.05)
            except select.error: 
                pass
            else:
                command = []
                # Go through the clients list to be read
                for client in clients_to_read:
                    received_command = client.recv(1024)
                    try:
                        received_command = received_command.decode(encode_format)
                        command = received_command.split()
                        print('command: ',command)
                    except UnicodeDecodeError:
                        return_message = 'E010'
                        pass
       
                for client in clients_to_read:
                    return_message = '5/5'
                    client.sendall(return_message.encode(encode_format))

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...