简体   繁体   中英

Python TCP/IP socket - Sending and receiving messages in order

Happy new year everyone!

I have created a socket TCP/IP using Python. In client side, I send the text "A" and "B", and in the server side, if I receive the correct text (A & B), I will send back to client a reply: "GOOD". Everything is good until I change the order of the text to "B" and "A".

How can I improve the server side to receive the messages and don't care about the order AB or BA.

Server:

import socket

HOST = 'localhost'
PORT = 8019
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1) # Number of connections

# Server loop
while True:
    # Accept connections
    client, address = s.accept()
    print("Connected to", address)

    # Receive data and decode using utf-8
    data = client.recv( 1024 ).decode( 'utf-8' )
    print("Received :", repr(data))

    # Send data to client in utf-8
    if ("A" == data):
        reply = "GOOD"
        client.send(reply.encode('utf-8'))
    else:
        reply = "FAIL"
        client.send(reply.encode('utf-8'))
    # Receive data and decode using utf-8
    data = client.recv( 1024 ).decode( 'utf-8' )
    print("Received :", repr(data))

    # Send data to client in utf-8
    if ("B" == data):
        reply = "GOOD"
        client.send(reply.encode('utf-8'))
    else:
        reply = "FAIL"
        client.send(reply.encode('utf-8'))
    s.close()
    break

Client:

import socket

# Create socket and connect it to server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost',8019))

# client loop
while True:
    message = "A"
    s.send(message.encode('utf-8'))
    print("Awaiting the reply...")
    reply = s.recv( 1024 ).decode( 'utf-8' )
    print("Received ", str(reply))
    
    message = "B"
    s.send(message.encode('utf-8'))
    print("Awaiting the reply...")
    reply = s.recv( 1024 ).decode( 'utf-8' )
    print("Received ", str(reply))
    s.close()
    break

Any help would be greatly appreciated! Best regards!

I think I could do something like this:

# Receive data and decode using utf-8
    data = client.recv( 1024 ).decode( 'utf-8' )
    print("Received :", repr(data))

    # Send data to client in utf-8
    if ("A" == data):
        reply = "GOOD"
        client.send(reply.encode('utf-8'))
    elif ("B" == data):
        reply = "GOOD"
        client.send(reply.encode('utf-8'))
    else:
        reply = "FAIL"
        client.send(reply.encode('utf-8'))


    # Receive data and decode using utf-8
    data = client.recv( 1024 ).decode( 'utf-8' )
    print("Received :", repr(data))

    # Send data to client in utf-8
    if ("A" == data):
        reply = "GOOD"
        client.send(reply.encode('utf-8'))
    elif ("B" == data):
        reply = "GOOD"
        client.send(reply.encode('utf-8'))
    else:
        reply = "FAIL"
        client.send(reply.encode('utf-8'))
    s.close()
    break

But I am still waiting for the better answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM