我毕设是做基于VBwinsock网络数据传输的 我现在的程序能实现一对多的连接 现在就差再实现一个功能就是在服务器和客户端界面显示在线用户列表 哪位大侠能帮下忙啊 感激不尽 下面是我现在的源代码
服务器
Private MaxChan As Integer '最大通道数
Private Sub Command1_Click()
EndEnd Sub Private Sub Form_Load() Dim i As Integer MaxChan = 10 For i = 1 To MaxChan - 1 Load sckServer(i) Next i sckListen.LocalPort = "2012" sckListen.Listen End Sub Private Sub sckBusy_Close() sckBusy.Close End Sub Private Sub sckBusy_DataArrival(ByVal bytesTotal As Long) sckBusy.SendData "服务器忙,请稍后再连接!" DoEvents End Sub Private Sub sckListen_ConnectionRequest(ByVal requestID As Long) Dim i As Integer '决定由哪一Winsock接受请求 For i = 0 To MaxChan - 1 If sckServer(i).State = 0 Then Exit For End If Next i If sckServer(i).State = 0 Then sckServer(i).Accept requestID Exit Sub End If '如果所有Winsock都用完则由专门的"忙"Winsock接受请求,以免用户要求得不到响应 sckBusy.Close sckBusy.Accept requestID End Sub Private Sub sckListen_Error(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) sckListen.Close sckListen.LocalPort = 1000 sckListen.Listen End Sub Private Sub sckServer_Close(Index As Integer) sckServer(Index).Close End Sub Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim s As String Dim i As Integer sckServer(Index).GetData s If UCase(Left(Trim(s), 2)) = "PT" Then '判断是否为悄悄话,点对点方式 If IsNumeric(Mid(Trim(s), 3, 1)) Then i = Mid(Trim(s), 3, 1) sckServer(i).SendData "Channel " & Index & " " & Right(Trim(s), Len(Trim(s)) - 3) DoEvents End If Else '广播方式 For i = 0 To MaxChan - 1 '利用winsock的State属性给所有连接在服务器上的客户发消息 If sckServer(i).State = 7 Then sckServer(i).SendData "Channel " & Index & " " & Trim(s) DoEvents End If Next i End If lstReceive.AddItem "Channel " & Index & " " & Trim(s) End Sub Private Sub sckServer_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) sckServer(Index).Close
End Sub
客户端Private Sub cmdConnect_Click()
On Error GoTo ErrorPro
sckClient.RemoteHost = Text1.Text
sckClient.RemotePort = Text2.Text sckClient.Connect Exit SubErrorPro: MsgBox "服务器未开或网络出错!" End End Sub Private Sub cmdSent_Click() sckClient.SendData txtSent.Text End SubPrivate Sub Command1_Click()
End '断开连接
End SubPrivate Sub ip_Click()
Text1.Text = "211.83.114.28"
End SubPrivate Sub port_Click()
Text2.Text = "2012"End Sub Private Sub sckClient_Close() MsgBox "服务器通道已关闭!" End End Sub Private Sub sckClient_Connect() MsgBox "连接成功!" cmdConnect.Enabled = False End Sub Private Sub sckClient_DataArrival(ByVal bytesTotal As Long) Dim s As String sckClient.GetData s IstReceive.AddItem s End Sub Private Sub sckClient_Error(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) sckClient.Close cmdConnect.Enabled = True End Sub
服务器
Private MaxChan As Integer '最大通道数
Private Sub Command1_Click()
EndEnd Sub Private Sub Form_Load() Dim i As Integer MaxChan = 10 For i = 1 To MaxChan - 1 Load sckServer(i) Next i sckListen.LocalPort = "2012" sckListen.Listen End Sub Private Sub sckBusy_Close() sckBusy.Close End Sub Private Sub sckBusy_DataArrival(ByVal bytesTotal As Long) sckBusy.SendData "服务器忙,请稍后再连接!" DoEvents End Sub Private Sub sckListen_ConnectionRequest(ByVal requestID As Long) Dim i As Integer '决定由哪一Winsock接受请求 For i = 0 To MaxChan - 1 If sckServer(i).State = 0 Then Exit For End If Next i If sckServer(i).State = 0 Then sckServer(i).Accept requestID Exit Sub End If '如果所有Winsock都用完则由专门的"忙"Winsock接受请求,以免用户要求得不到响应 sckBusy.Close sckBusy.Accept requestID End Sub Private Sub sckListen_Error(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) sckListen.Close sckListen.LocalPort = 1000 sckListen.Listen End Sub Private Sub sckServer_Close(Index As Integer) sckServer(Index).Close End Sub Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim s As String Dim i As Integer sckServer(Index).GetData s If UCase(Left(Trim(s), 2)) = "PT" Then '判断是否为悄悄话,点对点方式 If IsNumeric(Mid(Trim(s), 3, 1)) Then i = Mid(Trim(s), 3, 1) sckServer(i).SendData "Channel " & Index & " " & Right(Trim(s), Len(Trim(s)) - 3) DoEvents End If Else '广播方式 For i = 0 To MaxChan - 1 '利用winsock的State属性给所有连接在服务器上的客户发消息 If sckServer(i).State = 7 Then sckServer(i).SendData "Channel " & Index & " " & Trim(s) DoEvents End If Next i End If lstReceive.AddItem "Channel " & Index & " " & Trim(s) End Sub Private Sub sckServer_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) sckServer(Index).Close
End Sub
客户端Private Sub cmdConnect_Click()
On Error GoTo ErrorPro
sckClient.RemoteHost = Text1.Text
sckClient.RemotePort = Text2.Text sckClient.Connect Exit SubErrorPro: MsgBox "服务器未开或网络出错!" End End Sub Private Sub cmdSent_Click() sckClient.SendData txtSent.Text End SubPrivate Sub Command1_Click()
End '断开连接
End SubPrivate Sub ip_Click()
Text1.Text = "211.83.114.28"
End SubPrivate Sub port_Click()
Text2.Text = "2012"End Sub Private Sub sckClient_Close() MsgBox "服务器通道已关闭!" End End Sub Private Sub sckClient_Connect() MsgBox "连接成功!" cmdConnect.Enabled = False End Sub Private Sub sckClient_DataArrival(ByVal bytesTotal As Long) Dim s As String sckClient.GetData s IstReceive.AddItem s End Sub Private Sub sckClient_Error(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) sckClient.Close cmdConnect.Enabled = True End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货