Winsock Control

The winsock control lets you write programs in which your program communicates with a "server" over the internet (though you can also use it to write servers). This is accomplished by a simple "conversation" protocol that all servers use: the client sends a message to the server, and the server responds back to the client. The server can also send something to the client, to which the client should respond.

The first thing you have to do is, of course, connect to a server. This is done with the winsock control's Connect function:

Winsock.Connect "mail.ibm.com", "21"

The "mail.ibm.com" is the server you're connecting to, and the "21" is the port to which you're connecting. You'll have to find out the port used by the server you want to connect to.

The two main things to know are how to send messages, and how to receive messages.

Sending messages are easy: the winsock control has a "SendData" function, which you can use just like you'd expect:

Winsock.SendData "USER bnewhall"

Receiving messages are a little more tricky. The winsock control has a DataArrival event, which is fired whenever data arrives at the client. However, you must actually get the data with the winsock's GetData function, like so:

Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)

Dim data As String
data = Winsock.GetData ' data = message from server
lblMessage.Caption = data

End Sub

Then, when you're done, it's good form to Winsock.Close your connection to the server. That's it! You now have the basics for creating a winsock program.


Visual Basic by Example is hosted by GeoCities
1