Winsock Connect Function

Snippets, Projects and Assistance

Moderators: Moderator, Global Moderator

Post Reply
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

Winsock Connect Function

Post by Josh »

Well I was tired of using my winsock to connect to different servers and writing 3 lines of code just to get the control to connect. So I made the following function:

[color=\"red\"][size=1]* [b]Note[/b]: This is used for Microsoft Winsock Control 6.0[/size][/color]

Public Sub Connect(Server As String, Port As String)
With Winsock1
.RemoteHost = Server
.RemotePort = Port
.Connect
End With
End Sub

Syntax:
Connect \"irc.killanet.net\", \"6667\"
\'This is just an example...

Remember to replace [b]Winsock1[/b] with your own named winsock control.

Such a simple code, but such a help in your coding <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />

~Dren (victory)
Last edited by Josh on Thu Oct 13, 2005 11:42 am, edited 1 time in total.
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
FlyingDoggyJake
Sr. Member
Sr. Member
Posts: 254
Joined: Thu Jun 17, 2004 12:37 am

Winsock Connect Function

Post by FlyingDoggyJake »

Good idea Josh <img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' /> ... sometimes these little functions end up being the most useful.
Another example of a simple little function that saves a lot of grief when using Winsocks is:

[color=\"blue\"]Public Sub SendData(Data As String)
On Error Goto err

Winsock1.SendData Data & vbCrLf
Exit Sub

err:
MsgBox \"You must establish a connection before using that control!\"
End Sub[/color]

Then whenever you want to send data you can use this function and never have to worry about your program crashing due to \"Protocol\" or \"Connection State\" runtime errors.

As Josh put it: \"Such a simple code, but such a help in your coding\"
ImageImage
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

Winsock Connect Function

Post by Josh »

Never thought adding an \"On Error Goto err\", good thinking jake!
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Post Reply

Return to “Visual Basic”