C# 通過socket實現UDP 通訊
本文章已收錄於:
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 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);
- }
- }
- }
- }
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);
}
}
}
}
客戶端程式:
- 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);
- }
- }
- }
- }
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);
}
}
}
}
執行效果圖:
相關文章
- 通過 Socket 實現 UDP 程式設計 入門UDP程式設計
- java實現UDP通訊JavaUDP
- c#實現最簡單的socket通訊C#
- C# 實現socket通訊程式(伺服器端)C#伺服器
- C# 如何實現簡單的Socket通訊(附示例)C#
- UDP協議網路Socket程式設計(java實現C/S通訊案例)UDP協議程式設計Java
- socket通訊
- 通過socket實現DUP程式設計程式設計
- C# wm6透過udp協議和pc通訊C#UDP協議
- 使用Python語言通過PyQt5和socket實現UDP伺服器PythonQTUDP伺服器
- Socket,TCP,UDP,HTTP基本通訊原理和OC版本DemoTCPUDPHTTP
- C# NModbus RTU通訊實現C#
- iOS基於Socket.io即時通訊IM實現,WebRTC實現視訊通話iOSWeb
- unix socket通訊
- SOCKET通訊中TCP、UDP資料包大小的確定TCPUDP
- udp網路通訊UDP
- Python - UDP通訊PythonUDP
- ACE中UDP通訊UDP
- UDP雙向通訊UDP
- 通過 App Groups 實現程式間通訊APP
- 通過 Socket 實現 TCP 程式設計入門TCP程式設計
- C# UDP通訊 ReceiveAsync() 一直等待問題C#UDP
- 網路通訊1:UDPUDP
- 程式間通訊(Socket)
- socket.IO通訊
- Android Socket 通訊Android
- socket通訊的建立
- php的socket通訊PHP
- 【Python】socket通訊Python
- TCP和UDP實現簡單一對一通訊TCPUDP
- Java的通過管道來實現執行緒通訊Java執行緒
- Extjs 通過 Ext.Direct 實現與 Laravel 通訊JSLaravel
- Thinking in Java--使用NIO實現非阻塞Socket通訊ThinkingJava
- python UDP套接字通訊PythonUDP
- SwiftNIO初探-簡單UDP通訊SwiftUDP
- 通訊協議:HTTP、TCP、UDP協議HTTPTCPUDP
- Linux中的UDP通訊LinuxUDP
- socket.io通訊原理