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

  • Home
  • SEARCH
  • 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 6848117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:49:43+00:00 2026-05-27T00:49:43+00:00

I am trying to write a program that works as an intermedium. (M) I

  • 0

I am trying to write a program that works as an intermedium. (M)

I can only use telnet to connect :

A needs to connect to M, B connects to M.

A sends data to M on a socket, M needs to pass it to B

B sends data to M on another socket

I tried this by starting four threads with a shared list

The problem is it seems it is not writing to the other socket, or even accepting writing.

Does anyone know a better way to implement this and pass it through to another socket

My code :

import sys
import arduinoReadThread
import arduinoWriteThread
import socket
class ControllerClass(object):
    '''
    classdocs
    '''
    bolt = 0
    socketArray=list()

    def __init__(self):
        self.readAndParseArgv()
        self.createThreads()

    def readAndParseArgv(self):
        array = sys.argv
        print sys.argv
        if len(array) != 3:
            print "Too few arguments : ./script host:port host:port"
        else:
            for line in array:
                if ":" in line:
                    splitted = line.split(':')
                    HOST = splitted[0]
                    print HOST
                    PORT = int(splitted[1])
                    print PORT
                    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
                    s.bind((HOST,PORT)) #bind to that port
                    print "test"
                    s.listen(1) #listen for user input and accept 1 connection at a time.
                    self.socketArray.append(s)

    def createThreads(self):
        print "Creating Threads"
        sharedArray1 = list()
        sharedArray2 = list()
        s1 = self.socketArray.pop()
        s2 = self.socketArray.pop()
        sT1 = arduinoWriteThread.writeThread().run(self.bolt,sharedArray1,s2)
        sT2 = arduinoReadThread.readThread().run(self.bolt,sharedArray1,s1)
        sT3 = arduinoReadThread.readThread().run(self.bolt,sharedArray2,s2)
        sT4 = arduinoWriteThread.writeThread().run(self.bolt,sharedArray2,s1)
        sT1.start()
        sT2.start()
        sT3.start()
        sT4.start()
x = ControllerClass()
x

Two Threads :
Write Thread :

import threading
class writeThread ( threading.Thread ):

    def run ( self,bolt,writeList,sockeToWriteTo ):
        s = sockeToWriteTo
        while(bolt == 0):
            conn, addr = s.accept()
            if len(writeList) > 0:

                socket.send(writeList.pop(0))

Read Thread

import threading
    class readThread ( threading.Thread ):

    def run ( self,bolt,writeList,socketToReadFrom ):
        s = socketToReadFrom
        while(bolt == 0):
            conn, addr = s.accept()

            f = conn.rcv()
            print f
            writeList.append(f)
  • 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-27T00:49:44+00:00Added an answer on May 27, 2026 at 12:49 am

    You don’t really need threads for this…

    When a new connection is accepted, add it to a list. When receiving anything from one of the connection in the list, send to all connections except the one you got the message from.

    Use select to see which connections have send data to you.

    Edit

    Example using select:

    # serversocket: One server socket listening on some port, has to be non-blocking
    # all_sockets : List containing all connected client sockets
    while True:
        readset = [serversocket]
        readset += all_sockets
    
        # Wait for sockets to be ready, with a 0.1 second timeout
        read_ready = select.select(readset, None, None, 0.1)
    
        # If the listening socket can be read, it means it has a new connection
        if serversocket in read_ready:
            new_connection = serversocket.accept()
            new_connection.setblocking(0);  # Make socket non-blocking
            all_sockets += [new_connection]
            read_ready.remove(serversocket) # To not loop over it below
    
        for socket in read_ready:
            # Read data from socket
            data = socket.recv(2048)
            for s in all_sockets:
                # Do not send to self
                if s != socket:
                    s.send(data)
    

    Disclaimer I have never really used the Python socket functions, the code above was made from reading the manual pages just now. The code is probably not optimal or very Pythonic either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that outputs data that can be served over
I'm trying to write a program that uses sockets to connect with other instances
I'm trying to write a program that can stop and start services using SilverLight
I'm trying to write a program, that works in console or GUI mode, depending
I am trying to write a C++ program that works like the game 24.
I'm trying to write a program that will fetch data from a SQL Server
I'm trying to write a program that reads text from external file (string string
I am trying to write a program that allows a binary to be run,
I am trying to write a program that displays the integers between 1 and
i am trying to write a program that close explorer then runs another program.

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.