一个用TCP方式的一服务端多客户端连接示例!'服务器端. '首先在属性框中将Winsock1的index属性设为0 Dim wArr(30, 2) As String Private Sub Command1_Click() Dim i As Integer For i = 1 To 30 If wArr(i, 0) <> "" Then '些发送广播(TCP方式)! Winsock1(i).SendData "这是发送的信息" DoEvents End If Next i MsgBox "全部发送完毕!" End Sub Private Sub Form_Load() Winsock1(0).Protocol = sckTCPProtocol Winsock1(0).LocalPort = 7890 Winsock1(0).Listen End Sub Private Sub Winsock1_Close(Index As Integer) On Error GoTo acd Debug.Print "客户端:" & Winsock1(Index).RemoteHostIP & "已经关闭连接" wArr(Index, 0) = "" Unload Winsock1(Index) Exit Sub acd: End Sub Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long) Dim i As Integer, pi As Integer Dim bi As Boolean If Index = 0 Then For i = 1 To 30 If wArr(i, 0) = "" Then pi = i bi = True Exit For End If Next i If bi = True Then wArr(pi, 0) = Winsock1(0).RemoteHostIP Load Winsock1(pi) Winsock1(pi).LocalPort = 10000 + pi Winsock1(pi).Accept requestID MsgBox "requestID=" & requestID Else Debug.Print "客户端连接已满" End If End If End Sub Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim strmsg As String If wArr(Index, 0) <> "" Then Debug.Print "来自" & Winsock1(Index).RemoteHostIP & "的信息" Winsock1(Index).GetData strmsg ', vbByte Debug.Print strmsg 'StrConv(strmsg, vbUnicode) End If End Sub --------------- '客户端. Private Sub Command1_Click() Winsock1.Close Winsock1.RemoteHost = "vision.vssky.net" '服务器IP Winsock1.RemotePort = 7890 Winsock1.Connect End SubPrivate Sub Command2_Click() Dim abc As String abc = Now Winsock1.SendData abc End SubPrivate Sub Form_Load() Winsock1.Protocol = sckTCPProtocol End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim strmsg As String Winsock1.GetData strmsg, vbString MsgBox "收到信息:" & strmsg End Sub
可怎样才能互相找到呢?我想能在互联网里也能找到,就象有的远程控制程序一样~
谢谢~
所有的在线用户都从服务器里读取出来啊。
用winsock很容易做到阿
他的意思是能象那些远程控制软件一样~局域网可以通过UDP发送广播数据包,但在互联网里该如何实现呢~
我也想知道!
顶!!
'首先在属性框中将Winsock1的index属性设为0
Dim wArr(30, 2) As String
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 30
If wArr(i, 0) <> "" Then '些发送广播(TCP方式)!
Winsock1(i).SendData "这是发送的信息"
DoEvents
End If
Next i
MsgBox "全部发送完毕!"
End Sub
Private Sub Form_Load()
Winsock1(0).Protocol = sckTCPProtocol
Winsock1(0).LocalPort = 7890
Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
On Error GoTo acd
Debug.Print "客户端:" & Winsock1(Index).RemoteHostIP & "已经关闭连接"
wArr(Index, 0) = ""
Unload Winsock1(Index)
Exit Sub
acd:
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim i As Integer, pi As Integer
Dim bi As Boolean
If Index = 0 Then
For i = 1 To 30
If wArr(i, 0) = "" Then
pi = i
bi = True
Exit For
End If
Next i
If bi = True Then
wArr(pi, 0) = Winsock1(0).RemoteHostIP
Load Winsock1(pi)
Winsock1(pi).LocalPort = 10000 + pi
Winsock1(pi).Accept requestID
MsgBox "requestID=" & requestID
Else
Debug.Print "客户端连接已满"
End If
End If
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strmsg As String
If wArr(Index, 0) <> "" Then
Debug.Print "来自" & Winsock1(Index).RemoteHostIP & "的信息"
Winsock1(Index).GetData strmsg ', vbByte
Debug.Print strmsg 'StrConv(strmsg, vbUnicode)
End If
End Sub
---------------
'客户端.
Private Sub Command1_Click()
Winsock1.Close
Winsock1.RemoteHost = "vision.vssky.net" '服务器IP
Winsock1.RemotePort = 7890
Winsock1.Connect
End SubPrivate Sub Command2_Click()
Dim abc As String
abc = Now
Winsock1.SendData abc
End SubPrivate Sub Form_Load()
Winsock1.Protocol = sckTCPProtocol
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strmsg As String
Winsock1.GetData strmsg, vbString
MsgBox "收到信息:" & strmsg
End Sub
希望大家在讨论,讨论,给个提示~先谢过拉~!