title: How can I create a two thread one for sending video and others for receiving video at the sam time in UDP python
body: <p>Hi guy I am making a UDP protocol to create a live chat between server and client I have written a code for server to send video to client and vice versa which work fine.but right now I am trying to make it to sending and receiving at the same time I tried using two thread for both client and server but seems like I am getting this error</p>
  <blockquote>
  <p>2022-03-27 04:51:48.211 python3[6961:9402560] WARNING: nextEventMatchingMask should only be called from the Main Thread! This will throw an exception in the future.</p>
  </blockquote>
  <p>And it is not working<a href="https://i.stack.imgur.com/ZjZ54.png" rel="nofollow noreferrer"> image</a></p>
  <p>here is my code</p>
  <p>server.py</p>
  <pre><code>import numpy as np
  import cv2, imutils, socket
  import time
  import base64
  import threading
  
  
  BUFF_SIZE = 65536
  
  message = b'Hello'
  
  def create_UDP_Socket():
      global server_socket
      global host_ip
      global host_name
      global port
      
      try:
  
          server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
          server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFF_SIZE)
          host_name = socket.gethostname()
          print(host_name)
          host_ip = socket.gethostbyname(host_name)
          print(host_ip)
          port = 9998
      except socket.error as msg:
          print(f'Create socket error : {msg}')
        
  def binding_socket():
      try:
          socket_address = (host_ip, port)
          server_socket.bind(socket_address)
          print('listening at :-', socket_address)
      except socket.error as msg:
            print(f'Binding socket error : {msg}')
  
  def broadcast():
      print('Waiting for client to connect to broadcast')
      while True:
          
          vid = cv2.VideoCapture(0)
          fps, st, frames_to_count, cnt = (0, 0, 20, 0)
          # receive request from user 
          msg, client_addr = server_socket.recvfrom(BUFF_SIZE)
          print('GOT connection from ', client_addr)
          
          # Check user request
          if msg == b'request broadcast':
  
              print(f'Receveing request broadcasting from client{client_addr}')
              print(f'Broadcast video to user :{client_addr}')
              Width = 400
              while vid.isOpened():
                  _,frame = vid.read()
                  frame = imutils.resize(frame, width=Width)
                  endcoded, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 80])
                  message = base64.b64encode(buffer)
                  server_socket.sendto(message,client_addr)
              # print(f'Sending to client : {client_addr}')
                  frame = cv2.putText(frame, 'FPS: ' + str(fps), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                  cv2.imshow('Transmitting Video to Client (q:quit)',frame)
                  key = cv2.waitKey(1) &amp; 0xFF
  
                  if key == ord('q'):
                      server_socket.close()
                      break
                  if cnt == frames_to_count:
                      try:
                          fps = round(frames_to_count/(time.time()-st))
                          st=time.time()
                          cnt=0
                      except:
                          pass
                  cnt+=1
          else:
              print(f'Client at address {client_addr} wrong request')
              msg = b'wrong request'
              server_socket.sendto(msg,client_addr)
              break
  
  def receive_broadcast():
  
      # wating for client to send request
      print('waiting for client to connect')
  
      # receive connection from client
      msg, client_addr = server_socket.recvfrom(BUFF_SIZE)
      
      print(f'Connect to : {client_addr}')
      server_socket.sendto(message,client_addr)
      print('Receciving a broadcast')
        
  
      fps, st, frames_to_count, cnt = (0, 0, 20, 0)
      while True:
          
          # receiving video fromm client
          packet, _ = server_socket.recvfrom(BUFF_SIZE)
          data = base64.b64decode(packet,b' /')
          npdata = np.fromstring(data, dtype=np.uint8)
          frame = cv2.imdecode(npdata, 1)
          frame = cv2.putText(frame, 'FPS: ' + str(fps), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
          cv2.imshow(&quot;RECEIVING VIDEO from Client (q:quit)&quot;, frame)
          key = cv2.waitKey(1) &amp; 0xFF
          if key == ord('q'):
              server_socket.close()
              break
          if cnt == frames_to_count:
                  try:
                      fps = round(frames_to_count/(time.time()-st))
                      st=time.time()
                      cnt=0
                  except:
                      pass
          cnt+=1
  
  def start():
      t1 = threading.Thread(target=broadcast)
      t2 = threading.Thread(target=receive_broadcast)
      t1.start()
      t2.start()
      
  
  
  create_UDP_Socket()
  binding_socket()
  
  start()
  </code></pre>
  <p>client.py</p>
  <pre><code>import cv2, imutils, socket
  import numpy as np
  import time
  import base64
  import threading
  BUFF_SIZE = 65536
  client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFF_SIZE)
  host_name = socket.gethostname()
  host_ip =  socket.gethostbyname(host_name)
  print('Host ip: - ',host_ip)
  port = 9998
  
  
  def receive_broadcast():
      #request to connect
      message =  b'request broadcast'
      client_socket.sendto(message, (host_ip, port))
      print('test: ',message)
      print('Send Request to a server for Broadcast video')
      fps, st, frames_to_count, cnt = (0, 0, 20, 0)
      while True:
          packet, server_add = client_socket.recvfrom(BUFF_SIZE)
          if packet == b'wrong request':
              print('server reject ')
              break 
  
          #print(f&quot;connected to  server:{server_add}&quot;)
          data = base64.b64decode(packet,b' /')
          npdata = np.fromstring(data, dtype=np.uint8)
          frame = cv2.imdecode(npdata, 1)
          frame = cv2.putText(frame, 'FPS: ' + str(fps), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
          cv2.imshow(&quot;RECEIVING VIDEO from Server (q:quit)&quot;, frame)
          key = cv2.waitKey(1) &amp; 0xFF
          if key == ord('q'):
              client_socket.close()
              break
          if cnt == frames_to_count:
                  try:
                      fps = round(frames_to_count/(time.time()-st))
                      st=time.time()
                      cnt=0
                  except:
                      pass
          cnt+=1
      
  
  def broadcast():
  
      # request to connect
      message = b'request to connect to server'
      client_socket.sendto(message, (host_ip, port))
      print('test: ',message)
      print('Sending Broadcasting')
  
      while True:
          vid = cv2.VideoCapture(0)
          fps, st, frames_to_count, cnt = (0, 0, 20, 0)
          msg, client_addr = client_socket.recvfrom(BUFF_SIZE)
          print('GOT connection from ', client_addr)
          Width = 400
          while vid.isOpened():
              _,frame = vid.read()
              frame = imutils.resize(frame, width=Width)
              endcoded, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 80])
              message = base64.b64encode(buffer)
              client_socket.sendto(message,client_addr)
              frame = cv2.putText(frame, 'FPS: ' + str(fps), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
              cv2.imshow('Transmitting Video to Server (q:quit)',frame)
              key = cv2.waitKey(1) &amp; 0xFF
  
              if key == ord('q'):
                  client_socket.close()
                  break
              if cnt == frames_to_count:
                  try:
                      fps = round(frames_to_count/(time.time()-st))
                      st=time.time()
                      cnt=0
                  except:
                      pass
              cnt+=1
  
  
  def start():
  
      t1 = threading.Thread(target=broadcast)
      t2 = threading.Thread(target=receive_broadcast)
      t1.start()
      t2.start()
  
  start()
  </code></pre>