VBGood网站全文搜索 Google

首页 - 经验之谈 - 首次使用Winsock
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:11194, 日期:2001-04-18
Our First Winsock Application

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.Text
Winsock1.RemotePort = 1000


Do Until Winsock1.State = sckConnected
DoEvents: DoEvents: DoEvents: DoEvents

Winsock1.SendData (txtMessage.Text)

Winsock1.CloseLet注释:s take a few seconds out to explain this code:
Winsock1.RemoteHost = txtAddress.Text

Winsock1.RemotePort = 1000These 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 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.

Do Until Winsock1.State = sckConnected
   DoEvents: DoEvents: DoEvents: DoEvents
LoopThis 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.

Winsock1.SendData (txtMessage.Text)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...