Python 套接字服务器读取客户端的多命令

Python socket server read multi command of client

我创建了套接字服务器来读取来自套接字客户端的命令。

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

套接字客户端:

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

当我在客户端中设置命令顺序为 ABC 然后是 DEF 时效果很好,但是当我更改为 DEF ABC 时,它无法正常工作。请告诉我我必须更改代码的内容。我还考虑使用 缓冲区 。有理想吗?

此致

我对您的代码进行了一些更改以对其进行测试。问题是您没有发送客户端正在等待的响应。它发生在错误的命令到达时。

如果您的客户端正在等待信息,您的服务器必须发送信息!...另一端(服务器)也是如此。

最终你的问题是协议的问题。您必须设计不同部分之间将更改哪种消息,并确保发送和接收这些消息

服务器:

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

客户:


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

客户的输出订单 ABC DEF:

[OK]
good
[OK]
good

客户的输出订单 DEF ABC:

[FAIL]
bad
[FAIL]
bad