所以,我试图做一个非常简单的系统从客户端发送信息到服务器(从服务器到客户端,后来为好,但第一个孩子步骤)。 我不知道究竟如何使用UDPClient来发送和接收消息(特别是接待他们),主要是因为我没有什么触发ReceiveMessage()
函数,我不知道会是什么。
源代码是在这个环节,转到文件>下载。 如果你想只运行exe它已经建立。
所以我的问题基本上是:我如何可以轻松地使用UDPClient
,我怎么能得到这个系统的工作,都有些什么提示执行这种连接? 什么我应该注意(线程,问题与代码等)?
源 。
您需要首先需要建立两个UdpClient
秒。 对于听一个客户端,另一个用于发送数据。 (你还需要选择一个自由/未使用的端口号,知道你的目标的IP地址-您想将数据发送到机器)
要设置接收器,
实例化UdpClient
与您之前选择的端口号变量,
创建一个新的线程,以避免同时接收数据阻塞,
遍历所有的客户端的接收方法,只要你想接收数据(循环的执行应该是新的线程中),
当您收到一个大量的数据(称为“包”),你可能需要将字节数组转换成更有意义的事情,
创建一个方法,当你要完成接收数据退出循环。
要设置发件人,
实例化UdpClient
变量与您之前选择的端口号(您可能要启用地发送广播包的能力。这允许您将数据发送到您的局域网上的所有听众)
当需要传送数据,将数据转换为字节数组,然后调用Send()
我建议你有一个快速脱脂通读此 。
下面是一些代码,让你开始了......
'''''''''''''''''''''''Set up variables''''''''''''''''''''
Private Const port As Integer = 9653 'Port number to send/recieve data on
Private Const broadcastAddress As String = "255.255.255.255" 'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client
Private receivingClient As UdpClient 'Client for handling incoming data
Private sendingClient As UdpClient 'Client for sending data
Private receivingThread As Thread 'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
Private closing As Boolean = False 'Used to close clients if form is closing
''''''''''''''''''''Initialize listening & sending subs'''''''''''''''''
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
InitializeSender() 'Initializes startup of sender client
InitializeReceiver() 'Starts listening for incoming data
End Sub
''''''''''''''''''''Setup sender client'''''''''''''''''
Private Sub InitializeSender()
sendingClient = New UdpClient(broadcastAddress, port)
sendingClient.EnableBroadcast = True
End Sub
'''''''''''''''''''''Setup receiving client'''''''''''''
Private Sub InitializeReceiver()
receivingClient = New UdpClient(port)
Dim start As ThreadStart = New ThreadStart(AddressOf Receiver)
receivingThread = New Thread(start)
receivingThread.IsBackground = True
receivingThread.Start()
End Sub
'''''''''''''''''''Send data if send button is clicked'''''''''''''''''''
Private Sub sendBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendBut.Click
Dim toSend As String = tbSend.Text 'tbSend is a textbox, replace it with whatever you want to send as a string
Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)'Convert string to bytes
sendingClient.Send(data, data.Length) 'Send bytes
End Sub
'''''''''''''''''''''Start receiving loop'''''''''''''''''''''''
Private Sub Receiver()
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port) 'Listen for incoming data from any IP address on the specified port (I personally select 9653)
While (True) 'Setup an infinite loop
Dim data() As Byte 'Buffer for storing incoming bytes
data = receivingClient.Receive(endPoint) 'Receive incoming bytes
Dim message As String = Encoding.ASCII.GetString(data) 'Convert bytes back to string
If closing = True Then 'Exit sub if form is closing
Exit Sub
End If
End While
End Sub
'''''''''''''''''''Close clients if form closes''''''''''''''''''
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
closing = True 'Tells receiving loop to close
receivingClient.Close()
sendingClient.Close()
End Sub
这里有一些其他exmples: 这里 , 这里 , 这里和这里 。
Imports System.Threading
Shared client As UdpClient
Shared receivePoint As IPEndPoint
client = New UdpClient(2828) 'Port
receivePoint = New IPEndPoint(New IPAddress(0), 0)
Dim readThread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))
readThread.Start()
Public Shared Sub WaitForPackets()
While True
Dim data As Byte() = client.Receive(receivePoint)
Console.WriteLine("=" + System.Text.Encoding.ASCII.GetString(data))
End While
End Sub