VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 让使用Winsock的程序即可以发送又可接收
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:10726, 日期:2001-04-18
Going All Bi-Directional

So far, we注释:ve created a program that sends messages. And a program that receives messages.

But the send program can注释:t receive. And the receive program can注释:t send.

Now, I may not be an Einstein ?but the world pretty much survives on two-way communication. And even though most introductions don注释:t usually delve beyond the code we注释:ve explored so far, let注释:s be honest ?ICQ wouldn注释:t have a membership base of ninety million if you could only send messages one way.

So in order to implement this functionality, I注释:ve created a quick 注释:template注释: application that allows you to both send and receive. You can download that here ?go on, have a play around and see what you can do!

And for all the groovy guys and gals printing this article, here注释:s a dump of the core code, complete with basic error handling. All objects, such as Text Boxes have been given 注释:English注释: names to make the code more readable:

Private Sub Form_Load()

On Error GoTo PortErr

注释: Listen out for any possible connection
注释: requests or data, etc.

Winsock1.LocalPort = 202
Winsock1.Listen

注释: This uses port 201. To work, this program
注释: must be run on two separate computers
注释: - otherwise there will be a conflict with
注释: two programs trying to access port 201

Exit Sub
    
PortErr:

    MsgBox "Another program is using port 201. This " & _
    "may be because you注释:re running another copy " & _
    "of this program on the same computer. " & _
"Unfortunately, this only works over a real " & _
"network, either an office setup or the Internet. " & _
"When two computers try to 注释:listen in注释: to the same " & _
"part of a computer, one of them receives an error. " & _
"I am alas the unlucky soul. Sorry! Here注释:s an idea " & _
"though - alter my Form_Load source code, changing the " & _
"LocalPort property to 202 instead of 201. This should " & _
"allow you to test both programs on the same machine!", vbCritical
        
    End

End Sub

Private Sub cmdSendMessage_Click()

    If Winsock1.State = sckConnected Then
        
        注释:If connected, just send the data
        
        Winsock1.SendData (txtOutgoingMessage.Text)
        
    Else
    
        If MsgBox("You are not connected. " & _
            "Connect to " & txtAddress.Text & "?", _
            vbYesNo + vbQuestion) = vbYes Then
            
            注释:Connect to the remote machine
            
            Winsock1.Close
            Winsock1.RemotePort = 201
            Winsock1.RemoteHost = txtAddress.Text
            Winsock1.Connect
                
            注释:Wait until full connection is made
                
            Do Until Winsock1.State = sckConnected
                DoEvents: DoEvents: DoEvents: DoEvents
                If Winsock1.State = sckError Then
                    MsgBox "Problem connecting!"
                    Exit Sub
                End If
            Loop
        
            注释:Send the data
        
            Winsock1.SendData (txtOutgoingMessage)
        
        Else
        
            Call MsgBox("OK - not connected!", vbInformation)
        
        End If
    
    End If
        
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

    注释:Accept an incoming connection request
    
    If Winsock1.State <> sckClosed Then Winsock1.Close
    Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    
    注释:Display incoming data
    
    Dim strIncoming As String
    Winsock1.GetData strIncoming
    txtIncomingMessage.Text = strIncoming
    
End Sub