Private Sub Form_Load() Winsock1(0).LocalPort = 2013 '指定服务器监听端口 Winsock1(0).Listen '开始监听 End Sub
Private Sub Winsock1_Close(Index As Integer) Winsock1(Index).Close '对方断开 End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long) Dim I As Integer I = 1 If Winsock1.UBound > 0 Then For I = 1 To Winsock1.UBound If Winsock1(I).State = 0 Then Exit For '查找空闲的winsock控件 Next End If If I > Winsock1.UBound Then Load Winsock1(I) '没有空闲控件的话创建一个新控件 Winsock1(I).Accept requestID '应答客户端的请求 连接建立时将触发connect事件,表示连接正式建立 End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim Data() As Byte'如果只想试试效果的话你可以改成 Dim Data As String '不过用数组接收数据会比字符串好 Winsock1(Index).GetData Data '接收数据 '数据通信基本上就在这下边来写代码了 End Sub
Private Sub Winsock1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) Winsock1(Index).Close '网络错误