VB怎么实现局域网聊天的?

2020-08-27 社会 59阅读
1、winsock 部件 2、客务端
(客务端): 监听 -->接受请求 -->发送数据 -->关闭连接
Private Sub Form_Load()
Winsock1.LocalPort = 5238'这个是端口
Winsock1.Listen
Text1.Locked = True
Text2.Locked = True
Text2.Text = "等待客户登陆!"
End SubPrivate Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End SubPrivate Sub Text1_Change()
Winsock1.SendData Text1.Text
End SubPrivate Sub Winsock1_Close()
Winsock1.Close
Text2.Text = "等待客户登陆!"
Winsock1.LocalPort = 5238
Winsock1.Listen
End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
Text2.Text = "客户已登陆!"
Text1.Locked = False
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim str As String
Winsock1.GetData str, vbString
Text2.Text = str
End Sub
3、客户端
(户务端): 监听 -->接受请求 -->发送数据 -->关闭连接
Private Sub Command1_Click()
Winsock1.Close
Winsock1.RemoteHost = Text3.Text'这是IP。
Winsock1.RemotePort = 5238 '这是端口。。
Text2.Text = "链接户务器出错,请重新配置!"
On Error GoTo herr
Winsock1.Connect
Exit Sub
herr:
Winsock1.Close
Text2.Text = "链接户务器出错,请重新配置!"
Exit Sub
End SubPrivate Sub Form_Load()
Text1.Locked = True
Text2.Locked = True
End SubPrivate Sub Text1_Change()
Winsock1.SendData Text1.Text
End SubPrivate Sub Winsock1_Close()
'断开链接
Winsock1.Close
End Sub
Private Sub Winsock1_Connect()
'发送用户信息
Text2.Text = "链接成功!"
Text1.Locked = False
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim str As String
Winsock1.GetData str, vbString
Text2.Text = str
End Sub
看,客户端的端口和户务端的端口是一样的,这样就达到了一个协议,
客户端的IP就是连接户务端的主机IP。。。这样就搭到了连接
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com