實現一個小的通信功能:客戶端向服務端提交數據,然后服務端回執數據。
一、新建一個控制台應用程序:Client。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace Client { class Program { static void Main(string[] args) { //創建一個Socket對象 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); Console.Write("向服務端提交當前時間!\n"); string dt = DateTime.Now.ToString();//獲取當前時間。 byte[] send = Encoding.ASCII.GetBytes(dt); //string message = Console.ReadLine();//發送客戶端輸入信息。 //byte[] send = Encoding.ASCII.GetBytes(message); s.SendTo(send, (EndPoint)ipep);//向服務器發送 int length; byte[] data = new byte[1024]; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); length = s.ReceiveFrom(data, ref Remote);//通過ReceiveFrom接受來自服務端的數據 Console.Write("從" + Remote.ToString() + "接收信息:\n服務器已接收到客戶端發送時間"); string receive = Encoding.ASCII.GetString(data, 0, length); Console.WriteLine(receive); Console.ReadLine(); } } }
二、新建一個控制台應用程序:Server。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace Server { class Program { static void Main(string[] args) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); s.Bind(ipep);//使用Bind方法綁定所指定的接口使Socket與一個本地終結點相聯 Console.WriteLine("等待客戶端發送信息"); int length; byte[] data = new byte[1024]; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); length = s.ReceiveFrom(data, ref Remote); Console.Write("從" + Remote.ToString() + "接收信息:"); string receive = Encoding.ASCII.GetString(data, 0, length); Console.WriteLine(receive); //string sendString = "SerderTime from Cliend:"+receive; byte[] send = Encoding.ASCII.GetBytes(receive); s.SendTo(send, Remote); //s.Close(); Console.ReadLine(); } } }
先后運行服務端和客戶端程序。執行結果如下:
三、可以進一步的完善功能:如可以讓客戶端每5秒鍾向客戶端提交一次數據。
這樣只需要對程序進行一些修改:在客戶端增加一個定時器,在程序運行后,每5秒鍾向服務端提交一次當前時間。(在Client運行期間,Server端必須保持運行狀態)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace Client { public class Program { //創建一個Socket對象 static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); static IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); static void Main(string[] args) { Console.Write("向服務端提交當前時間!\n"); //創建一個定時器對象 System.Timers.Timer myTimer = new System.Timers.Timer(); myTimer.AutoReset = true; myTimer.Interval = 5000; myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed); myTimer.Enabled = true; Console.ReadLine(); } public static void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { string dt = DateTime.Now.ToString(); byte[] send = Encoding.ASCII.GetBytes(dt); //string message = Console.ReadLine(); //byte[] send = Encoding.ASCII.GetBytes(message); s.SendTo(send, (EndPoint)ipep);//向服務器發送當前時間 int length; byte[] data = new byte[1024]; IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(senderEP); length = s.ReceiveFrom(data, ref Remote);//通過ReceiveFrom接受來自服務端的數據 Console.Write("從" + Remote.ToString() + "接收信息:\n服務器已接收到客戶端發送時間"); string receive = Encoding.ASCII.GetString(data, 0, length); Console.WriteLine(receive); Console.ReadLine(); } } }
在服務端接收和回執部分代碼上添加一個while循環語句:
四、再次先后運行Server和Client。運行結果如下: