1

I'm trying to write a server for a chat program. I want the server to have a tcp connection with every chat user. Is there way for the server to have multiple tcp connections at the same time without creating a socket for every connection? And if yes, how?

1 Answer 1

2

No. Unlike UDP sockets, TCP sockets work are connection-oriented. Whatever data is written into a socket, "magically" appears to come out of the socket at the other end as a stream of data. For that, both sockets maintain a virtual connection, a state. Among other things, the state defines both endpoints of the conntection - IP and port numbers of both sockets. So a single TCP socket can only talk to a single TCP socket at the other end.

UDP sockets on the other hand operate on a per-packet basis (connectionless), allowing you to send a receive packets to/from any destination using the same socket. However, UDP does not guarantee reliability and in-order delivery.

Btw, your question has nothing to do with python. All sockets (except raw sockets) are operating system sockets that work in the same way in all languages.

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

3 Comments

You better use TCP for data as well. Implementing reliability and order in UDP is not straightforward. Without these features, data will be lost or arrive out-of-order
Why don't you want to open more than one socket? There are ways in all languages for working with multiple sockets.
I don't want a user limitation for a chat room. I already tried to generate a socket per user, but this ended in a big chaos, so I'll use UDP in the moment. Maybe I'll come back to TCP later.

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.