Search code examples
pythonsocketsservertcpbuffer

Python socket server read multi command of client


I created a socket server to read the commands from a socket client. In client side, I send ABC and then DEF, in server side, each time I received ABC or DEF from client, the server will send back to client OK.

Server

import socket               
import sys
host = socket.gethostname() 
port = 12345                

server_tcp = socket.socket()         
server_tcp.bind((host, port))        
server_tcp.listen(5)                 

while True:
   c, addr = server_tcp.accept()     

   data = c.recv(1024)
   print ('data received: %s') % data
   if 'ABC' == data:
       print ('sending back ok to the client')
       texte = 'OK';
       n=c.send(texte)
   else:
       print ('I did not get the right command ABC')
       break
   data = c.recv(1024)
   print ('data received: %s') % data
   if 'DEF' == data:
       print ('sending back ok to the client')
       texte = 'OK';
       n=c.send(texte)
   else:
       print ('I did not get the right command DEF')
       break
  c.close()

Socket client:

import socket
import sys

host = socket.gethostname() 
port = 12345                

client_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    rc  = client_tcp.connect((host, port))
except:
    print('Server not found')
texte = 'ABC';
n=client_tcp.send(texte)
data=client_tcp.recv(1024);
print (data)
if 'OK' == data:
    print('good')
else:
    print('bad')
texte = 'DEF';
n=client_tcp.send(texte)
data=client_tcp.recv(1024);
  print (data)
if 'OK' == data:
    print('good')
else:
    print('bad')
client_tcp.close()                     # Close the socket when done

When I set the command in client with order ABC - DEF I receive OK - OK in server. But with DEF - ABC, I just only received only one OK. Best regards


Solution

  • I made some changes to your code to test it. The problem is that you are not sending the response that the client is waiting for. It happens when the wrong command arrives.

    if your client is waiting for information YOUR SERVER MUST SENDS INFORMATION!... and it's the same for the other side (Server).

    In the end, your problem is an issue of protocol. You must design what kind of message will be changed between different parts and be sure that those messages are sent and received

    Server:

    import socket               
    import sys
    host = socket.gethostname() 
    port = 9966                
    
    server_tcp = socket.socket()         
    server_tcp.bind((host, port))        
    server_tcp.listen(5)                 
    
    n = 0
    
    while n < 2:
    
        c, addr = server_tcp.accept()     
        
        inData = c.recv(1024)
        data = inData.decode()
    
        texte = '';
    
        print ('data received: {0}'.format(data))
        if 'ABC' == data:
            print ('sending back ok to the client')
            texte = 'OK';        
        else:
            print ('I did not get the right command ABC')
            texte = 'FAIL';
            #break
        print("Respose: {0}".format(texte))
        #ALWASY SEND THE RESPONSE IF YOUR CLIENT WAITS FOR IT
        c.sendall(texte.encode(encoding = 'UTF-8'))
        
    
        inData = c.recv(1024)
        data = inData.decode()
        print ('data received: {0}'.format(data))
        if 'DEF' == data:
            print ('sending back ok to the client')
            texte = 'OK';
            #n=c.send(texte.encode(encoding = 'UTF-8'))
        else:
            print ('I did not get the right command DEF')
            texte = 'FAIL';
            #break
        print("Respose: {0}".format(texte))
        #ALWASY SEND THE RESPONSE IF YOUR CLIENT WAITS FOR IT
        c.sendall(texte.encode(encoding = 'UTF-8'))
    
        print ('Closing Socket Client')
        c.close()
        n += 1
    

    Client:

    
    import socket
    import sys
    
    host = socket.gethostname() 
    port = 9966                
    
    client_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        rc  = client_tcp.connect((host, port))
    except:
        print('Server not found')
    
    #texte = "ABC"
    texte = "DEF"
    n=client_tcp.send(texte.encode(encoding = 'UTF-8'))
    inData=client_tcp.recv(1024)
    data = inData.decode()
    print ("[" + data + "]")
    if 'OK' == data:
        print('good')
    else:
        print('bad')
    
    
    #texte = "DEF"
    texte = "ABC"
    n=client_tcp.send(texte.encode(encoding = 'UTF-8'))
    inData=client_tcp.recv(1024)
    data = inData.decode()
    print ("[" + data + "]") 
    
    if 'OK' == data:
        print('good')
    else:
        print('bad')
    client_tcp.close() 
    

    Client's output Order ABC DEF:

    [OK]
    good
    [OK]
    good
    

    Client's output Order DEF ABC:

    [FAIL]
    bad
    [FAIL]
    bad