http://songchao.blog.51cto.com/133022/38849
使用不需連線的通訊端,我們能夠在自我包含的資料包裡發送訊息,採用獨立的讀函數讀取訊息,讀取的訊息是使用獨立的發送函數發送的。但是UDP資料包不能保證可靠傳輸,存在許多的因素,比如網路繁忙等等,都有可能阻止資料包到達指定的目的地。
(1)UDP的簡單應用: 由於UDP是一種不需連線的協議。因此,為了使伺服器應用能夠發送和接收UDP資料包,則需要做兩件事情: 建立一個Socket對象; 將建立的通訊端對象與本地IPEndPoint進行綁定。 完成上述步驟後,那麼建立的通訊端就能夠在IPEndPoint上接收流入的UDP資料包,或者將流出的UDP資料包發送到網路中任意其他裝置商。使用 UDP進行通訊時,不需要TCP串連。因為異地的主機之間沒有建立串連,所以UDP不能使用標準的Send()和Receive()t通訊端方法,而是使 用兩個其他的方法:SendTo()和ReceiveFrom()。 SendTo()方法指定要發送的資料,和目標機器的IPEndPoint。該方法有多種不同的使用方法,可以根據具體的應用進行選擇,但是至少要指定資料包和目標機器。如下: SendTo(byte[] data,EndPoint Remote) ReceiveFrom()方法同SendTo()方法類似,但是使用EndPoint對象聲明的方式不一樣。利用ref修飾,傳遞的不是一個EndPoint對象,而是將參數傳遞給一個EndPoint對象。
(2)UDP伺服器 UDP應用不是嚴格意義上的真正的伺服器和客戶機,而是平等的關係,即沒有主與次的關係。為了簡便起見,仍然把下面的這個應用叫做UDP伺服器。原始碼如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace SimpleUdpSrvr
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);//定義一網路端點
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//定義一個Socket
newsock.Bind(ipep);//Socket與本地的一個終結點相關聯
Console.WriteLine("Waiting for a client....."); 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.GetBytes(data,0,recv)); string welcome = "Welcome to my test server!";
data = Encoding.ASCII.GetBytes(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);
}
}
}
} 對於接收流入的UDP伺服器程式來說,必須將程式與本地系統中指定的UDP連接埠進行綁定。這就可以通過使用合適的本地IP地址建立一個 IPEndPoint對象,以及何時的UDP連接埠號碼。上述範常式序中的UDP伺服器能夠在連接埠9050從網路上接收任意流入的UDP資料包。
(3)UDP客戶機 UDP客戶機程式與伺服器程式非常類似。 因為客戶機不需要在指定的UDP連接埠等待流入的資料,因此,不使用Bind()方法,而是使用在資料發送時系統隨機指定的一個UDP連接埠,而且使用同一個 連接埠接收返回的訊息。在看法產品時,要為客戶機指定一套UDP連接埠,以便伺服器和客戶機程式使用相同的連接埠號碼。UDP客戶機程式首先定義一個 IPEndPoint,UDP伺服器將發送資料包到這個IPEndPoint。如果在遠程裝置上運行UDP伺服器程式,在IPEndPoint定義中必須 輸入適當的IP地址和UDP連接埠號碼資訊。using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace SimpleUdpClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];//定義一個數組用來做資料的緩衝區
string input, stringData; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); string welcome = "Hello,are you there?";
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];
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 == "text")//結束標記
{
break;
}
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);//將資料發送到指定的終結點Remote
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);//從Remote接受資料
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("Stopping client");
server.Close();
}
}
}
(4)測試伺服器與客戶機 由於條件的限制,測試是在一台電腦上進行的。所以在客戶機的定義中是將發送和接收資訊的地址設定為原生。 先啟動伺服器,在啟動客戶機,按照提示輸入資訊即可。