最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中使用UDP通信的示例

 更新時間:2020年11月30日 10:59:10   作者:zls365  
這篇文章主要介紹了C#中使用UDP通信的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

網(wǎng)絡通信協(xié)議中的UDP通信是無連接通信,客戶端在發(fā)送數(shù)據(jù)前無需與服務器端建立連接,即使服務器端不在線也可以發(fā)送,但是不能保證服務器端可以收到數(shù)據(jù)。本文實例即為基于C#實現(xiàn)的UDP通信。具體功能代碼如下:

服務器端代碼如下

static void Main(string[] args)
{
  UdpClient client = null;
  string receiveString = null;
  byte[] receiveData = null;
  //實例化一個遠程端點,IP和端口可以隨意指定,等調用client.Receive(ref remotePoint)時會將該端點改成真正發(fā)送端端點
  IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
  
  while (true)
  {
 client = new UdpClient(11000);
 receiveData = client.Receive(ref remotePoint);//接收數(shù)據(jù)
 receiveString = Encoding.Default.GetString(receiveData);
 Console.WriteLine(receiveString);
 client.Close();//關閉連接
  }
}
客戶端代碼如下:
 
static void Main(string[] args)
{
  string sendString = null;//要發(fā)送的字符串
  byte[] sendData = null;//要發(fā)送的字節(jié)數(shù)組
  UdpClient client = null;
  
  IPAddress remoteIP = IPAddress.Parse("127.0.0.1");
  int remotePort = 11000;
  IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);//實例化一個遠程端點
  
  while (true)
  {
 sendString = Console.ReadLine();
 sendData = Encoding.Default.GetBytes(sendString);
  
 client = new UdpClient();
 client.Send(sendData, sendData.Length, remotePoint);//將數(shù)據(jù)發(fā)送到遠程端點
 client.Close();//關閉連接
  }

以上就是C#中使用UDP通信的示例的詳細內容,更多關于c# udp通信的資料請關注腳本之家其它相關文章!

相關文章

最新評論

吴桥县| 北海市| 玉林市| 介休市| 高安市| 日土县| 宜兰市| 白沙| 阿拉尔市| 宝山区| 界首市| 南通市| 交城县| 高台县| 邮箱| 拜城县| 体育| 西乡县| 平安县| 阿克苏市| 武隆县| 河南省| 信丰县| 鹤壁市| 杭锦旗| 万宁市| 吴江市| 新津县| 平顺县| 巴塘县| 崇义县| 清徐县| 梁山县| 密云县| 黄梅县| 西安市| 虞城县| 达日县| 穆棱市| 安平县| 分宜县|