Sign in to follow this  
prof_1

C# UdpClient stops sending..

Recommended Posts

Hey.

[b]Client[/b].
- Creates a UdpClient.
- Starts a Listen-Thread for listening at the server with UpdClient.Receive

[b]Server[/b].
- Creates a UdpClient.
- Creates one thread for listening to messages
- and one Thread.Timer (with function assigned to it) for sending out positions of every client to every other client.

It works and so on. But it seems like the server will stop sending messages after a while. I counted the messages sent. When i have 1 client it will send around 60-100. If i have 5 clients, it will send 20-30.


Error could therefore be Either the UdpClient, The threads or the Timer (with function assigned to it..)

Could not find anything on the web regarding this.


[i][sub]Also i think i found a bug on the forum. If account is blocked because of password-misses - you can reset password to log in. Hehe.[/sub][/i]

Share this post


Link to post
Share on other sites
Well i have narrowed it down to the Timer function, wich is pretty much improvised i think. (Mod can probably move topic now..)
[code]
public void startServer()
{
udp = new UdpClient(80);
listenthread = new Thread(listenAndHandleMessages);
listenthread.Start();
...
...
Timer timer = new Timer(new TimerCallback(updateAndSendMessages), null, 0, 10);
}
[/code]
[code]

void updateAndSendMessages(Object obj)
{
// send to clients.
}
[/code]

I use [i]null [/i]and i have an parameter "object" in the function. I do not use it, do i have to use it in some way? I mean, the code works. But stops fireing the timer for some reason..

Share this post


Link to post
Share on other sites
First, I think the timer variable should not be local like how you have it. It should be a class member or a global variable, depending on the rest of the code, so you keep an active reference of it alive so it is not GC'ed. I can't say I know for certain if that would cause a problem or not in the long run, but that looks really suspicious to me.

Second, when a timer seems to go poof, it is usually because you have a deadlock somewhere in the system or unhanded exceptions. Since you are using threads, I'd suggest you start combing your code for any possible places of deadlock or thereof lack of locking that might cause issues.

Share this post


Link to post
Share on other sites
[quote name='prof_1' timestamp='1298503845' post='4778195']
Well i have narrowed it down to the Timer function, wich is pretty much improvised i think. (Mod can probably move topic now..)
[code]
public void startServer()
{
...
Timer timer = new Timer(new TimerCallback(updateAndSendMessages), null, 0, 10);
}
[/code]
[/quote]

That "Timer" object will be garbage collected, at which point it will stop sending messages.

Share this post


Link to post
Share on other sites
Ah. It was because of the GC. Thanks!

[s]And yes i probably have deadlocks. Especially when i try to read from a dictionary and some other thread tries to add in it. I do lock it with lock(){} and it seems to happen only sometimes..[/s]

[u]edit: seems like the code will stop here when i have a bunch of clients. 100 clients = stop. They wont receive messages.. When i have like 5 it works forever.
I use lock(){} every time i use my collections, adding reading and such.. Is this bad?[/u]

[code]

void listen()
{
while (true)
{
Console.WriteLine("listening");
byte[] data = udpclient.Receive(ref ipendpoint); <--- here i guess..
if (data.Length != 0)
{
char[] splitter = { ' ' };
[/code]

[s]Basically. What should i use for being able to listen (aka add clients while running) and sending (send positions to other clients) also while running?[/s]
[s]
[/s]
The code does work. But i used my external IP wich made the messages get lost i assume. Changed it to localhost and now it works again. With 100+ clients.

Share this post


Link to post
Share on other sites
It's hard to say without knowing more specifics of how everything is setup, how you are testing, and what the rest of your code looks like.

If you are trying to run 100 clients on one PC and your UDP messages are too large or are being sent too fast, it's possible you are simply overwhelming your network. If you have a router that has any features for managing traffic, it's possible it's opting to simply drop most of the packets. Alternatively, if you are trying to run a stress test between two different networks, it's possible your ISP has certain limitations on the UDP traffic and will drop most of it since it might look like "suspicious activity". Those cases are usually triggered when testing with the same (or really similar ) packet over and over so it looks like an attack. You should also check out the [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=15"]Forum FAQ[/url] because it provides some more details regarding UDP and NAT punch through that you might have to make use of as well.

If you say it works fine on localhost with a lot of connections, then I'd start considering network issues first rather than specific code issues. However, you do still need to make sure you are catching exceptions where appropriate and add enough debugging information to know what is going on for sure. When it comes to lock and collections, you will want to read this: [url="http://blogs.msdn.com/b/jaredpar/archive/2009/02/11/why-are-thread-safe-collections-so-hard.aspx"]Why are thread safe collections so hard?[/url] It goes over the basics of a lot of mistakes people make with their implementations. There are also other important resources for using threads and locks, but it's something you just have to research and learn if you are going to be making use of them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this