0

I try to make a two udp listening servers in one python code code is below,

import threading
import time
import socket

class udpreceive:
      def __init__(self,port,ip):
           self.port = port
           self.ip = ip
           self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
           self.sock.bind((self.ip, self.port))

      def startserver(self):
           while True:
           time.sleep(1)
           data, addr = self.sock.recvfrom(1024)
           print (data)

server1 = udpreceive(514,"192.168.1.5")
s1 =  threading.Thread(target=server1.startserver())

server2 = udpreceive(515,"192.168.1.5")
s2 =  threading.Thread(target=server2.startserver())

s2.start()
s1.start()

this is client code for udp sender 1

import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
counter =0
while True:
   send_data = "hellow world server1 :- "+ str(counter)
   sock.sendto(send_data.encode(), ("192.168.1.5", 514))
   print (send_data)
   counter +=1

this is client code for sender 2

import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
counter =0
while True:
   send_data = "hellow world server2 :- "+ str(counter)
   sock.sendto(send_data.encode(), ("192.168.1.5", 514))
   print (send_data)
   counter +=1

When i run receive code its only show sender 1 data. start only sender 2 and start receive code but its not show any sender 2 sending data but i start sender 1 receive code show sender 1 data.

what is the wrong thing i do? i need to show both sender 1 and sender 2 data in receive program

im new to oop and socket programming thanks

  • It's UDP, you will need just one receiver for both senders. You might want to change the message from one of them. At the moment they are the same and the message might be hard to identify. – Klaus D. Mar 11 '19 at 10:52
  • Oh my bad im forgot to change it in this. but my code have hellow world server 2 in code 2 and hellow world server 1 in code one. I edit it thanks i run only sender 2 and start listener. but sender 2 details are not capturing :( – wisnshaftler Mar 11 '19 at 11:04

1 Answers1

2

In threading module, "target" keyword argument should not contain brackets, see How NOT to wait for a thread to finish in Python . As it should be:

threading.Thread(target=server1.startserver)

Then, the two UDP server threads shall start then join, as fairly sharing CPU resources is important (especially when using infinite loops).

The code for the server could be:

import threading
import time
import socket

class udpreceive:
     def __init__(self,port,ip):
          self.port = port
          self.ip = ip
          self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
          self.sock.bind((self.ip, self.port))

     def startserver(self):
          while True:
               data, addr = self.sock.recvfrom(1024)
               print(f'port {self.port} receives {data}')

s1 = udpreceive(514, "192.168.1.5")
s2 = udpreceive(515, "192.168.1.5")
threads = [threading.Thread(target=s1.startserver), threading.Thread(target=s2.startserver)]

for th in threads:
     th.start()
     print(f'threads {th} started')
     th.join(0.1)

Meanwhile, you may need to change the code in sender 2 to:

sock.sendto(send_data.encode(), ("192.168.1.5", 515)) # Send to port 515, not 514