multithreading to run UDP client and UDP server in the same program
<p>I am trying to write a code in python, using multithreading, that runs UDP server side and UDP client side in a single program file. I need to make sure that the threads are synchronized.</p>

<p>The problem (as far as i have understood) with my code is that when thread1 runs, it acquires the lock since thread1's run() method runs the serverSide() method which contains a forever while loop, thread1 does not release the lock and therefore, the program gets stuck.</p>

<p>Can anyone please help me synchronize the threads while making sure the server and client run properly</p>

<pre><code>import threading
import time
import sys
from datetime import datetime
from socket import *


class myThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name
        # Get lock to synchronize threads
        threadLock.acquire()
        serverSide()
        # Free lock to release next thread
        threadLock.release()

class myThread1 (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name
        # Get lock to synchronize threads
        threadLock.acquire()
        clientSide()
        # Free lock to release next thread
        threadLock.release()



def serverSide():
    serverPort = 44000
    serverIP = '192.168.0.0'
    serverSocket = socket(AF_INET, SOCK_DGRAM)
    serverSocket.bind((serverIP,serverPort))
    print ("SERVER HERE!\nThe server is ready to receive")
    while 1:
        message, clientAddress = serverSocket.recvfrom(2048)
        modifiedMessage = message.upper()
        serverSocket.sendto(modifiedMessage, clientAddress)


def clientSide():
    serverIP = "192.168.0.0"
    serverPort = 44000
    clientSocket = socket(AF_INET, SOCK_DGRAM)
    message = raw_input("CLIENT HERE!\nInput lowercase sentence:")

    clientSocket.sendto(message.encode(),(serverIP, serverPort))
    modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
    print(modifiedMessage) # print the received message

    clientSocket.close() # Close the socket

threadLock = threading.Lock()
threads = []

# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread1(2, "Thread-2", 2)

# Start new Threads
thread1.start()
thread2.start()

# Add threads to thread list
threads.append(thread1)
threads.append(thread2)

# Wait for all threads to complete
for t in threads:
    t.join()
print "Exiting Main Thread"
</code></pre>