欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#基于UDP進(jìn)行異步通信的方法

 更新時(shí)間:2015年04月23日 09:37:23   作者:songguo  
這篇文章主要介紹了C#基于UDP進(jìn)行異步通信的方法,實(shí)例分析了C#基于UDP實(shí)現(xiàn)異步通信的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了C#基于UDP進(jìn)行異步通信的方法。分享給大家供大家參考。具體如下:

服務(wù)器端:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace AsyncServer
{
 public class UdpState
 {
  public UdpClient udpClient;
  public IPEndPoint ipEndPoint;
  public const int BufferSize = 1024;
  public byte[] buffer = new byte[BufferSize];
  public int counter = 0;
 }
 public class AsyncUdpSever
 {
  private IPEndPoint ipEndPoint = null;
  private IPEndPoint remoteEP = null;
  private UdpClient udpReceive = null;
  private UdpClient udpSend = null;
  private const int listenPort = 1100;
  private const int remotePort = 1101;
  UdpState udpReceiveState = null;
  UdpState udpSendState = null;
  private ManualResetEvent sendDone = new ManualResetEvent(false);
  private ManualResetEvent receiveDone = new ManualResetEvent(false);
  public AsyncUdpSever()
  {
   ipEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
   remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], remotePort);
   udpReceive = new UdpClient(ipEndPoint);
   udpSend = new UdpClient();
   udpReceiveState = new UdpState();   
   udpReceiveState.udpClient = udpReceive;
   udpReceiveState.ipEndPoint = ipEndPoint;
   udpSendState = new UdpState();
   udpSendState.udpClient = udpSend;
   udpSendState.ipEndPoint = remoteEP;
  }
  public void ReceiveMsg()
  {
   Console.WriteLine("listening for messages");
   while(true)
   {
    lock (this)
    { 
     IAsyncResult iar = udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
     receiveDone.WaitOne();
     Thread.Sleep(100);
    }
   }
  }
  private void ReceiveCallback(IAsyncResult iar)
  {
   UdpState udpReceiveState = iar.AsyncState as UdpState;
   if (iar.IsCompleted)
   {
    Byte[] receiveBytes = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
    string receiveString = Encoding.ASCII.GetString(receiveBytes);
    Console.WriteLine("Received: {0}", receiveString);
    //Thread.Sleep(100);
    receiveDone.Set();
    SendMsg();
   }
  }
  private void SendMsg()
  {
   udpSend.Connect(udpSendState.ipEndPoint);
   udpSendState.udpClient = udpSend;
   udpSendState.counter ++;
   string message = string.Format("第{0}個(gè)UDP請(qǐng)求處理完成!",udpSendState.counter);
   Byte[] sendBytes = Encoding.Unicode.GetBytes(message);
   udpSend.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), udpSendState);
   sendDone.WaitOne();
  }
  private void SendCallback(IAsyncResult iar)
  {
   UdpState udpState = iar.AsyncState as UdpState;
   Console.WriteLine("第{0}個(gè)請(qǐng)求處理完畢!", udpState.counter);
   Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar));
   sendDone.Set();
  }
  public static void Main()
  {
   AsyncUdpSever aus = new AsyncUdpSever();
   Thread t = new Thread(new ThreadStart(aus.ReceiveMsg));
   t.Start();
   Console.Read();
  }
 }
}

客戶端:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace AsyncClient
{
 public class UdpState
 {
  public UdpClient udpClient = null;
  public IPEndPoint ipEndPoint = null;
  public const int BufferSize = 1024;
  public byte[] buffer = new byte[BufferSize];
  public int counter = 0;
 }
 public class AsyncUdpClient
 {
  public static bool messageSent = false;
  // Receive a message and write it to the console.
  private const int listenPort = 1101;
  private const int remotePort = 1100;
  private IPEndPoint localEP = null;
  private IPEndPoint remoteEP = null;
  private UdpClient udpReceive = null;
  private UdpClient udpSend = null;
  private UdpState udpSendState = null;
  private UdpState udpReceiveState = null;
  private int counter = 0;
  private ManualResetEvent sendDone = new ManualResetEvent(false);
  private ManualResetEvent receiveDone = new ManualResetEvent(false);
  private Socket receiveSocket;
  private Socket sendSocket;
  public AsyncUdpClient()
  {
   localEP = new IPEndPoint(IPAddress.Any, listenPort);
   remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0],remotePort);
   udpReceive = new UdpClient(localEP);   
   udpSend = new UdpClient();
   udpSendState = new UdpState();
   udpSendState.ipEndPoint = remoteEP;
   udpSendState.udpClient = udpSend;
   udpReceiveState = new UdpState();
   udpReceiveState.ipEndPoint = remoteEP;
   udpReceiveState.udpClient = udpReceive;
   receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
   receiveSocket.Bind(localEP);
   sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
   sendSocket.Bind(remoteEP);
  }
  public void SendMsg()
  { 
   udpSend.Connect(remoteEP);
   //Thread t = new Thread(new ThreadStart(ReceiveMessages));
   //t.Start();
   Byte[] sendBytes;
   string message;   
   while (true)
   { 
    message = "Client" + (counter++).ToString();
    lock (this)
    {
     sendBytes = Encoding.ASCII.GetBytes(message);
     udpSendState.counter = counter;
     udpSend.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), udpSendState);
     sendDone.WaitOne();
     Thread.Sleep(200);
     ReceiveMessages();
    }
   }    
  }
  public void SendCallback(IAsyncResult iar)
  {
   UdpState udpState = iar.AsyncState as UdpState;
   if (iar.IsCompleted)
   {
    Console.WriteLine("第{0}個(gè)發(fā)送完畢!", udpState.counter);
    Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar));
    //if (udpState.counter == 10)
    //{
    // udpState.udpClient.Close();
    //}
    sendDone.Set();
   }   
  }
  public void ReceiveMessages()
  {
   lock (this)
   {
    udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
    receiveDone.WaitOne();
    Thread.Sleep(100);
   } 
  }
  public void ReceiveCallback(IAsyncResult iar)
  {
   UdpState udpState = iar.AsyncState as UdpState;
   if (iar.IsCompleted)
   {
    Byte[] receiveBytes = udpState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
    string receiveString = Encoding.Unicode.GetString(receiveBytes);
    Console.WriteLine("Received: {0}", receiveString);
    receiveDone.Set();
   }   
  }
  public static void Main()
  {
   AsyncUdpClient auc = new AsyncUdpClient();
   auc.SendMsg();
   Console.Read();
  }
 }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼

    C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-08-08
  • Unity實(shí)戰(zhàn)之FlyPin(見縫插針)小游戲的實(shí)現(xiàn)

    Unity實(shí)戰(zhàn)之FlyPin(見縫插針)小游戲的實(shí)現(xiàn)

    這篇文章主要介紹了利用Unity制作FlyPin(見縫插針)小游戲的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試
    2022-01-01
  • Unity實(shí)現(xiàn)通用的信息提示框

    Unity實(shí)現(xiàn)通用的信息提示框

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)通用的信息提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示實(shí)例

    DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示實(shí)例

    這篇文章主要介紹了DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示的方法,實(shí)例講述了窗體與圖形繪制函數(shù)的用法,需要的朋友可以參考下
    2014-10-10
  • C#之lock的使用及說明

    C#之lock的使用及說明

    這篇文章主要介紹了C#之lock的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 關(guān)于C#.net winform程序驗(yàn)證moss的集成身份認(rèn)證實(shí)例

    關(guān)于C#.net winform程序驗(yàn)證moss的集成身份認(rèn)證實(shí)例

    因?yàn)榫W(wǎng)站使用的是windows集成認(rèn)證,所以遇到了權(quán)限問題,需要輸入密碼。使操作和用戶體驗(yàn)非常不方便,研究了好久沒有找到好的方法,最后終于讓我踏破鐵鞋總結(jié)出了下面的方法
    2013-03-03
  • 詳解C#中SqlParameter的作用與用法

    詳解C#中SqlParameter的作用與用法

    本篇文章主要介紹了C#中SqlParameter的作用與用法,因?yàn)橥ㄟ^SQL語句的方式,有時(shí)候存在腳本注入的危險(xiǎn),所以在大多數(shù)情況下不建議用拼接SQL語句字符串方式,希望通過SqlParameter實(shí)現(xiàn)來實(shí)現(xiàn)對(duì)數(shù)據(jù)的操作。
    2016-12-12
  • C#多線程的ResetAbort()方法

    C#多線程的ResetAbort()方法

    這篇文章介紹了C#多線程的ResetAbort()方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C# winform 請(qǐng)求http的實(shí)現(xiàn)(get,post)

    C# winform 請(qǐng)求http的實(shí)現(xiàn)(get,post)

    本文主要介紹了C# winform 請(qǐng)求http的實(shí)現(xiàn)(get,post),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C#強(qiáng)制類型轉(zhuǎn)換小結(jié)

    C#強(qiáng)制類型轉(zhuǎn)換小結(jié)

    任何一門編程語言均有相關(guān)數(shù)據(jù)類型。C#也不例外,不過轉(zhuǎn)換過程要注意小類型能轉(zhuǎn)換成大類型,但大類型一般不能轉(zhuǎn)換成小類型,下面小編給大家詳解C#強(qiáng)制類型轉(zhuǎn)換小結(jié),需要的朋友參考下吧
    2017-07-07

最新評(píng)論