C#網絡編程之UDP

UDP簡介:UDP 是User Datagram Protocol的簡稱, 中文名是用戶數據包協議,是一種無鏈接的傳輸層協議,提供面向事務的簡單不可靠信息傳送服務。UDP是與TCP相對應的協議,它是面向非鏈接的協議,它不與對方鏈接,而直接把數據包發送過去。「面向非鏈接」就是正式通訊前沒必要與對方創建鏈接,無論對方狀態就直接發送。算法

UDP特性:服務器

  (1)UDP是一個無鏈接協議,傳輸數據以前,源端和終端不創建鏈接,當它想傳送時就簡單的抓取來自應用程序的數據,並儘量快的把他扔到網絡上。在發送端UDP傳送的速度僅僅是受應用程序數據生成的速度、計算機能力和傳輸帶寬限制;在接收端,UDP把每一個消息段放在隊列中,應用程序每次從隊列中讀取一個消息段。網絡

  (2)因爲傳輸數據不創建鏈接,所以也就不須要維護鏈接狀態,包括收發狀態等,所以一臺服務機可同時向多臺客戶機傳輸相同的信息。工具

  (3)UDP信息包的標題很短,只有8個字節,相對於TCP的20個字節信息包的額外開銷很小。性能

  (4)吞吐量不受擁擠控制算法的調節,只受應用軟件生成數據的速率、傳輸寬帶、源端和終端主機性能的限制。
this

  (5)UDP使用盡可能最大努力交付,即不保證可靠交付,所以主機不須要維持複雜的連接狀態表(該表中有許多參數)。spa

  (6)UDP是面向報文的。發送方對應用程序交下來的報文,在添加首部後就向下交付個IP層。既不拆分,也不合並,而是保留這些報文的邊界,所以,應用程序須要選擇合適的報文大小。server

 

示例:在解決方案建立兩個控制檯程序:UDPSender,UDPReceive隊列

備註:事務

  (1)vs2012啓動兩個項目需設置:右鍵解決方案->通用屬性->啓動項目->多啓動項目->勾選你要啓動的多個項目->肯定->啓動

  (2)vs2012始終能看到解決方案的設置:工具->配置->項目和解決方案->常規->老是顯示解決方案

  (3)IPEndPoint類包含應用程序鏈接到主機的服務所需的主機和本地或遠程端口信息。經過組合服務的主機IP地址和端口號,IPEndPoint造成到服務的鏈接點。

客戶端:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPSender

{

  class program

  {

  static void main(String[] args)

  {

    byte[] data = new byte[1024];

    string input,stringData;

    Console.WriteLine("this is a Client.host name is {0}",Dns.GetHostName());

    //設置服務器IP和端口號

    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8001);

    //定義網絡類型,數據鏈接類型和網絡協議UDP

    Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);

    string welcome = "Hello! I am a Client";

    data = Encoding.ASCII.GetBytes(welcome);

    server.SendTo(data,data.Length,SocketFlags.None,ipep);

    IPEndPoint sender = new IPEndPoint(IPAddress.Any,0);

    EndPoint Remote = (EndPoint)sender;

    data = new byte[1024];

    //對於不存在的IP地址,加入此行代碼後,能夠在指定時間內解除阻塞模式限制

    //server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);

    int recv = server.ReceiveFrom(data,ref Remote);

    Console.WriteLine("Message received from {0}: ", Remote.ToString());

    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

    while(true)

    {

      input = Console.ReadLine();

      if(input == "exit")

      {

        break;

      }

      server.SendTo(Encoding.ASCII.GetBytes(input),Remote);

      data = new byte[1024];

      recv = server.ReceiveFrom(data,ref Remote);

      stringData = Encoding.ASCII.GetString(data,0,recv);

      Console.WriteLine(stringData);

    }

  }

}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPReceive

{

  class Program

  {

    static void main(String[] args)

    {

      int recv;

      byte[] data = new byte[1024];

      //獲得本機IP,設置端口號

      IPEndPoint ipep = new IPEndPoint(IPAddress.Any,8001);

      Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.UDP);

      //綁定網絡地址

      newsock.Bind(ipep);

      Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());

      //等待客戶機鏈接

      Console.WriteLine("Waiting for a client");

      //獲得客戶端IP

      IPEndPoint sender= new IPEndPoint(IPAddress.Any,0);

      EndPoint Remote = (EndPoint)(sender);

      //接受數據

      recv = newsock.ReceiveFrom(data,ref Remote);

      Console.WriteLine("Message received from {0}",Remote.ToString());

      Console.WriteLine(Encoding.ASCII.GetString(data,0,recv));

      //客戶端鏈接成功後,發送信息

      string welcome = "Hi I am a Server";

      data = Encoding.ASCII.GetByte(welcome);

      //發送信息

      newsock.SendTo(data,data.Length,SocketFlags.None,Remote);

      while(true)

      {

        data = new byte[1024];

        //發送接受的信息

        recv = newsock.ReceiveFrom(data,ref Remote); 

        Console.WriteLine(Encoding.ASCII.GetString(data,0,recv));

        newsock.SendTo(data,recv,SocketFlags.None,Remote);

      }

    }

  }

}

相關文章
相關標籤/搜索