0

I am facing a strange problem using a simple python client/server example. After having established the connection for the first time, if I tried to send the message "get id", I don't get any answer from my server and client side is stuck (I am developping an application to communicate with a measurement system via ethernet instead of RS232 and "get id" is one of the command we use). However if I start by sending "a" or "1" or even "hello world", the server answers and then if I send "get id" it works !

Now if I run the server code and use Putty as client, I can send "get id" as a first message and the server answers without any problem !

I would like to understand what is going on...

Here is the client code

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')

And server code

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, wlist, 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))

Thanks for your help !

Gregos38
  • 53
  • 1
  • 3
  • 1
    Example output would be useful. I can't reproduce this issue; my first `get id` comes back with `received message: 5/5` – erik258 Nov 03 '20 at 15:17
  • 1
    You can't rely on matching up sends and recvs. If you send "1\r" and then you send "get id\r", the server might receive for example "1" and then "\rget" and then " id\r" – user253751 Nov 03 '20 at 15:48
  • https://stackoverflow.com/a/43420503/238704 – President James K. Polk Nov 03 '20 at 19:22
  • @DanielFarrell: I don't have any output because my client is waiting and the server never send an answer. – Gregos38 Nov 04 '20 at 10:12
  • @user253751: ok but why it only happens for this specific "get id\r". It seems the server doesn't answer only in this case. – Gregos38 Nov 04 '20 at 10:15
  • @DanielFarrell: to be more precise, when I send "get id\r" as a first message, the server part after the `else:` is not executed. – Gregos38 Nov 04 '20 at 10:43

0 Answers0