1

Can both a client and a server be run in the same program at the same time in Python. I want to connect a client to an external server and a server to receive messages from that external server at the same time. Whenever my server receives message from that external server my client should send messages to that external server accordingly.

Following is the way I tried to achieve that ( Just the connecting part)

import select
import socket


host = 'localhost'
portClient = 6000
portServer = 7000
backlog = 5
size = 1024

client = socket.socket()
server = socket.socket()

client.connect((host,portClient))
client.send('#JOIN')

server.bind((host,portServer))
server.listen(backlog)

running = 1

while running:
    c,address = server.accept()
    c.close()


client.close()
server.close()

When I run this code, no response from the external server comes. When the while loop is omitted. I get an error saying that our server has actively refused to accept the external server.

Can I achieve this by using Python select module or Threading module? Or is there a better way?

1 Answer 1

2

TCP socket is a bi-directional stream of bytes. You can, and should, do all your communication with the server over the same single socket.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide a small example of code how a server could send a message before it receives a message from a client?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.