Search code examples
pythonudpclient-servermulticast

Client-Server python : The received message can't be printed individually


I am pretty new to all the network programming. I have some hardware where I normally get 2 bundled messages through a UDP stream (hence I wrote a fake server to simulate the hardware)

I have created a simple client-server where server sends 2 messages and client picks them up. My issue is when I print out the data that I receive from the server, it prints out as 2 messages but there is no way to pick up only one of the messages. I tried everything I can think of. I feel like the answer is very simple.

For example:

print(self.data) outputs:

b'#message1,765,0,77.5,allgood,2096,183412.000' b'#message2,654,0,76.5,allgood,2096,183411.000'

print(self.data.decode().split(',')[0]) outputs:

#message1

#message2

what I want output to be:

#message1

So how do I only pick #message1? Any help would be much appreciated. I am pasting my code down below:

fakeServer.py:

import socket
import time
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE1 = "#message1,765,0,77.5,allgood,2096,183412.000"
MESSAGE2 = "#message2,654,0,76.5,allgood,2096,183411.000"

print ("UDP target IP:", UDP_IP)
print ("UDP target port:", UDP_PORT)
print ("message1:", MESSAGE1)
print ("message2:", MESSAGE2)

while True:
    sock = socket.socket(socket.AF_INET, # Internet
                    socket.SOCK_DGRAM) # UDP
    sock.sendto(MESSAGE1.encode('utf-8'), (UDP_IP, UDP_PORT))
    sock.sendto(MESSAGE2.encode('utf-8'), (UDP_IP, UDP_PORT))
    time.sleep(3)

fakeClient.py

import socket
from tspiMessages import *
import io


class Client():
    def __init__(self):
        self.client()
    def client(self):
        server_address = ('', 5005)

        sock = socket.socket(socket.AF_INET, # Internet
                        socket.SOCK_DGRAM) # UDP
        sock.bind(server_address)
        try:
            while True:
                self.data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
                self.data1 = self.data.decode().split(',')#[0]
                print(self.data)

        finally:
            sock.close()


if __name__ == '__main__':
    client = Client()


Solution

  • Instant consecutive printouts look confusing but your code is working fine.

    You just need to separate your data processing logic by determining the type of data you receive. Whatever logic you put in the while loop will run for each message from the socket.

    self.data, addr = sock.recvfrom(1024)
    print("==== NEW DATA RECEIVED ====")
    if "message1" in self.data:
      print("DATA TYPE IS message1")
      print("======== START message1 DATA ====")
      print(self.data)
      print("======== END message1 DATA ====")
    elif "message2" in self.data:
      print("DATA TYPE IS message2")
      print("======== START message2 DATA ====")
      print(self.data)
      print("======== END message2 DATA ====")
    else:
      print("DATA TYPE IS UNKNOWN")
      print("======== START UNKNOWN DATA ====")
      print(self.data)
      print("======== END UNKNOWN DATA ====")
    print("==== DATA PROCESSING ENDS ====")
    

    Try this in fakeClient.py, it will make sense.