接4楼,接下来是关于客户端的
客户端这里我是设计了两个窗口,一个是登录窗口,还有一个是会话窗口
登录窗口所需的控件:
1.Label控件3个(不用命名,只是起标识作用的)
2.Text控件3个(分别被命名为:username,hostip,hostport)
控件2个(分别被命名为:cmd_connect,cmd_exit)
在cmd_connect中写下如下代码:
程序代码:
Private Sub cmd_connect_Click()
If (IsNumeric(hostport.Text) = False) Then
Call MsgBox("请输入正确的端口号", vbExclamation, "Alert")
hostport.SetFocus
Exit Sub
End If
username.Locked = True
cmd_connect.Caption = "登录中"
cmd_connect.Enabled = False
Load Client '载入会话窗口
End Sub
然后退出的方面应该不用我写了吧...
登录窗口界面参考:
会话窗口所需控件:
1.list控件1个(命名为report)
2.text控件1个(命名为message)
4.Winsock控件1个(命名为ClientLink)
5.Timer控件2个(命名为linktest,maxtime)
首先定义两个全局变量:
Public nickname$
Dim shut%
然后在Form中写入如下代码:
程序代码:
Private Sub Form_Load()
shut = 0 '异常终止标识符
nickname = "" '清除原先昵称
ClientLink.RemoteHost = ClientLogin.hostip.Text
ClientLink.RemotePort = ClientLogin.hostport.Text
ClientLink.Connect
End Sub
Private Sub Form_Unload(Cancel As Integer)
ClientLogin.cmd_connect.Caption = "链接"
ClientLogin.cmd_connect.Enabled = True
ClientLogin.username.Locked = False
If (shut = 1) Then '如果异常中断标识符生效则不执行接下来的步骤
Exit Sub
End If
Dim ret%
ret = MsgBox("您确定要断开连接?", 4 + vbQuestion + 256, "Continue?")
If (ret = 6) Then
ClientLink.SendData "[logout]|0000"
DoEvents
ClientLink.Close
ClientLogin.Show
Exit Sub
End If
Cancel = 1
End Sub
然后是ClientLink的相关代码:
程序代码:
Private Sub ClientLink_DataArrival(ByVal bytesTotal As Long)
Dim data0$, place%, datatype$, data1$
ClientLink.GetData data0
place = InStr(data0, "|")
datatype = Left(data0, place - 1)
data1 = Right(data0, Len(data0) - place)
Select Case (datatype)
Case "[linkmax]" '接收到[服务器已满]时执行的操作
linktest.Enabled = False
ClientLink.Close
shut = 1
Unload Me
If (ClientLogin.Visible = False) Then
ClientLogin.Show
End If
Call MsgBox("服务器已满", vbExclamation, "Tip")
Case "[server]"
report.AddItem "[Server]:" & data1
Case "[serverclose]" '接收到[服务器已关闭]时的操作
linktest.Enabled = False
ClientLink.Close
shut = 1
Unload Me
If (ClientLogin.Visible = False) Then
ClientLogin.Show
End If
Call MsgBox("服务器已关闭", vbExclamation, "Tip")
Case "[accept]" '接收到[允许连接]时执行的操作
nickname = ClientLogin.username.Text '获取用户昵称
Client.Caption = "客户端[当前用户:" & nickname & "]"
ClientLink.SendData "[nickname]|" & nickname
maxtime.Enabled = False '停止连接超时检测
report.AddItem "登录成功"
report.AddItem "本机号码:" & ClientLink.LocalHostName
report.AddItem "本机IP地址:" & ClientLink.LocalIP
report.AddItem "本机端口:" & ClientLink.LocalPort
ClientLogin.username.Text = ""
ClientLogin.cmd_connect.Enabled = True
ClientLogin.Hide
Client.Show
Case "[message]"
report.AddItem data1
End Select
End Sub
Private Sub ClientLink_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)
maxtime.Enabled = False
If (Number = 10061) Then
Call MsgBox("服务器未开启", vbExclamation, "Tip")
ElseIf (Number = 10065) Then
Call MsgBox("没有网络连接.", vbExclamation, "Tip")
Else
Call MsgBox("抱歉,连接失败." & Chr(10) & "错误编号:" & Number & Chr(10) & "错误描述:" & Description, vbExclamation, "Alert")
End If
ClientLink.Close
shut = 1
Unload Me
End Sub
然后是超时检测和状态检测
超时检测的Timer的Interval设置为5000
程序代码:
Private Sub maxtime_Timer()
maxtime.Enabled = False
shut = 1
Unload Me
Call MsgBox("链接超时", vbExclamation, "Alert")
End Sub
状态检测的Timer的Interval设置为1000
程序代码:
Private Sub linktest_Timer()
If (ClientLink.State = 8) Then
linktest.Enabled = False
ClientLink.Close
ClientLogin.Show
shut = 1
Unload Me
Call MsgBox("与服务器断开连接", vbExclamation, "Alert")
Exit Sub
End If
End Sub
最后是发送消息的这部分
程序代码:
Private Sub message_KeyPress(KeyAscii As Integer)
If (KeyAscii <> 13) Then
Exit Sub
ElseIf (message.Text = "") Then
KeyAscii = 0
Exit Sub
End If
If (ClientLink.State = 7) Then
report.AddItem "[You]:" & MessageBox.Text
ClientLink.SendData "[message]|[" & nickname & "]:" & MessageBox.Text '发送数据(联系服务端的数据处理)
End If
message.Text = ""
KeyAscii = 0 '取消回车所造成的'登'的声音
End Sub
对了,还有服务端的发送消息部分
程序代码:
Private Sub Command_KeyPress(KeyAscii As Integer)
If (KeyAscii <> 13) Then
Exit Sub
End If
Dim cmd$(), data_send$, num%
data_send = "[server]|" & Command.Text
For num = 1 To linknow
If (ServerLink(num).State = 7) Then
ServerLink(num).SendData data_send
DoEvents
End If
Next num
report.AddItem "[Server]:" & Command.Text
Command.Text = ""
KeyAscii = 0
End Sub
差不多就是这样了...
参考的客户端界面图:
PS:
因为原程序已经被我魔改的加入了一堆乱七八糟的东西,所以以上内容是我删删改改的结果,有概率存在bug,然后也因为这个原因我就不贴原程序了...
[此贴子已经被作者于2018-11-4 19:04编辑过]