Important alert: (current site time 11/22/2014 9:57:42 PM EDT)
 

winzip icon

VB.NET Sockets Client Class

Email
Submitted on: 3/17/2002 12:28:58 AM
By: Larry Gadea  
Level: Intermediate
User Rating: By 45 Users
Compatibility: VB.NET
Views: 94079
 
     This code will let you use the power of .NETs System.Net.Sockets like the Winsock Control in Visual Basic 6. I have included a nice and compact version of an IRC client which you can use as an example on how to use this class. Please take the time to vote for it and give me some feedback with what you think. Version 1.1 fixes reconnection errors.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :VB.NET Sockets Client Class
//**************************************
Please let me know if you have any problems by emailing me at: trivex@trivex.net. Also, take note that the email in my profile is no longer accurate.
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.

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

3/17/2002 1:07:56 AMLarry Gadea

Please let me know if you need any new things added or any problems. Im always here to help.
(If this comment was disrespectful, please report it.)

 
3/17/2002 2:30:30 PMJuan

Cool, i want a server!

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

 
3/17/2002 6:30:56 PMLarry Gadea

Hehe :) Im working on it. Remember to vote if you like my code.
(If this comment was disrespectful, please report it.)

 
3/19/2002 7:02:32 AMjoseph

very nice source code..
I want to get server code
As soon as...
thanks...

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

 
3/20/2002 3:07:39 AMManas Mukherjee

hmm! did you check wintalk.sln comes with microsoft sdk? You may like to look at it.
(If this comment was disrespectful, please report it.)

 
4/3/2002 7:52:51 AMTommy

Only disconnecting doesn't seem to work.... gives me manny strange errors...
(If this comment was disrespectful, please report it.)

 
4/7/2002 7:37:25 AMTommy

OK, to start with. I did NOT give you a low rating ok? And if i did it was not on purpose. The code is very cool but me and my 10-year-old brain as u like to call it can't find out about the disconnecting. For all the rest, the code works great and it's verry usefull. Thnx for the nice code but next time plz don't call me a 10 year old just because i'm LEARNING and because english isn't my first language.
(If this comment was disrespectful, please report it.)

 
4/19/2002 5:01:40 AMEnknown

So how is the server coming up?
*bring me christmas already!* =)
(If this comment was disrespectful, please report it.)

 
4/19/2002 11:08:26 AMAnibal Acosta

I include this to check if is connected or not

Public Function Connected() As Boolean
Try
Return client.Connected
Catch
RaiseEvent onError(Err.Description)
Exit Function
End Try
End Function

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

 
5/2/2002 9:16:08 AMUltimatum

In order to get the socket to work again after a disconnect, you must put 'client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)' directly after the client.Close()
(If this comment was disrespectful, please report it.)

 
5/2/2002 9:17:19 AMUltimatum

(continued from above)
Otherwise, .NET will dispose of the socket object, therefore disallowing a re-connect.
(If this comment was disrespectful, please report it.)

 
5/12/2002 8:51:40 PMKen

Very cool....you've taken a lot of additional trial and error time out of my weekend. Go Go Gadget Email Client...
(If this comment was disrespectful, please report it.)

 
5/17/2002 12:46:23 AMRyan

Great job! I believe the problem with the disconnect is that is disposes the socket before it trys to send disconnect data to the client. I could be wrong, but if I come up with an answer I will post it here :-)


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

 
5/17/2002 12:59:40 AMRyan

'add this at top of socket class
Private mConnected As Boolean

'Add this line to the top of Sub sockdataArrival
If mConnected = False Then Exit Sub
(If this comment was disrespectful, please report it.)

 
5/17/2002 1:00:06 AMryan

Public Sub Disconnect()
Try
'Kill the socket.
client.Shutdown(SocketShutdown.Both) 'Kill any data being sent or received.

'let the functions know that we are disconnecting
'this prevents data being sent after the socket closes
'apparently .Shutdown doesn't work as documented
'this is the work around
mConnected = False

client.Close() 'Kill socket connection.
client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
(If this comment was disrespectful, please report it.)

 
5/17/2002 1:00:19 AMryan

I implemented this to send e-mails via my local smtp server - it worked beautifully several times
Hope this helps - Ryan
(If this comment was disrespectful, please report it.)

 
5/17/2002 1:01:35 AMRYAN

feel free to e-mail me if you want the code......I might post it if I get too many requests :-)
(If this comment was disrespectful, please report it.)

 
5/22/2002 10:16:39 AMDavid Elizalde

Good job! This code is a lot better than what MSDN is trying to provide (meaning your works)... When's the server coming out?
(If this comment was disrespectful, please report it.)

 
5/31/2002 3:35:42 PMdatz

Thanks Larry for this source. Helped me alot :)
(If this comment was disrespectful, please report it.)

 
6/16/2002 1:56:27 PMNehal

hello,
thank u for your code, but i wanted to ask you about sending SMS to a mobile number through vb.net...isn't it done through sockets too? and if you can send me any related info i will be really thankfull, and ii also wanted to know whether you wrote the server or not?
Regards.
(If this comment was disrespectful, please report it.)

 
7/23/2002 8:08:47 PMXantrh

Thanks, I am just beginning to look at vb.net and this is exactly what I was needing. You have saved me alot of time too :)
(If this comment was disrespectful, please report it.)

 
8/1/2002 12:58:18 PMThe New iSoftware Company!

Wonderful! I rewrote it all for my needs. Hope you dont mind. Id be glad to create a server. I have some ideas. Email me. 5!
(If this comment was disrespectful, please report it.)

 
8/8/2002 8:44:03 AMGuo Xu

How can I use Indexes like in VB6?
(If this comment was disrespectful, please report it.)

 
8/11/2002 4:40:09 AMShihab

Great work Larry, I was really impressed with the code, the thing is though, it would'nt connect with me, I always get "Error: connection refused" whenever I try to run your code. I've got both smtp & pop3 servers, both failed to work ... Would someone tell me what am I doing wrong in here ?

Thanks in advance for everything,
Shihab
(If this comment was disrespectful, please report it.)

 
8/23/2002 5:31:13 PMLarry Gadea

I have made a version 1.1 to fix reconnection issues.
(If this comment was disrespectful, please report it.)

 
9/9/2002 2:42:16 PMAndrew Mathiason

how do you send commands? like /join etc?
(If this comment was disrespectful, please report it.)

 
9/9/2002 2:51:36 PMAndrew Mathiason

Nevermind, I read the rfc.
(If this comment was disrespectful, please report it.)

 
11/6/2002 2:23:04 PMSahand

Well there is a problem when using this and any other socket classes (including my own) with windows forms. when you create a new window in the DataArrival event of the object , the window will run on another thread and as a result it freezes and when the thread exits , the form is closed too.
Do you have any idea how to solve this?
(If this comment was disrespectful, please report it.)

 
12/26/2002 2:09:04 PMclearlytechnical

Larry, I emailed you and hope to here from you!

Thank you so much for this contribution, I am sure it has helped many many many people write socket programs.

Very clean. Nice structure.


Thanks again,



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

 
1/31/2003 7:21:34 PM

Thank you for that great piece of code !

Joyfully impressed,
BK


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

 
3/24/2003 10:41:17 PMRobert Croll

Is Gadea, is good.
Perfect for those moving across from VB6
(If this comment was disrespectful, please report it.)

 
4/11/2003 12:23:39 PMkwickdoc

Do I miss anything here? I couldnt use this in VB6. i NEED AN OCX OR DLL. Anyone let me know how to use this in VB6 Thanks
(If this comment was disrespectful, please report it.)

 
4/26/2003 11:11:32 PMchalex

hey, im trying to use this class in my aim client project and when ever i try sending data i get an error from the onerror event saying "cast from string". anyone have an idea on how to fix this?
(If this comment was disrespectful, please report it.)

 
5/3/2003 8:11:05 AMGuo Xu

You can never use a .NET dll in vb 6
(If this comment was disrespectful, please report it.)

 
7/19/2003 1:21:17 AM

When I try and use it
I'll enter
(If this comment was disrespectful, please report it.)

 
7/19/2003 1:22:02 AM

When I try and use it I'll enter PRIVMSG " and it only sends the last word of my message. Why is this and how can I fix it?
(If this comment was disrespectful, please report it.)

 
11/19/2003 4:59:19 PM

it is tike so that you need to write PRIVMSG : so it will work

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

 
12/6/2003 12:18:14 PMLeeus

spot on, does the job, well done
(If this comment was disrespectful, please report it.)

 
12/12/2003 7:44:29 PMBenjamin Hall

I've come accross the oddest thing.

What i was trying to achieve is a telnet.log file, basically recording all data recieved from my socket.
Now when i convert my byte array to a string everything looks fine, when i write this string to a textbox it looks fine... but if i try to write this string directly to a textfile using obj.WriteLine(sData) i get a HUGE text file.

What it looks like its doing is writing the complete recievebuffer (32767 characters) and i dont understand why.
If i write the string to a textbox and then the textbox.text to a file using the same method i get exactly what i would expect.


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

 
12/12/2003 11:31:09 PM

Great code!!! Thank you!
(If this comment was disrespectful, please report it.)

 
3/14/2004 5:22:43 PM

This code has been very helpful. I just have one question.

Where is the part of the code that is actually sending the data recieved into Textbox1? I am looking, and I am not seeing it -- maybe I'm just tired.
(If this comment was disrespectful, please report it.)

 
4/13/2004 8:47:38 AMFade (Amit BS)

Using the '.Connected' proeprty does not reflect the actual state of the socket,
i is very possible that the socket has been disconnected(remote disconnect, error etc.) and that the .Connected property will still return TRUE, keep that in mind you guys,

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

 
5/11/2004 2:22:32 PM

Any one has a VB6 version of Socket client with win 32 API, I will compile to a dll.

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

 
5/13/2004 4:55:11 AM

Hi, Larry!
Where can I find the 1.1 version for your awarded "VB.NET Sockets Client Class" you mention.

I'll be very pleased, as well, to find out if you've released a server for it.


Thank you very much.

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

 
10/4/2004 6:53:24 PM

Great work! Wouldn't mind seeing what you could come up with for a fully functional IRC client/server...!
(If this comment was disrespectful, please report it.)

 
10/10/2004 6:41:20 AM

hi larry,
the client part was gr8.. could u plz give me the server part
thanx
atit

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

 
12/19/2004 9:55:41 AM

thx so much coz it will help me to get a job...
(If this comment was disrespectful, please report it.)

 
1/19/2005 7:17:31 AM

Would you be able to send me the 1.1
version for your
(If this comment was disrespectful, please report it.)

 
1/19/2005 7:19:00 AM

Would you be able to send me a copy of version 1.1 of your "VB.NET
Sockets Client Class". Also, did you ever release a
server for it.

Thanks
Alison

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

 
2/16/2005 9:47:06 PMlaney

great work mr. larry..
do u hav already server side?
pls email me.. tnx a lot.

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

 
2/20/2005 11:45:47 AM

Excellent work, I was trying to make a server class but misserably failied :(

If you have created one could you send it to me? Thanks a lot much is appreciated!
(If this comment was disrespectful, please report it.)

 
2/27/2005 3:21:21 PMDennis Petrasch

This is a wonderful Class. But i cant say anything :-/. What is the command for ? (normally /say) .. but he tolds me: /SAY :Unknown command. Know a solution ? Thanks
(If this comment was disrespectful, please report it.)

 
3/3/2005 7:59:51 AMSomeone

A couple of questions:

1) Why are the member variables Shared?
2) What about thread safety? Events are raised on a different thread (the worker thread) which may cause errors on the application that utilizes the class.
(If this comment was disrespectful, please report it.)

 
3/14/2005 6:13:30 AM

great work.. i need this C# code... can any body please post the same code in C#.

Thanks in advance.

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

 
3/29/2005 2:11:40 AMCHUBBZonDUBBZ

Could you please teach me what each line of this code does? Thanks
(If this comment was disrespectful, please report it.)

 
3/29/2005 2:12:21 AMCHUBBZonDUBBZ

Well, each important line, I would like to know so I could write a program like this from scratch
(If this comment was disrespectful, please report it.)

 
3/31/2005 8:23:39 PM

This code is EXCELLENT, although there may be a slight problem with HTTP downloading files... I am emailing the author to attempt to resolve this issue!
(If this comment was disrespectful, please report it.)

 
8/19/2005 2:08:55 AMnetAdmin

Nice code man.....


i found one more interesting code to make your own sniffer thought somebody might need one ..... have a look at it ......


http://www.pscode.com/vb/scripts/ShowCode.asp?lngWId=10&txtCodeId=3875

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

 
8/22/2005 8:38:18 AMdas

i wud like to knwo whether it wil work in .net CF ?in vb.net ins smrt device appln
i have smartdevice with wince4.2 ver is running am usng .net compact framework .
would you plz posta working -real time code for vb.net CF?
regds
thnx in advance............

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

 
10/17/2005 9:25:01 PMdee-u

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

 
10/24/2005 9:02:40 PMKevin

I see that Srini has requested this code in C# but do not see any responses. I also do not see his email so... Does anyone have this available in C# and would you be willing to share it?

Thanks much

I can be emailed at kevin.evers@everrec.com
(If this comment was disrespectful, please report it.)

 
6/11/2006 4:52:18 PMRobin Degen

I am re-writing this class to be IRC specific. So it will have events like "ChatReceived" and commands like "JoinChannel" or "SendMessage". It is pretty advanced allready, and with your permission i'll post it to (properly credited offcourse)
(If this comment was disrespectful, please report it.)

 
8/9/2006 5:30:41 PMJesse Starks

Took me a while to figure this out - but in case anyone is using this code with .NET 2 framework - packets may be handled out of order by the windows form thread.

To fix this be sure to process the protocol commands in the sockets class using the async callback
(If this comment was disrespectful, please report it.)

 
1/5/2008 12:55:22 PMstephane

well i did spend days looking for such a solution in vb.net and i think its the best for a beginner in VB.net
(If this comment was disrespectful, please report it.)

 
2/1/2008 2:51:04 AMWalter Senekal

Thanks very much for sharing this.
This code is very usefull, it is the best by far that i have found so far.


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

 
8/21/2008 7:59:05 AMNubbins

Hi,

Can't get ur IRC client to "register" onto my IRC server (Quakenet) it'll ping timeout eventually (before it joins channels/etc)
(If this comment was disrespectful, please report it.)

 
12/22/2009 11:30:14 AMLusian

it's so cool! i love it.
thanks :)
(If this comment was disrespectful, please report it.)

 
7/24/2011 9:34:31 AMident

Private Sub AT(ByVal source As Object)

If (Me.InvokeRequired) Then
Me.Invoke(New System.Threading.ParameterizedThreadStart(AddressOf AT), source)
Else
TextBox1.Text += String.Format("{0}{1}", DirectCast(source, String), Environment.NewLine)
TextBox1.ScrollToCaret()
End If

End Sub
(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.