Tuning the Receiver
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 = 1000
Winsock1.ListenThis 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.Close
Winsock1.Accept requestIDThis 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 String
Winsock1.GetData strIncoming
MsgBox strIncomingThis 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!
文章来源于领测软件测试网 https://www.ltesting.net/
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073