We'll start our look at Winsock by creating a mini messaging application.
It's not going to be all-out bells and whistles, but you'll be able to send a message from computer A to computer B and it'll give you a feel for how the 'Winsock control' works.
So let's get started:
- Launch Visual Basic
- Click 'Project', 'Components'
- Check the 'Microsoft Winsock Control'
- Hit OK
- Draw out the Winsock control onto your Form
Now, the Winsock control is the key to this article. It allows you to easily communicate over a network.
- Add a Text Box to your Form, changing its Name to 'txtAddress'
- Add another Text Box to your Form, changing its Name to 'txtMessage'
The first Text Box will hold the 'address' of the computer you want to send your message to. And the second box will hold the actual message itself. This will be sent "through" the Winsock control we added just a few seconds ago.
- Add a Command Button to your Form
- Insert the following code behind your button:
Winsock1.RemoteHost = txtAddress.TextWinsock1.RemotePort = 1000Winsock1.ConnectDo Until Winsock1.State = sckConnectedDoEvents: DoEvents: DoEvents: DoEventsLoopWinsock1.SendData (txtMessage.Text)Winsock1.CloseLet's take a few seconds out to explain this code:
Winsock1.RemoteHost = txtAddress.TextWinsock1.RemotePort = 1000
These first two lines of code simply tell the Winsock control which computer it should 'connect' to.
The RemoteHost can either be a computer name or IP address.
Top Tip: An IP address is a series of four numbers separated by dots, such as 188.8.131.52. It's just a numeric address that uniquely identifies your computer. Everyone on your network will have a unique IP address and every time you connect to the Internet, your machine is automatically assigned an IP address and no-one else on the Net will have the same address as yourself. It's sort of like a real address information sent to that address only gets to yourself, no-one else. To find out your IP address, click Start, Programs, MS DOS Prompt, type 'IPCONFIG' then press return
That second line of code simply sets the 'RemotePort'. This can be pretty much any number and you can imagine it as a frequency on a radio dial. Here, you're tuning into 1000 FM on the other computer and preparing to broadcast.
Winsock1.ConnectDo Until Winsock1.State = sckConnected DoEvents: DoEvents: DoEvents: DoEventsLoop
This chunk of code simply attempts to connect to the remote computer and loops around until it has that connection.
Top Tip: It's worth noting that if a connection isn't made because, say, the other computer has just crashed or cannot be found, this code will just continue looping round and round. When an error occurs in Winsock, the State property equals sckError (9) so you may want to add a little code to handle that here.
And finally, this last piece of code sends your message across to that other computer.
So, that's our message-sending program, the client, finished. Now let's build the other end the receiving application...
Page 2 of 9