Private Sub Command1_Click() If Option2.Value = True Then 'On Error Resume Next udpPeerA.Close udpPeerA.RemoteHost = Text1.Text '要连接到的计算机名 udpPeerA.RemotePort = Text4.Text '要连接到的端口号 udpPeerA.LocalPort = Text3.Text '该Winsock控制将要使用的本地端口号,便于其它端与该Winsock通讯 udpPeerA.Bind '将该Winsock控制绑定到该本地端口 MsgBox "远程IP" & udpPeerA.RemoteHost & "对端端口" & udpPeerA.RemotePort & "本地端口" & udpPeerA.LocalPort End If
'------------------------------TCP 启动监控------------------------------- If Option1.Value = True Then If Sck.LocalPort <> Text2.Text Then Sck.LocalPort = Text2.Text '初始化本地端口 If Sck.RemoteHost <> Text1.Text Then Sck.RemoteHost = Text1.Text '初始化对端IP Sck.Listen ' 开始监控,以便其它用户可以连接到我这里 MsgBox Sck.LocalPort & " " & Sck.RemoteHost End If End Sub
Private Sub Command2_Click()
'初始化UDP空间的IP地址端口及发送信息---------------------- UDP ----------------------- If Option2.Value = True Then udpPeerA.SendData txtSend.Text '发送文本 End If '------------------------------------------------------- UDP结束 ----------------------
'--------------------------------- TCP 发送信息 ----------------------------------- If Option1.Value = True Then MsgBox Sck.State If Sck.State <> 0 Then Sck.Close Sck.Connect Text1, Text2 ' 连接到指定的IP地址指定的端口号上 Timer2.Enabled = True End If '--------------------------------- TCP 发送信息结束 ---------------------------------
End Sub
Private Sub Form_Load()
End Sub
Private Sub Sck_Connect() Timer2.Enabled = True '如果网络已经是CONNECTED,就发送信息 End Sub
Private Sub Timer1_Timer() lblState.Caption = Sck.State End Sub
Private Sub Timer2_Timer() Sck.SendData txtSend.Text ' sent data 'Sck.Close lblState.Caption = Sck.State & "ok" Timer2.Enabled = False
End Sub
Private Sub udpPeerA_DataArrival(ByVal bytesTotal As Long)
Dim strData As String udpPeerA.GetData strData, vbString txtOutput.Text = strData udpPeerA.SendData txtSend.Text '发送文本 'udpPeerA.Close End Sub
Sub UpdateState() ' sub for controling sck.state 'If Sck.State = 7 Then Command3.Enabled = True: Command4.Enabled = True Else Command3.Enabled = False: Command4.Enabled = False ' is connected ? ' print the state of sck Select Case Sck.State Case 0 lblState.Caption = "0 - sckClosed" Case 1 lblState.Caption = "1 - sckOpen" Case 2 lblState.Caption = "2 - sckListening" Case 3 lblState.Caption = "3 - sckConnectionPending" Case 4 lblState.Caption = "4 - sckResolvingHost" Case 5 lblState.Caption = "5 - sckHostResolved" Case 6 lblState.Caption = "6 - sckConnecting" Case 7 lblState.Caption = "7 - sckConnected" Case 8 lblState.Caption = "8 - sckClosing" Case 9 lblState.Caption = "9 - sckError" End Select End Sub
Private Sub sck_ConnectionRequest(ByVal requestID As Long) If Sck.State <> sckClosed Then Sck.Close ' if not closed the connection close it Sck.Accept requestID ' accept the requestid End Sub
Private Sub Sck_DataArrival(ByVal bytesTotal As Long) Dim IncomeData As String Sck.GetData IncomeData, vbString ' get data txtOutput.Text = IncomeData ' append the data to the textbox 'Sck.Close End Sub