一、UDP協議概述
UDP(User Datagram Protocol,用戶數據報協議)是一種無連接的協議,數據報文的大小在IP協議層被限制為65,507位元組。UDP不保證數據的可靠傳輸,沒有重傳機制,而且無序且不重複,比TCP更加輕量級,通常用於實時的應用場景,如遊戲,視頻等。
二、c#udp實現
1、發送數據
c#中使用UdpClient實現UDP協議的發送數據,示例代碼如下:
using System.Net.Sockets; using System.Text; class SendData { static void Main(string[] args) { //定義IP和端口 string ip = "127.0.0.1"; int port = 8888; //定義發送的數據 string message = "Hello World!"; byte[] sendBytes = Encoding.ASCII.GetBytes(message); //定義UDP客戶端 UdpClient client = new UdpClient(); //發送數據 client.Send(sendBytes, sendBytes.Length, ip, port); //關閉UDP客戶端 client.Close(); } }
2、接收數據
c#中使用UdpClient實現UDP協議的接收數據,示例代碼如下:
using System.Net; using System.Net.Sockets; using System.Text; class ReceiveData { static void Main(string[] args) { //定義IP和端口 string ip = "127.0.0.1"; int port = 8888; //定義UDP客戶端 UdpClient client = new UdpClient(port); //定義接收數據的IP和端口 IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(ip), port); while (true) { //接收數據 byte[] receiveBytes = client.Receive(ref remoteIpep); string message = Encoding.ASCII.GetString(receiveBytes, 0, receiveBytes.Length); Console.WriteLine("接收到的數據為:" + message); } //關閉UDP客戶端 client.Close(); } }
三、小標題
1、c#udp實現視頻傳輸
UDP適用於實時的應用,如實時視頻傳輸。c#可以結合FFmpeg庫實現視頻的採集和傳輸,示例代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FFmpeg.AutoGen; using System.Runtime.InteropServices; using System.Net.Sockets; using System.Threading; using System.Net; namespace UdpVideoStreaming { class Program { static void Main(string[] args) { int videoStreamIndex = -1; //初始化FFmpeg ffmpeg.av_register_all(); ffmpeg.avcodec_register_all(); ffmpeg.avformat_network_init(); //打開視頻 AVFormatContext* pFormatContext = null; if (ffmpeg.avformat_open_input(&pFormatContext, "test.mp4", null, null) != 0) { Console.WriteLine("無法打開視頻文件"); return; } //查找視頻流 if (ffmpeg.avformat_find_stream_info(pFormatContext, null) < 0) { Console.WriteLine("無法查找視頻流"); return; } Codecs.CodecContext videoCodecContext = new Codecs.CodecContext(); videoStreamIndex = ffmpeg.av_find_best_stream(pFormatContext, AVMediaType.AVMEDIA_TYPE_VIDEO, -1, -1,videoCodecContext.Codec, 0); if (videoStreamIndex == -1) { Console.WriteLine("無法找到視頻流"); return; } //打開視頻解碼器 if (ffmpeg.avcodec_open2(videoCodecContext, videoCodecContext.Codec, null) = 0) { if (pPacket->stream_index == videoStreamIndex) { //發送視頻數據 int length = 0; byte[] data = new byte[pPacket->size]; Marshal.Copy(new IntPtr(pPacket->data), data, 0, pPacket->size); client.Send(data, data.Length, remoteIpep); //延時 Thread.Sleep(40); } ffmpeg.av_packet_unref(pPacket); } ffmpeg.av_packet_free(&pPacket); ffmpeg.avcodec_close(videoCodecContext); ffmpeg.avformat_close_input(&pFormatContext); } } }
2、c#udp實現多人遊戲
UDP適用於實時的應用,如多人網絡遊戲。c#可以使用Unity引擎結合UDP協議實現多人遊戲,示例代碼如下:
using UnityEngine; using System.Collections; using System.Net.Sockets; using System.Net; using System.Threading; public class UdpController : MonoBehaviour { private UdpClient udpClient; private Thread receiveThread; private void Start() { //定義接收數據的IP和端口 IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 8888); //定義UDP客戶端 udpClient = new UdpClient(remoteIpep); //開啟接收數據的線程 receiveThread = new Thread(new ThreadStart(ReceiveData)); receiveThread.Start(); } private void OnApplicationQuit() { receiveThread.Abort(); udpClient.Close(); } private void ReceiveData() { while (true) { byte[] receiveBytes = udpClient.Receive(ref remoteIpep); string message = Encoding.ASCII.GetString(receiveBytes, 0, receiveBytes.Length); Debug.Log("接收到的數據為:" + message); } } private void SendData() { //定義發送的數據 string message = "Hello World!"; byte[] sendBytes = Encoding.ASCII.GetBytes(message); //定義發送的IP和端口 IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888); //發送數據 udpClient.Send(sendBytes, sendBytes.Length, remoteIpep); } }
3、c#udp與TCP的區別
TCP為面向連接的協議,具有可靠性高的特點,可以保證數據的可靠傳輸。而UDP為無連接的協議,不保證數據的可靠傳輸,但是更輕量級。在網絡遊戲,實時視頻傳輸等實時應用場景中,使用UDP能夠保證更好的實時性。
原創文章,作者:NAMYB,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/329188.html