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

I want to know what technology to use in creating a Chat application in C#, a server and a chat client for the possibility to maintain a minimum of 1000 users logged in at the same and chatting. I have created a application like this with TcpClient, BinaryReader and Writer but when around 250 users connects i receive a message like "the connection was forcibly closed by the remote host". The server is done in this way: when a users connects to the server i create a TcpClient and a thread in witch i am waiting in a try catch and in a while for the message that user can send.

If you want some code please ask.Thanks

Regards,
Sas Gabriel
Posted

1 solution

First idea you should never play with is creating variable number of threads, something like a thread per client. Don't even think about it! It is absolutely excessive and can kill your service. For a basic network service based on TCP and multiple channels, you need to create just two network threads: one listening for new connections, another one reading/writing from/two network stream to implement your application-level protocol.

You can find a quick sketch of such design in my past solutions:

Multple clients from same port Number[^],
automatic updater triggered via server[^].

Please also see the links to other related solutions in these posts.

Good luck,
—SA
 
Share this answer
 
Comments
Gabriel Sas 14-Dec-11 15:32pm    
Nice explanation about how this server should be, now i know in witch direction to go and search about the rest of the information i need. But in this server design i dont understand one thing:
Gabriel Sas 14-Dec-11 15:32pm    
the thread with the tcplistener.acceptclient is very clear to me, but in the other thread how do i receive a message from multiple users? because if i have a BinaryReader read; and i am doing message=read.ReadString(); it blocks and waits until a message is receive, in witch way to implement this?
Sergey Alexandrovich Kryukov 14-Dec-11 18:00pm    
On every accept you receive a socket (through AcceptSocket) or an instance of TcpClient (through AcceptTcpClient), a server object representing remote socket of the connected client. If you work with socket, call its send/receive, if an instance of TcpClient, get a string from it its GetStream and read/write from/to it.

Is it clear?

Also, don't call async APIs as you already use stream and can call blocking APIs in a sequential manner.

It's important to understand the nature of client socket or instance of TcpClient instance. Once connected, it comes in two instances on different processes: one on client, another on server, with one-to-one correspondence. TcpListener does not participate in communications themselves, it only listens/accepts.
--SA

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