- 服务端:
- '用vb.net做一个简单的聊天工具,可以实现群发的
- Imports System.Threading
- Imports System.Net
- Imports System.Net.Sockets
- Public Class Form1
- Dim ListenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '创建监听的套接字
- Dim Dict As New Dictionary(Of String, Socket) '用于保存连接的客户的套接字的键值对集合
- Dim DictThre As New Dictionary(Of String, Thread) '用于保存通信线程的键值对集合
- Private Sub ButListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButListen.Click '开始监听的按钮
- TextBox.CheckForIllegalCrossThreadCalls = False '取消文本框的跨线程检查
- Dim Address As IPAddress = IPAddress.Parse(TxtIP.Text.Trim)
- Dim EndPoint As New IPEndPoint(Address, TxtPort.Text) '创建一个网络节点对象
- ListenSocket.Bind(EndPoint) '给负责监听的套接字绑定一个网络节点
- ListenSocket.Listen(2)
- ShowMsg("正在监听……")
- Dim thre As New Thread(AddressOf Connect) '创建一个新的线程用于处理客户端发来的连接请求
- thre.IsBackground = True '设为后台线程
- thre.Start() '开启线程
- End Sub
- Sub Connect() '处理客户端的连接请求的过程
- While True
- Dim SockConect As Socket = ListenSocket.Accept
- ShowMsg("连接成功!" & "(连接信息:" & SockConect.RemoteEndPoint.ToString & ")")
- Dict.Add(SockConect.RemoteEndPoint.ToString, SockConect) '将连接成功的套接字添加到键值对集合
- LBOnLine.Items.Add(SockConect.RemoteEndPoint.ToString) '添加到列表
- Dim Thre As New Thread(AddressOf RecClient) '创建一个新的线程用于和链接成功的套接字通信
- Thre.IsBackground = True '设为后台线程
- Thre.Start(SockConect)
- DictThre.Add(SockConect.RemoteEndPoint.ToString, Thre) '将创建的通信线程添加到键值对集合
- End While
- End Sub
- Sub RecClient(ByVal SockTelNet As Socket) '处理客户端发来的数据
- While True
- Dim AryMsg(1024) As Byte
- Dim RecLen As Int32
- Try '捕获异常
- RecLen = SockTelNet.Receive(AryMsg) '接受客户端发来得信息
- Catch ss As SocketException
- ShowMsg(ss.NativeErrorCode & vbCrLf & ss.Message) '显示错误信息
- Dict.Remove(SockTelNet.RemoteEndPoint.ToString) '移除断开连接的套接字
- LBOnLine.Items.Remove(SockTelNet.RemoteEndPoint.ToString) '从列表中移除
- Return
- Catch s As Exception
- ShowMsg(s.Message)
- Return
- End Try
- Dim StrMsg As String
- StrMsg = System.Text.Encoding.UTF8.GetChars(AryMsg, 0, RecLen)
- ShowMsg("接收:" & StrMsg)
- End While
- End Sub
- Sub ShowMsg(ByVal Msg As String) '显示信息
- TxtMsg.AppendText(Format(DateTime.Now, "F") & vbCrLf & " " & Msg & vbCrLf & vbCrLf) 'TxtMsg用途显示消息记录
- End Sub
- Sub SendMsg(ByVal Msg As String) '发送信息
- Dim AryMsg() = System.Text.Encoding.UTF8.GetBytes(Msg)
- Try '捕获异常
- Dict(LBOnLine.SelectedItem).Send(AryMsg) '给选中用户发送消息
- Catch ss As SocketException
- ShowMsg(ss.NativeErrorCode & vbCrLf & ss.Message)
- Return
- Catch s As Exception
- ShowMsg(s.Message)
- Return
- End Try
- ShowMsg("发送:" & Msg)
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Button1发送消息的按钮
- SendMsg(TxtSendMsg.Text) '发送消息
- End Sub
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Button2群发的按钮
- For Each a In Dict.Values '遍历通信套接字键值对集合,群发消息
- Dim AryMsg() = System.Text.Encoding.UTF8.GetBytes(TxtSendMsg.Text.Trim)
- Try '捕获异常
- a.Send(AryMsg) '发送消息
- Catch ss As SocketException
- ShowMsg(ss.NativeErrorCode & vbCrLf & ss.Message)
- Catch s As Exception
- ShowMsg(s.Message)
- End Try
- Next
- ShowMsg("群发:" & TxtSendMsg.Text.Trim)
- End Sub
- End Class
- 客户端:
- 客户端和服务端发送和接收数据的处理基本一样的,只是一个是主动连接的,一个是被动连接的
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.Threading
- Public Class Form1
- Dim SockClient As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '穿件一个
- Private Sub ButListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButListen.Click
- TextBox.CheckForIllegalCrossThreadCalls = False '取消文本框的跨线程检查
- Dim Address As IPAddress = IPAddress.Parse(TxtIP.Text.Trim)
- Dim Endpoint As New IPEndPoint(Address, TxtPort.Text)
- Try '捕获异常
- SockClient.Connect(Endpoint) '发起连接
- Catch ss As SocketException
- ShowMsg(ss.Message)
- Return
- Catch s As Exception
- ShowMsg(s.Message)
- Return
- End Try
- ShowMsg("连接成功!")
- Dim Thre As New Thread(AddressOf RecMsg) '创建接收数据的线程
- Thre.IsBackground = True
- Thre.Start()
- End Sub
- Sub RecMsg() '接受数据处理
- While True
- Dim AryMsg(1024) As Byte
- Dim RecLen As Int32
- Try '捕获异常
- RecLen = SockClient.Receive(AryMsg)
- Catch ss As SocketException
- ShowMsg(ss.Message)
- Return
- Catch s As Exception
- ShowMsg(s.Message)
- Return
- End Try
- Dim StrMsg As String
- StrMsg = System.Text.Encoding.UTF8.GetChars(AryMsg, 0, RecLen)
- ShowMsg("接收:" & StrMsg)
- End While
- End Sub
- Sub ShowMsg(ByVal Msg As String) '显示信息
- TxtMsg.AppendText(Format(DateTime.Now, "F") & vbCrLf & Msg & vbCrLf)
- End Sub
- Private Sub ButSendMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButSendMsg.Click
- SendMsg(TxtSendMsg.Text.Trim)
- End Sub
- Sub SendMsg(ByVal Msg As String) '发送数据
- Dim AryMsg() = System.Text.Encoding.UTF8.GetBytes(Msg)
- Try '捕获异常
- SockClient.Send(AryMsg)
- Catch ss As SocketException
- ShowMsg(ss.Message)
- Return
- Catch s As Exception
- ShowMsg(s.Message)
- Return
- End Try
- ShowMsg("发送:" & Msg)
- End Sub
- End Class
- 这个简单的例子就到这了
复制代码 |