WinSock控件网络通信示例

服务器端程序

Private Sub CommandButton1_Click()

Winsock1.LocalPort = 5600

Winsock1.Listen

End Sub

 

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

If Winsock1.State <> sockclosed Then Winsock1.Close

Winsock1.Accept requestID

End Sub

 

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim s As String

Winsock1.GetData s

TextBox1.Text = s

End Sub

 

Private Sub TextBox2_Change()

Winsock1.SendData Me.TextBox2.Text

End Sub

 

客户端程序

Private Sub CommandButton1_Click()

Winsock1.RemoteHost = “127.0.0.1”

Winsock1.RemotePort = 5600

Winsock1.Connect

End Sub

 

Private Sub TextBox1_Change()

Winsock1.SendData Me.TextBox1.Text

End Sub

 

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim s As String

Winsock1.GetData s

Me.TextBox2.Text = s

End Sub