Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +

1. I have to program a server which can listen multiple clients on a single port (say 8080).
2. Once connection is established with a client, communication starts between client and server. Client sends - Server Receives, then Server sends - Client receives.
3. If multiple Clients are connected to server at a time on the port 8080, all clients should receive data simultaneously.

Ok, I have done the points 1 and 2. i understand some basics of Socket Programming / TCP/IP Programming. I have created a TCPListener object which keeps in listen mode, and whenever a client connects, it accepts it and create a thread to receive its data and send data to that client.

The problem is when i connect two clients, then the one starts receiving and other client just receive data for one or two time, and then gives an exception -

"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."

Why this is happening?
is there something i am missing or doing wrong.
I think there is something wrong in Threading part.

Please help.

Thanks in advance.
Updated 15-Apr-21 2:09am
Sergey Alexandrovich Kryukov 13-Jan-12 15:56pm    
You are doing something very wrong. This is a very typical schema of the TCP service, and it always worked well with multiple clients on the same port, in different combinations. Does your TcpClient accepts each client? What do you get: a socket of an instance of TcpClient? what do you do with them? Do you use different threads for listening and network read/write operations? Could you make a shortest possible code sample and post it? If you can, please use "Improve question" above.

1 solution

Please see my comment to the question. I can also give you some basic idea and the skeleton of right design I tried to explain in my past solution:
Multple clients from same port Number[^].

If something is not clear, feel free to ask your follow-up questions. The key here is accepting a client, getting an instance of socket or TCP client using System.Net.Sockets.TcpListener.AcceptSocket or System.Net.Sockets.TcpListener.AcceptTcpClient and using separate threads and thread synchronization properly, see[^].

Share this answer
DeepsMann 17-Jan-12 0:10am    
Thanks SAKryukov for your reply,
all these links are really useful.

I have tried using Asynchronous Sockets, and its working fine.
Sergey Alexandrovich Kryukov 29-Feb-12 0:06am    
You are welcome.

Yes, I do understand asynchronous sockets work, by I just think that in most cases the approach based on separate threads making blocking calls in more straightforward and simple, easier to develop and maintain, more general. You see, I remember when threads were not yet available, for example, on Linux, but the systems were multitasking based on processes. At that time, asynchronous APIs already existed and were much more important. Under the hood, they used some parallelism similar the threads (how else?), but on lower level. Now threads are the commonplace and inter-thread communications are well-polished, so using threads should dominate. Asynchronous API is more of the inertia of thinking, in my opinion.

Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900