Python Forum
simple udp server/client
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple udp server/client
#1
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?
Reply
#2
Doing some search i see if using multiprocess global variable are not shared between process, anyone can point me an example how do it?
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,274 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,421 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,359 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,750 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,755 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,191 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,311 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,334 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 Jun-02-2020, 01:08 PM
Last Post: nuffink

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020