C# 通過socket實現UDP 通訊
阿新 • • 發佈:2019-01-23
UDP不屬於面向連線的通訊,在選擇使用協議的時候,選擇UDP必須要謹慎。在網路質量令人十分不滿意的環境下,UDP協議資料包丟失會比較嚴重。但是由於UDP的特性:它不屬於連線型協議,因而具有資源消耗小,處理速度快的優點,所以通常音訊、視訊和普通資料在傳送時使用UDP較多,因為它們即使偶爾丟失一兩個資料包,也不會對接收結果產生太大影響。比如我們聊天用的ICQ和QQ就是使用的UDP協議。 我們通過UDP進行資訊收發的時候,沒有嚴格客戶端和服務端的區別,它不同於UDP,UDP 必須建立可靠連線之後才可以通訊,而UDP隨時都可以給指定的ip和埠所對應程序傳送訊息。UDP傳送訊息時需要繫結自己IP 和 埠號,接收訊息的時候沒有特殊限制,只要有人給自己傳送,自己線上,就可以接收。
接下來我們通過一個簡單的程式看一下UDP通訊的過程。
服務端程式:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- namespace UDP_Server
- {
- class
- {
- static Socket server;
- staticvoid Main(string[] args)
- {
- server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- server.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001));//繫結埠號和IP
- Console.WriteLine("服務端已經開啟"
- Thread t = new Thread(ReciveMsg);//開啟接收訊息執行緒
- t.Start();
- Thread t2 = new Thread(sendMsg);//開啟發送訊息執行緒
- t2.Start();
- }
- /// <summary>
- /// 向特定ip的主機的埠傳送資料報
- /// </summary>
- staticvoid sendMsg()
- {
- EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000);
- while (true)
- {
- string msg = Console.ReadLine();
- server.SendTo(Encoding.UTF8.GetBytes(msg), point);
- }
- }
- /// <summary>
- /// 接收發送給本機ip對應埠號的資料報
- /// </summary>
- staticvoid ReciveMsg()
- {
- while (true)
- {
- EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用來儲存傳送方的ip和埠號
- byte[] buffer = newbyte[1024];
- int length = server.ReceiveFrom(buffer, ref point);//接收資料報
- string message = Encoding.UTF8.GetString(buffer,0,length);
- Console.WriteLine(point.ToString()+ message);
- }
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace UDP_Server
{
class Program
{
static Socket server;
static void Main(string[] args)
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
server.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001));//繫結埠號和IP
Console.WriteLine("服務端已經開啟");
Thread t = new Thread(ReciveMsg);//開啟接收訊息執行緒
t.Start();
Thread t2 = new Thread(sendMsg);//開啟發送訊息執行緒
t2.Start();
}
/// <summary>
/// 向特定ip的主機的埠傳送資料報
/// </summary>
static void sendMsg()
{
EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000);
while (true)
{
string msg = Console.ReadLine();
server.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
}
/// <summary>
/// 接收發送給本機ip對應埠號的資料報
/// </summary>
static void ReciveMsg()
{
while (true)
{
EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用來儲存傳送方的ip和埠號
byte[] buffer = new byte[1024];
int length = server.ReceiveFrom(buffer, ref point);//接收資料報
string message = Encoding.UTF8.GetString(buffer,0,length);
Console.WriteLine(point.ToString()+ message);
}
}
}
}
客戶端程式:
[csharp] view plain copy print?- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- namespace UDP_client
- {
- class Program
- {
- static Socket client;
- staticvoid Main(string[] args)
- {
- client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- client.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000));
- Thread t = new Thread(sendMsg);
- t.Start();
- Thread t2 = new Thread(ReciveMsg);
- t2.Start();
- Console.WriteLine("客戶端已經開啟");
- }
- /// <summary>
- /// 向特定ip的主機的埠傳送資料報
- /// </summary>
- staticvoid sendMsg()
- {
- EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001);
- while(true){
- string msg = Console.ReadLine();
- client.SendTo(Encoding.UTF8.GetBytes(msg), point);
- }
- }
- /// <summary>
- /// 接收發送給本機ip對應埠號的資料報
- /// </summary>
- staticvoid ReciveMsg()
- {
- while (true)
- {
- EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用來儲存傳送方的ip和埠號
- byte[] buffer = newbyte[1024];
- int length = client.ReceiveFrom(buffer, ref point);//接收資料報
- string message = Encoding.UTF8.GetString(buffer, 0, length);
- Console.WriteLine(point.ToString() + message);
- }
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UDP_client
{
class Program
{
static Socket client;
static void Main(string[] args)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000));
Thread t = new Thread(sendMsg);
t.Start();
Thread t2 = new Thread(ReciveMsg);
t2.Start();
Console.WriteLine("客戶端已經開啟");
}
/// <summary>
/// 向特定ip的主機的埠傳送資料報
/// </summary>
static void sendMsg()
{
EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001);
while(true){
string msg = Console.ReadLine();
client.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
}
/// <summary>
/// 接收發送給本機ip對應埠號的資料報
/// </summary>
static void ReciveMsg()
{
while (true)
{
EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用來儲存傳送方的ip和埠號
byte[] buffer = new byte[1024];
int length = client.ReceiveFrom(buffer, ref point);//接收資料報
string message = Encoding.UTF8.GetString(buffer, 0, length);
Console.WriteLine(point.ToString() + message);
}
}
}
}
執行效果圖: