If you've ever attempted to build a real-life radio receiver (I have... D'OH!) - you'll be pleased to know that programming one in Visual Basic is so much easier.
Let's get on with it:
- Launch another instance of Visual Basic
- Add the Winsock control
- Behind the Form_Load event, add this code:
Winsock1.LocalPort = 1000Winsock1.Listen
This tells your Winsock control to listen in to 1000 FM for any incoming messages.
Next, let's add a little code behind the Winsock control itself. There are two main events we'll be dealing with here, ConnectionRequest and DataArrival. Let's cover these now:
- Add the following code behind the ConnectionRequest event of the Winsock control:
If Winsock1.State <> sckClosed Then Winsock1.CloseWinsock1.Accept requestID
This code will run when our program receives a connection request, such as from our message-sending client attempting to do a Winsock1.Connect.
Firstly, this code ensures the Winsock control is 'closed' in other words, makes sure it isn't connected to another computer. Then it accepts the connection request and the two computers are magically 'linked'.
- Add the following code behind the DataArrival event of the Winsock control:
Dim strIncoming As StringWinsock1.GetData strIncomingMsgBox strIncoming
This event fires up when information is arriving when someone does a 'Winsock1.SendData' - following a successful connection. The first line creates a variable to store the incoming information. The second line slots the incoming information into the variable via the GetData method. After this you can do what you want with the data I chose to simply display it in a message box.
Top Tip: After you've retrieved the data via the GetData method, it disappears from memory. If you want to look at it without having it vanish from memory, use the PeekData method.
And that's it! You've created both a program to send a message (the client) and a program to receive a message (the server). Of course, you could merge them both together to both send and receive, but we'll cover that in a couple of minutes. For now, let's test our project:
- Press F5 in each copy of Visual Basic to start each application
- In your message sending application, fill in the two Text Boxes the first should contain the computer name or IP address of the machine you are sending the message to, the second should contain your actual message
Top Tip: If you're running both the sending and receiving program on the same computer, you can use the IP address 127.0.0.1 this number simply refers to your computer.
Another Top Tip: It's always more stable using an IP address to locate a computer, than a machine name. But how can you find out a users IP address? Do what ICQ does! When your users log on, check out the Winsock's LocalIP property, which contains the users unique IP address. Then register this with some central database. When someone wants to send a message, lookup the user, find their IP address, then send them a message. When they log off, remove their details from your database. That's how the biggies do it!
- Press the Command Button in your message sending program
- Flip back to your receiving program
Do you see a box holding your message text? You should do! If so, congratulations you've just sent your first message via the Winsock control.
And even if you're experimenting with this on your one computer remember, it can work anywhere. You could be sending messages right across the office, via your company network, or even right across the globe thanks to the Internet.
Now, let's briefly review the major code snippets we've looked at here in printable format!
Page 3 of 9