Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:00:01+00:00 2026-05-29T08:00:01+00:00

I have written this two .py code to communicate between each outher . A.py

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:00:01+00:00Added an answer on May 29, 2026 at 8:00 am
    • Using global s, host, port is the cause of the problem. In A.py,
      for instance, the server and client classes are both changing the
      same variables s, host and port. 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 global if you don’t have to, and you very rarely have to.
      In this case, your problem is fixed by using instance attributes.

    • Also, I suggest writing the client.run method without recursive
      calls to self.run(). Python has a limit to how many recursive calls
      you 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
      while loop. (See below).

    import argparse
    import socket    
    import threading
    import thread
    import time   
    
    class server(threading.Thread):
        def __init__(self, port):
            threading.Thread.__init__(self)
            self.s = socket.socket()        
            self.host = socket.gethostname() 
            self.port = port
    
        def run(self):
            print 'Server started!'
            print 'Waiting for clients...'
    
            self.s.bind((self.host, self.port))       
            self.s.listen(5)                 
            c, addr = self.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):
        def __init__(self, port):
            threading.Thread.__init__(self)
            self.s = socket.socket()         
            self.host = socket.gethostname() 
            self.port = port
    
        def run(self):
            while True:
                time.sleep(5)
                print 'Connecting to ', self.host, self.port
                try:
                    self.s.connect((self.host, self.port))
                    break
                except Exception as err:
                    print "Waiting", err
            print "Connectd"
            while True:
                time.sleep(2)
                msg = raw_input('CLIENT >> ')
                if len(msg) == 0:break
                self.s.send(msg)
    
    if __name__ == '__main__':
        parser = argparse.ArgumentParser()
        parser.add_argument('--server_port', type = int, default = 8888)
        parser.add_argument('--client_port', type = int, default = 7777)
        args = parser.parse_args()
    
        thread1 = server(args.server_port)
        thread2 = client(args.client_port)
    
        thread1.start()
        thread2.start()
    
        thread1.join()
        thread2.join()
    

    Run it with

    test.py --server 8888 --client 7777
    test.py --server 7777 --client 8888
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this innocent javascript code, which lets the user create two markers
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have written this piece of code public class Test{ public static void main(String[]
I have written this code for myself(it is not a home work) I want
I have written a code to refresh two divs at regular intervals of time.
I have written code in Java to merge two xml file, the first file
I have written some code to check two dates, a start date and an
I Have written such a code below and I will pass two lists to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.