当前位置:编程学习 > VB >>

求个vb聊天程序源代码

答案:添加部件 Microsoft Winsock Control 6.0

服务端

Private Gac() As Boolean
Dim Socknumber As Integer
Private Sub Form_Load()
Winsock1(0).LocalPort = 1600
Winsock1(0).Listen
Socknumber = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
Winsock1(0).Close
End Sub

Private Sub Winsock1_Close(Index As Integer)
Winsock1(Index).Close
Unload Winsock1(Index)
Gac(Index) = False
Text3.Text = Int(Text3.Text) - 1
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Socknumber = Socknumber + 1

Load Winsock1(Socknumber)
Winsock1(Socknumber).Accept requestID
ReDim Preserve Gac(Socknumber)
Gac(Socknumber) = True
Text3.Text = Int(Text3.Text) + 1
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim c As String
Winsock1(Index).GetData c, vbString
Dim i As Integer
For i = 1 To UBound(Gac)
If Not i = Index Then
If Gac(i) Then
Winsock1(i).SendData c
DoEvents
End If
End If
Next i

Text1 = c
End Sub

客户端

Option Explicit
Private Sub Command1_Click()

Winsock1.Connect
Command1.Enabled = False
Do
DoEvents
Loop Until Winsock1.State = sckConnected Or Winsock1.State = sckError
If Winsock1.State = sckError Then
Command1.Enabled = True
Winsock1.Close
Text3.Text = "已与服务器连接失败" + Chr$(13) + Chr$(10) + Text3.Text
Else

Text3.Text = "已与服务器连接成功" + Chr$(13) + Chr$(10) + Text3.Text
Command2.Enabled = True
Command3.Enabled = True
End If
End Sub

Private Sub Command2_Click()

Winsock1.SendData Text1.Text
Text2.Text = "我说的话: " + Text1.Text + Chr$(13) + Chr$(10) + Text2.Text
Text1.Text = ""

End Sub

Private Sub Command3_Click()
Winsock1.Close
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = False
Text3.Text = "已与服务器断开" + Chr$(13) + Chr$(10) + Text3.Text
End Sub

Private Sub Form_Load()
Winsock1.RemoteHost = "192.168.0.23"
Winsock1.RemotePort = 1600
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = False

End Sub

Private Sub Winsock1_Close()
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = False
Winsock1.Close
Text3.Text = "已与服务器断开" + Chr$(13) + Chr$(10) + Text3.Text

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim c As String
Winsock1.GetData c, vbString
Text2.Text = "对方说的话: " + c + Chr$(13) + Chr$(10) + Text2.Text
End Sub

加一个WInsock1控件,两个文本框(txtMess、txtSend)和一个按钮,代码如下

Private Sub Command1_Click()
Winsock1.SendData txtSend.Text '发送聊天内容
txtSend.Text = "" '聊天内容清空
End Sub

Private Sub Form_Load()
Dim my As String
Winsock1.Protocol = sckUDPProtocol '设置为UDP协义
Winsock1.LocalPort = 60 '随便设置,不过两个要一样
Winsock1.RemotePort = 60
my = Winsock1.LocalIP '取得自己的IP地址,是不要的
Winsock1.RemoteHost = my '这里应该是改为别人计算机的IP,由于自己是单机,所以就设置成自己的,自己给自己发信息,可以拿另一台主机试试

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) '取到数据时发生
Dim Tmp As String
Winsock1.GetData Tmp, vbString '取得数据
Tmp = "对方IP地址:" + Winsock1.RemoteHostIP + "发送内容为-->" + Tmp
txtMess.Text = Tmp + Chr$(13) + Chr$(10) + txtMess.Text '换行输出聊天信息
End Sub

语句超简单,我用了几小时才弄出。

上一个:VB中的OLE控件怎么使用?
下一个:用VB来做EXCEL

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,