0

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

fpga
  • 11
  • 1
  • 4
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 21 '21 at 16:27

1 Answers1

0

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
Jorge Omar Medra
  • 978
  • 1
  • 9
  • 19
  • Thank you for your answer, I think I am not clear in the clarification of the question. In fact, even we change the order of client's commands ABC DEF or DEF ABC, the server must send the repond [OK] good [OK] good – fpga Oct 22 '21 at 09:11
  • in C, I used char recv_buffer [1024] to store all the commands of the client, and if the server received ABC DEF or even DEF ABC, it can respond OK OK for two cases. But I don't know how to do it in Python. Any help would be appreciated – fpga Oct 22 '21 at 09:33
  • Try to change your ifs in the server. From this `, if “ABC” == data:` to this `if “ABC” == data or “DEF” == data:` – Jorge Omar Medra Oct 22 '21 at 19:45
  • Thank you for your comments, I think i will switch to use the char buffer to solve this problem, but until now I don't know how. The link below give me the idea but it seems that the char buffer in python is overwiden. https://stackoverflow.com/questions/34675555/python-char-array-declaration – fpga Oct 25 '21 at 12:15