Python Forum

Full Version: simple udp server/client
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, im trying to make i simple udp server client (two in one).
the client send every 5 second a string to a server(port 23000) and save the respone in "reply = msgFromServer[0]".
in simultane the server wait if a client send a string and reply the "reply = msgFromServer[0]".

import binascii
import re
import socket
import sys
import logging
import os
import time
from multiprocessing import Process

localIP = "0.0.0.0"
localPort = 5009

serverAddressPort   = ("192.168.0.107", 23000)

MESSAGE = "reply1"
bufferSize  = 108
msgFromServer       = "Hello UDP Client"
bytesToSend         = str.encode(msgFromServer)

def start_sender():
    global reply
    UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

    while(True):
        UDPClientSocket.sendto(bytesToSend, serverAddressPort)
        time.sleep(5)

    while(True):
        msgFromServer = UDPClientSocket.recvfrom(bufferSize)
        reply = msgFromServer[0]




def start_listener():
    global reply
    UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
    UDPServerSocket.bind((localIP, localPort))

    while(True):
        bytesAddressPair = UDPServerSocket.recvfrom(108)
        message = bytesAddressPair[0]
        address = bytesAddressPair[1]
        UDPServerSocket.sendto(reply, address)#here i reply the bytes from the sender msgFromServer[0] but nothing is sent


if __name__ == '__main__':
    try:
        p1 = Process(target = start_listener)
        p1.start()
        p2 = Process(target = start_sender)
        p2.start()
    except Exception as e:
        print(e)
        sys.exit()
but nothing is replaying when i client send a string for the listener on port 5009
anyone can help?
Doing some search i see if using multiprocess global variable are not shared between process, anyone can point me an example how do it?
(Nov-25-2019, 11:01 AM)cardmaker Wrote: [ -> ]Doing some search i see if using multiprocess global variable are not shared between process
That's right, they're entirely separate processes so the variables are totally different objects.

(Nov-25-2019, 11:01 AM)cardmaker Wrote: [ -> ]anyone can point me an example how do it?
I Googled "python share variable between processes" and that looked promising. Feel free to ask back with specific questions / concerns.