I have written this two .py code to communicate between each outher .
A.py listens to port 8888 and sends data to 7777
B.py listens to port 7777 and sends data to 8888
Both of these client part stuck in an infinite loop after starting their server.
where is the problem ??
If I use only server in A.py and client in B.py (and vice versa ) without any threading they works fine.
A.py:
import socket
import threading
import thread
import time
class server(threading.Thread):
s = ''
host = 0
port = 0
def __init__(self):
threading.Thread.__init__(self)
global s,host,port
s = socket.socket()
host = socket.gethostname()
port = 8888
def run(self):
global s,host,port
print 'Server started!'
print 'Waiting for clients...'
s.bind((host, port))
s.listen(5)
c, addr = s.accept()
print 'Got connection from', addr
while True:
time.sleep(2)
msg = c.recv(1024)
if len(msg)==0 : break
print addr, ' >> ', msg
class client(threading.Thread):
s = ''
host = 0
port = 0
def __init__(self):
threading.Thread.__init__(self)
global s,host,port
s = socket.socket()
host = socket.gethostname()
port = 7777
def run(self):
try:
time.sleep(5)
global s,host,port
print 'Connecting to ', host, port
s.connect((host, port))
print "Connectd"
while True:
time.sleep(2)
msg = raw_input('CLIENT >> ')
if len(msg)==0:break
s.send(msg)
except:
print "Waiting"
self.run()
thread1 = server()
thread2 = client();
thread1.start()
thread2.start()
thread1.join()
thread2.join();
B.py:
import socket
import threading
import thread
import time
class server(threading.Thread):
s = ''
host = 0
port = 0
def __init__(self):
threading.Thread.__init__(self)
global s,host,port
s = socket.socket()
host = socket.gethostname()
port = 7777
def run(self):
global s,host,port
print 'Server started!'
print 'Waiting for clients...'
s.bind((host, port))
s.listen(5)
c, addr = s.accept()
print 'Got connection from', addr
while True:
time.sleep(2)
msg = c.recv(1024)
if len(msg)==0 : break
print addr, ' >> ', msg
class client(threading.Thread):
s = ''
host = 0
port = 0
def __init__(self):
threading.Thread.__init__(self)
global s,host,port
s = socket.socket()
host = socket.gethostname()
port = 8888
def run(self):
try:
time.sleep(5)
global s,host,port
print 'Connecting to ', host, port
s.connect((host, port))
print "connected"
while True:
time.sleep(2)
msg = raw_input('CLIENT >> ')
if len(msg)==0:break
s.send(msg)
except:
print "waiting"
self.run();
thread1 = server()
thread2 = client();
thread1.start()
thread2.start()
thread1.join()
thread2.join();
Using
global s, host, portis the cause of the problem. In A.py,for instance, the server and client classes are both changing the
same variables
s,hostandport. By changing the port to be the same value, you are either messing up either the server or the client (whichever runs first).Never use
globalif you don’t have to, and you very rarely have to.In this case, your problem is fixed by using instance attributes.
client.runmethod without recursivecalls to
self.run(). Python has a limit to how many recursive callsyou can make, and if the client has to wait too long, a recursive
call here could cause your program to fail. Instead, you could use a
whileloop. (See below).Run it with