Solved

UDP Socket Server Async New Client Instances?

Posted on 2011-02-17
4
1,129 Views
Last Modified: 2012-05-11
I'm looking at this link concerning a UDP Socket Server Chat.
http://www.daveoncsharp.com/2009/08/csharp-chat-application-over-asynchronous-udp-sockets-part-1/

I question the accumulating client instances, could be hundreds in a very short time. I noticed an ArrayList used to hold client instances, but I think its purpose is for tracking and sending messages back to the clients. I don't think it's used for disposal purposes.

       Client client = new Client();

Q. What if the ArrayList is not used?

Q. Are the clients instances disposed automatically?
0
Comment
Question by:pointeman
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 400 total points
ID: 34919973
I really only made a cursory examination of the code in that link, but at a glance it looks like the ArrayList is used to keep track of logged in clients.  When an incoming message is received the server loops through the list and sends each client in the ArrayList a copy of the message.  When a client sends the "i'm logging out" message the appropriate Client is removed from the ArrayList.

The garbage collector will clean-up any unused objects automatically - such as Client's that have been removed from the list - but the garbage collector runs when it thinks it's necessary.  100's of these objects doesn't really seem like a big deal to me - the client object probably doesn't use much memory.

Although, if the client is using network resources it may be wise to implement IDisposable on the client class, and then call client.Dispose() after removing it from the ArrayList, this way the network resources are released immediately instead of waiting for the garbage collector to get around to it.
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 100 total points
ID: 34922518
Hi,

tgerbert was right with his analysis. Please check the data streams used in the code are getting closed immediately after use. Else they will be cleared only during finalization (resource waste).

Since broadcasting the message (Looping) to all clients will consume time in processing. So use unique ID for every login and perform all transaction to those specific client.
0
 

Author Comment

by:pointeman
ID: 34933666
So you both think the .NET garbage collection will take care of it?

If so, I won't worry about it anymore :)
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 400 total points
ID: 34935153
Yes, "Client" is just a small struct, so there's really no need to clean'em up manually, the garbage collector will get'em - and even with thousands of them you should be fine on memory.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question