simple udp server/client - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: simple udp server/client (/thread-22735.html) |
simple udp server/client - cardmaker - Nov-24-2019 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? RE: simple udp server/client - cardmaker - Nov-25-2019 Doing some search i see if using multiprocess global variable are not shared between process, anyone can point me an example how do it? RE: simple udp server/client - micseydel - Nov-26-2019 (Nov-25-2019, 11:01 AM)cardmaker Wrote: Doing some search i see if using multiprocess global variable are not shared between processThat'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. |