Jump to content
  • Advertisement
Sign in to follow this  
SolidProject

[.net] Help with my network stuff please

This topic is 4331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello together ! I've developed a simple Network project with .NET 2.0 using TcpClient / TcpListener. Basically the server listens for incoming connections and start a new thread every time a new connection was found. That new thread then runs in a seperate loop waiting for data on that connection. It seems to work correctly, but I noticed that the data transfer seems to be quite slow. When I send a package from a client to the server it can take up to 2-3 seconds before the package is evaluated and handled. So my question would be if there is a better approach to implementing such a multiconnection network core. I don't want to use remoting since it seems to be even slower. I'm not even sure if the "lag" is caused by my code or by the transfer process itself. So what I hope for would be some kind of tutorial / design approach on how to develop such a network core ? Thx for any help.

Share this post


Link to post
Share on other sites
Advertisement
How many threads are you starting during your test?
Sometimes it is better to use the threadpool instead of creating a thread per conection.

Could you show us your server code?

Cheers

Share this post


Link to post
Share on other sites
/// <summary>
/// Main worker thread method. Waits for new connections and creates a new Communication instance for
/// every connected client.
/// </summary>
private void WaitForNewConnection()
{
while (mIsStarted)
{
Thread.Sleep(5);
TcpClient c = null;
try
{
//Waits until either a new client connects or the server is stopped.
//When a new client connected, the next loop repetition will wait for the next one.
c = mServer.AcceptTcpClient();
}
catch
{
}

//Got a valid connection ?
if(c != null)
//Assign a client ID to this client and store the connection in our communication table.
mConnections.Add(mClientKey++, new Communication(c, this));

//Notify the new client about it's client ID.
DataPackage p = new DataPackage(0, 0, ePackageCategory.SystemMessage);
p.AddContent("ClientID", mClientKey - 1);
if(mIsStarted)
SendPackage(mClientKey - 1, p);

//Give the client some time to handle it's successful connection to the server...
Thread.Sleep(100);

//Inform top level about the new connection...
if (NewClientConnected != null)
NewClientConnected(mClientKey - 1);
}
}


This is the server method which accepts new connections. The Communication class then starts a new thread in it's constructor which polls the network stream for new data.

About the threadpool : Isn't the threadpool restricted to a certain amount of threads ? Currently in my test app, I only tested with 2 or 3 connected clients, but I don't know yet how many clients will be necessary later.
First I thought the speed problem was because the server app had too much cpu use but I checked and it stays constant at about 45 % cpu usage so this can't really be the problem.

Share this post


Link to post
Share on other sites
Hmmm... some suggestions:
1. Get rid of the Sleeps.
2. Yes the threadpool is limited. I think I saw mentioned that in .NET 2.0 it is configurable but the idea is that you use a small amout of threads to handle a lot of clients. Let the listening thread recieve incomming calls and immediately dispatch them to another thread. Let the thread return to the pool as soon as possible.
3. Let the Communication thread do the handshake; not the listening thread.
4. When running this on a single cpu/core machine the Communication thread code might also block the listening thread. Only one thread will run at a time.

Cheers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!