Solved

UDP Socket Server Async New Client Instances?

Posted on 2011-02-17
4
1,091 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now