Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class Form1
Private Chat As Chat
Private thread As Thread
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
thread.Abort()
Chat.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'vs05在新加的功能窗体控件的线程安全,即只有创建控件的线程才可以对线程操作。
'但是很多时候我们需要跨线程操作例如本例,下面是最简单的处理方法。
CheckForIllegalCrossThreadCalls = False '允许跨线程操作窗体
'由于本人是用一台机器测试的所以可以自己发送自己接收,如果两台用两台机器做可以通过其它途径设置IP和端口
Chat = New Chat("8000", "127.0.0.1", AddressOf receive)
AddHandler Chat.GetMessageEvent, AddressOf receive
'启动线程侦听接收信息
thread = New Thread(AddressOf Chat.Receive)
thread.Start()
End Sub
Private Sub send(ByVal message As String)
Chat.Send(message)
'MessageBox.Show(message)
End Sub
Private Sub receive(ByVal message As String)
'将收到的信息显示出来,仅此而以
RTB.AppendText(message)
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'读者可以通过一个文本框来输入要发送的信息,这里本人懒得写。
Chat.Send("aaa" + Chr(13))
End Sub
End Class
Class Chat
Private m_Port As String '端口
Private m_IP As String 'IP
Private m_UDPClient As UdpClient
Private m_RIEP As IPEndPoint '终端
Private m_ReFunction As ReceiveMessage '委托变量
#Region "属性"
''' <summary>
''' 对方端口
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Port() As String
Get
Return m_Port
End Get
Set(ByVal value As String)
m_Port = value
End Set
End Property
''' <summary>
''' 对方IP
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property IP() As String
Get
Return m_IP
End Get
Set(ByVal value As String)
m_IP = value
End Set
End Property
''' <summary>
''' 委托方法
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ReFunction() As ReceiveMessage
Get
Return m_ReFunction
End Get
Set(ByVal value As ReceiveMessage)
m_ReFunction = value
End Set
End Property
#End Region
#Region "事件委托"
'通过事件和委托两种方法实现 信息的接收
Public Event GetMessageEvent(ByVal Message As String)
Public Delegate Sub ReceiveMessage(ByVal message As String)
#End Region
#Region "构造函数"
Sub New(ByVal f As ReceiveMessage)
m_Port = "8000"
m_IP = "127.0.0.1"
m_ReFunction = f
m_RIEP = New IPEndPoint(IPAddress.Parse(m_IP), Convert.ToInt32(m_Port))
m_UDPClient = New UdpClient(m_RIEP)
End Sub
Sub New(ByVal p As String, ByVal i As String, ByVal f As ReceiveMessage)
m_Port = p
m_IP = i
m_ReFunction = f
m_RIEP = New IPEndPoint(IPAddress.Parse(m_IP), Convert.ToInt32(m_Port))
m_UDPClient = New UdpClient(m_RIEP)
End Sub
#End Region
#Region "方法"
''' <summary>
''' 发送消息
''' </summary>
''' <param name="message"></param>
''' <remarks></remarks>
Public Sub Send(ByVal message As String)
Dim sendBytes() As Byte = System.Text.Encoding.BigEndianUnicode.GetBytes(message.ToCharArray())
Try
Connect()
m_UDPClient.Send(sendBytes, sendBytes.Length)
Catch ex As Exception
Throw ex
End Try
End Sub
''' <summary>
''' 接收信息
''' </summary>
''' <remarks></remarks>
Public Sub Receive()
Dim selfIEP As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
While True
Dim receiveBytes() As Byte
Try
receiveBytes = m_UDPClient.Receive(selfIEP)
Catch ex As Exception
Throw ex
End Try
Dim receiveStr As String = System.Text.Encoding.BigEndianUnicode.GetString(receiveBytes)
m_ReFunction(receiveStr)
'触发事件 出可以激活下面的方法,但是要隐掉上一句
'RaiseEvent GetMessageEvent(receiveStr)
End While
End Sub
''' <summary>
''' 连接对方
''' </summary>
''' <remarks></remarks>
Public Sub Connect()
Try
m_UDPClient.Connect(m_RIEP)
Catch ex As Exception
Throw ex
End Try
End Sub
''' <summary>
''' 关闭连接
''' </summary>
''' <remarks></remarks>
Public Sub Close()
m_UDPClient.Close()
m_UDPClient = Nothing
End Sub
#End Region
End Class
在群43255191中有源文件可以下载,功能和界面将会完善。并实现聊天室功能。