求个vb聊天程序源代码
发布网友
发布时间:2022-05-10 01:56
我来回答
共2个回答
热心网友
时间:2023-10-18 07:49
加一个WInsock1控件,两个文本框(txtMess、txtSend)和一个按钮,代码如下Private Sub Command1_Click()
Winsock1.SendData txtSend.Text '发送聊天内容
txtSend.Text = "" '聊天内容清空
End SubPrivate 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 SubPrivate 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语句超简单,我用了几小时才弄出。
热心网友
时间:2023-10-18 07:49
添加部件 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 SubPrivate Sub Form_Unload(Cancel As Integer)
Winsock1(0).Close
End SubPrivate Sub Winsock1_Close(Index As Integer)
Winsock1(Index).Close
Unload Winsock1(Index)
Gac(Index) = False
Text3.Text = Int(Text3.Text) - 1
End SubPrivate 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 SubPrivate 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 SubPrivate Sub Command2_Click() Winsock1.SendData Text1.Text
Text2.Text = "我说的话: " + Text1.Text + Chr$(13) + Chr$(10) + Text2.Text
Text1.Text = ""End SubPrivate Sub Command3_Click()
Winsock1.Close
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = False
Text3.Text = "已与服务器断开" + Chr$(13) + Chr$(10) + Text3.Text
End SubPrivate Sub Form_Load()
Winsock1.RemoteHost = "192.168.0.23"
Winsock1.RemotePort = 1600
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = False
End SubPrivate Sub Winsock1_Close()
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = False
Winsock1.Close
Text3.Text = "已与服务器断开" + Chr$(13) + Chr$(10) + Text3.Text
End SubPrivate 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