微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python套接字的奇怪问题

如何解决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 举报,一经查实,本站将立刻删除。