Important alert: (current site time 7/16/2013 10:08:41 AM EDT)
 

winzip icon

Complete Async Sockets Demo Server/Client

Email
Submitted on: 5/27/2004 7:50:03 PM
By: DarkMercenary44  
Level: Intermediate
User Rating: By 28 Users
Compatibility: VB.NET
Views: 30375
 
     In the code is an AsyncSockets class that makes the more advanced System.Net.Sockets (Asynchronous Sockets) functions alot easier. This is almost the same as my last submission, this time I have included a full working example of a multiuser server/client chat app. All events are placed in the code for ease of use. I looked everywhere for this code for use in my custom MUD engine framework, I didn't find it so I peiced it together with help from MSDN Library. If you like this code please vote, I worked for almost 3 weeks on getting this right.
 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/7/2004 10:28:15 AMMatt Alline

nice code and all but it took me a week to sludge through it because there were no comments. You should comment more so that people who look at your code are not confused about whats going on. Thanks though :)
(If this comment was disrespectful, please report it.)

 
7/3/2004 3:09:59 AM

Hi DarkMercenary,
Your Assync demo sample is pretty good. But if you document it in a proper manner then it will be very helpful for users on internet. I was trying for some samples in c# which uses sockets for asyn prog. Actually I want to build an application where multiple client can connect and send msgs to server , client to client communication and server to all or specific client. Your sample will help me littel bit. Bye, I need to figure out all the possibilities for my application. Thanks for your sample.
(If this comment was disrespectful, please report it.)

 
7/13/2004 3:49:28 PMMichael E. Chancey Jr.

Your code works well but for some reason when you try to display a new window from within a DataArrival event, it completely locks the window that is being display. Hope maybe you have some insight on why it would do that.
(If this comment was disrespectful, please report it.)

 
7/20/2004 4:30:50 AMEnderBeta

Is there a way for me to check for conection to know if I need to refresh the connection?
(If this comment was disrespectful, please report it.)

 
7/20/2004 6:54:47 AMDarkMercenary44

Michael, I'll look into that for ya.

EnderBeta - You shouldn't need to refresh, say you have a user connected, but you want to check to make sure he is still connected...well you don't need to cause if they disconnect then it will tell you by firing the disconnect event.
(If this comment was disrespectful, please report it.)

 
8/24/2004 3:13:21 PMd1rtyw0rm

This code reach the perfection, great structure, easy to use and understand. This class is a REAL GREAT PIECE OF CODE !!

5 Star for me !
(If this comment was disrespectful, please report it.)

 
8/24/2004 10:21:38 PMDarkMercenary44

Thanks for the words of appreciation d1rtyw0rm.
(If this comment was disrespectful, please report it.)

 
9/16/2004 8:35:08 PM

Code is really COOL. It helped me a lot to learn and understand. Thank you Sir. And I have a question - I added a disconnect button into client and put "_socket.Close()" on it...it writes in client that it is disconnected but server doesn't fire appropriate event. Am I doing something wrong?
(If this comment was disrespectful, please report it.)

 
9/21/2004 7:47:44 AMAmbarish Joglekar

This code is amazing ...i will need some help though ...i hope u will help me with my problems....
thnx in advance ... and 5 from me.
(If this comment was disrespectful, please report it.)

 
9/21/2004 4:55:38 PMDarkMercenary44

Unknown - I'll check on that for you.
Ambarish Joglekar email me if you have questions webmaster@shadowmud.org
(If this comment was disrespectful, please report it.)

 
9/23/2004 10:48:49 AMBueller

Nice code its easily extensible and flexible. It solved a problem i was having with my own socket handler class of how to maintain and properly cleanup connections.
(If this comment was disrespectful, please report it.)

 
10/8/2004 8:50:45 PM

It's a perfect code. I have tried AsyncSockets too but i din't get a solution to my problem. I have searched everywhere but i couldn't find any. Because it's asynchronous like Michael said it locks when i try to open a new window or add a row to a datagrid ! Any solution ?
(If this comment was disrespectful, please report it.)

 
10/8/2004 9:35:31 PM

I've got it !!!!!
I got this problem since 1-2 years ! But tonight i got the solution ! ;)I have seen in another project...
Solution :

'Declaring a delegate
Public Delegate Sub DisplayInvoker(ByVal t As String)

'Declaring a sub
Private Sub DisplayText(ByVal t As String)
datatable.Rows.Add(New Object() {t})
End Sub

' In the _socket_socketDataArrival Event

Private Sub _socket_socketDataArrival(ByVal SocketID As String, ByVal SocketData As String) Handles _socket.socketDataArrival
Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), New Object() {SocketData})
End Sub
(If this comment was disrespectful, please report it.)

 
10/18/2004 3:32:14 AMJames H.

Tested this out with telnet, since you've got port 23 listening in your code, but when I close the telnet session, the Disconnected event is never fired. Happen to know why?
(If this comment was disrespectful, please report it.)

 
10/19/2004 8:31:29 AMelTurcin

It seems the code works ok but I've noticed that when a client disconnects, the server does not free memory...any ideas how to fix this?
(If this comment was disrespectful, please report it.)

 
11/4/2004 3:03:15 PMJason Vetter

Looks good so far. I would change this though.

In AsyncClient.AsyncSocket.vb:
Public Sub Connect

Change the following:
Dim hostEndPoint As New IPEndPoint(IPAddress.Parse(hostIP), hostPort)

** to **

Dim hostEndPoint As New IPEndPoint(Dns.Resolve(hostIP).AddressList(0), hostPort)
(If this comment was disrespectful, please report it.)

 
11/16/2004 9:23:34 AM

Nice code, like it alot. Has saved me heaps of time. I'm wondering if you have tried this code on a portable device connecting to a Host, will the compact framework handle all the code?
(If this comment was disrespectful, please report it.)

 
11/28/2004 11:46:21 PMRoss Peoples

I have sucessfully figured out how to serialize objects (including data structures) through AsyncSockets! Took hours of trial and error, but I finally got it to work. This will save loads of time and headache when transferring objects. If you could implement this in your next release, I'm sure there would be alot of happy folks like me :) If you need the source from my trials, email me.
(If this comment was disrespectful, please report it.)

 
12/21/2004 11:21:54 AMRazack

How can i make a new Socket and connect to a ip and add to the SocketManager ?
(If this comment was disrespectful, please report it.)

 
1/22/2005 12:12:43 AM

Great work. It really fits my needs perfectly and i appreciate all your hard work. One quick question. As soon as I close one of my sockets I get into an infinate loop in the dataarrival event with no data? My clients use winsock(yuck i know) to connect. Any ideas?
(If this comment was disrespectful, please report it.)

 
2/6/2005 1:39:25 AMPaulo S. Silva Jr

Hi, DarkMercenary44 and All.

I had to use a client/server TCP connection in a project I'm working on and found your class. It's quite good.

After reading the comments on the code I notice that several users have a few issues about the client disconnection because the disconnect event isn't fired at the server.

Well it's due the way the TCP protocol is implemented. Imagine the following the client is a remote machine and thus its Socket object is local when it disconnects it doesn't SEND any thing to the server. Without the server RECIEVING anything it cannot fire anything.

(If this comment was disrespectful, please report it.)

 
2/6/2005 1:39:38 AMPaulo S. Silva Jr

That's why most protocols over TCP has a disconnect message that stated the server should close the connection. Once the server RECIEVE the disconnection request it closes the server connection and forget about it.

Well, I also took the liberty of revamped the entire code (adding a few comments too in the process :) reducing the number of classes and implementing a true collecion of connections.

You can find this here at PSC at http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3256&lngWId=10
Regards.

(If this comment was disrespectful, please report it.)

 
7/4/2005 1:20:02 PM

Is a nice Piece of code you have there, just wondering if there is any control ovet the length of the data message?
(If this comment was disrespectful, please report it.)

 
8/29/2005 3:16:07 AMdas

hi greats,
will this code work wioth .net Compact Framework.i have an application which is commun. with a HP-UX server using socket side programming.(i think i hav to use Async.Sockets)
for testing purposes we careetd a c# server appln .which is listening teh client requests for recving data from client. and when v send data from server , the client shoud recv it back.
i tried with this code but having shwoing some pblms in GUID stuff.
another err or shows while
completing the incoming connection
private sub connectioncomplete() procedure.

since .net CF doesnt. support GUID thing , how can i implement these in .net CF?
if Mr.DarkMercenary is hearing my request?
plz help..
i am a new be in .net espacialy in .net CF.

(If this comment was disrespectful, please report it.)

 
2/27/2006 11:12:41 AMJason Vetter

Nice code sir. Things appear to be working very well. My only complaint is that things are poorly typed.

Option Strict On will clearly show you this. Other than that nice.
(If this comment was disrespectful, please report it.)

 
4/24/2006 3:05:14 AMVineet

Nicely written code indeed. It would be nice if you can do some value addition to it by doing the following:
1. Putting comments in the code to give detailed explanation.
2. By showing one block diagram (flow chart) kind of diagram which will clearly explain the flow of work/information and the sequence of it.
3. You may also add a sequence diagram and a state diagram so that it can be understood by people like me who are more comfortable with overview first and then it is easy for them to understand the code, its organisation and logic.
Thanks in anticipation of addtions.
(If this comment was disrespectful, please report it.)

 
6/19/2006 6:48:17 AMRick

Eventhough the code could probably have been more documented this is probably one of the best asynchronous net sockets example here on PCS without using any third-party additional components. Great Stuff!!!
(If this comment was disrespectful, please report it.)

 
8/31/2006 11:06:31 PMbrianham

Wow! Nice Work!
(If this comment was disrespectful, please report it.)

 
4/18/2007 5:25:32 PMJPS500

Private Sub _socketManager_onDataArrival(ByVal SocketID As String, ByVal SocketData As String) Handles _socketManager.onDataArrival
textHistory.Text += vbNewLine & "[" & SocketID & "]: " & SocketData
End Sub

Replace the old sub with that, I added vbnewline so the chat output is

nickname: hey
nickname: whats up

instead of

nickname: hey
heynickname: whats up
(If this comment was disrespectful, please report it.)

 
7/31/2007 11:52:46 AMRoby

Nice bit of code saved me hrs of messing about, could be better documented but fairly easy to work out. only problem I am is finding out the IP address of the incoming remote connection so that I can keep a log of whos connected to the server application
(If this comment was disrespectful, please report it.)

 
8/2/2007 6:31:10 AMzzz

nice work
(If this comment was disrespectful, please report it.)

 
8/28/2007 5:47:25 AMRoby

I added some code to get the remote host Ip address fairly easy, but have spent a while working out why my aplication locked up when a socket dosconnected, basicaly in the asyncSocket.vb ondata arival gets stuck in a loop, i just added a few lines of code to fix this problem, if anyone else has this problem let me know and ill post the code to fix this
(If this comment was disrespectful, please report it.)

 
10/4/2007 10:45:24 AMguideX

Works great! I keep getting 'Object not set to the instance of an object' in strange places, when using delegates out of the sockets raised events, any ideas?
(If this comment was disrespectful, please report it.)

 
10/7/2007 1:54:07 AMguideX

Nevermind, figured it out. This is really great code, I mean IT'S REALLY GREAT!
(If this comment was disrespectful, please report it.)

 
3/21/2008 12:26:07 PMerryko

hello.
i downloaded your chat example some days ago, and i actualyl think it's great, 5 stars for me.
but, i would like to share a problem i found:
on the client side, when the application received data:


Private Sub _socket_socketDataArrival(ByVal SocketID As String, ByVal SocketData As String) Handles _socket.socketDataArrival
Control.CheckForIllegalCrossThreadCalls =

False

Me.Text = SocketData

If SocketData = "ABC" Then
Beep()


End If

End Sub

i added the line of code with IF.
and the problem seems to be very strange. in fact the application strangely doesn't reproduce the beep.
i also tried to apply this code to the server but there, it WORKS... creepy.
i worked on it for an entire day, but couldn't find a solution... anyone any idea?
(If this comment was disrespectful, please report it.)

 
11/25/2008 6:06:00 AMRizky

Nice Code, I'm currently working on the both the server and multiple clients with tcp port connections. A port has been opened at server side for listening. Have tried multiple clients connections upto 120 clients. But in certain times, new client connections to the server are failed, but the existing client connection to server side are stil working fine if they're still connected, once they disconnected it, they cant do reconnection.
Meaning to say that the server side doesnt respond to any new client tcp connection after some times. No specific error message could be found. Do you have any idea on this? Any possibilities for this matter to happen?
Look forward to your helps. Thanks in advance.

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.