UDPC#发送接受聊天

以前GM TOOLS新版聊天功能代码sample:

  1.    private const int sampleUdpPort = 4568;
  2.         public Thread sampleUdpThread;
  3.         public SockApp _m_ClientEvent;
  4.         public delegate void InvokeServer(Enum.Message_Body[,] mResult); 
  5.         private  InvokeServer _iServer;
  6.         private System.Windows.Forms.Form _form;
  7.         public UdpUserServer(SockApp m_ClientEvent, InvokeServer iServer, System.Windows.Forms.Form form)
  8.         {
  9.             try
  10.             {       
  11.                 //Starting the UDP Server thread.
  12.                 _m_ClientEvent = m_ClientEvent;
  13.                 _iServer = iServer;
  14.                 _form = form;
  15.                 sampleUdpThread = new Thread(new ThreadStart(StartReceiveFrom2));
  16.                 sampleUdpThread.Start();
  17.                 Console.WriteLine("Started SampleTcpUdpServer's UDP Receiver Thread!/n");
  18.             }
  19.             catch (Exception e)
  20.             {
  21.                 Console.WriteLine("An UDP Exception has occurred!" + e.ToString());
  22.                 //sampleUdpThread.Abort();
  23.             }
  24.         }
  25.         
  26.         public void StartReceiveFrom2()
  27.         {
  28.             
  29.             IPHostEntry localHostEntry;
  30.         
  31.             try
  32.             {
  33.                 //Create a UDP socket.
  34.                 Socket soUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  35.                 
  36.                 try
  37.                 {
  38.                     localHostEntry = Dns.GetHostByName(Dns.GetHostName());
  39.                 }
  40.                 catch(Exception)
  41.                 {
  42.                     Console.WriteLine("Local Host not found"); // fail
  43.                     return ;
  44.                 }
  45.                 IPEndPoint localIpEndPoint = new IPEndPoint(IPAddress.Any,sampleUdpPort);               
  46.                 soUdp.Bind(localIpEndPoint);
  47.             
  48.                 while (true)
  49.                 {
  50.                     try 
  51.                     {
  52.                         Enum.Message_Body[,] mResult = null;
  53.                         string Server_Sky = null;
  54.                         Byte[] received = new Byte[256];
  55.                         IPEndPoint tmpIpEndPoint = new IPEndPoint(localHostEntry.AddressList[0], sampleUdpPort);
  56.                         EndPoint remoteEP = (tmpIpEndPoint);
  57.                         int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
  58.                         string[] ip = remoteEP.ToString().Split(':');
  59.                         if (ip[0].ToString() != tmpIpEndPoint.Address.ToString())
  60.                         {
  61.                             //int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
  62.                             using (DataConvert data = new DataConvert())
  63.                             {
  64.                                 mResult = data.UdpParserBuffer(received, out Server_Sky);
  65.                                 if(mResult!=null)
  66.                                     _form.Invoke(_iServer, mResult);
  67.                             }
  68.                         }
  69.                     }
  70.                     catch(Exception Exp)
  71.                     {
  72.                         Console.WriteLine("A Socket Exception has occurred!" + Exp.ToString());
  73.                     }            
  74.                 }
  75.             }
  76.             catch (SocketException se)
  77.             {
  78.                 Console.WriteLine("A Socket Exception has occurred!" + se.ToString());
  79.             }   
  80.         }

以下是客户端代码:

  1. public enum clientType {TCP, UDP}; //Type of connection the client is making.
  2.         private const int ANYPORT = 0;
  3.         private const int SAMPLEUDPPORT = 4568;
  4.         public clientType cliType;
  5.         public void SendMsg(Enum.Message_Body[] mContect)
  6.         {
  7.             try
  8.             {
  9.                 //Create an instance of UdpClient.
  10.                 UdpClient udpClient = new UdpClient(mContect[0].oContent.ToString(), SAMPLEUDPPORT);
  11.                 IPHostEntry remoteHostEntry = Dns.GetHostByName(mContect[0].oContent.ToString());
  12.                 IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteHostEntry.AddressList[0], SAMPLEUDPPORT);
  13.                 Byte[] inputToBeSent = new Byte[256];
  14.                 //构造要发送的数据
  15.                 using (DataConvert data = new DataConvert())
  16.                 {
  17.                     inputToBeSent = data.SetBuffer(Enum.ServiceKey.SERVERINFO_IP_QUERY, Enum.MsgCategory.COMMON, mContect);
  18.                     //向服务器发送数据
  19.                     udpClient.Send(inputToBeSent, inputToBeSent.Length);
  20.                 }
  21.             }
  22.             catch (Exception e)
  23.             {
  24.                 Console.WriteLine(e.Message);
  25.             }
  26.         }
  27.         public void SendMsg(Enum.Message_Body[] mContect, Enum.Message_Body[,] mUserList)
  28.         {
  29.             try
  30.             {
  31.                 //if (mContect[2].oContent.ToString() == "1")
  32.                 //{
  33.                     //UdpClient udpClient = new UdpClient("192.168.24.255", SAMPLEUDPPORT);
  34.                     //IPHostEntry remoteHostEntry = Dns.GetHostByName("192.168.24.255");
  35.                     //IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteHostEntry.AddressList[0], SAMPLEUDPPORT);
  36.                     //Byte[] inputToBeSent = new Byte[256];
  37.                     ////构造要发送的数据
  38.                     //using (DataConvert data = new DataConvert())
  39.                     //{
  40.                     //    inputToBeSent = data.SetBuffer(Enum.ServiceKey.SERVERINFO_IP_QUERY, Enum.MsgCategory.COMMON, mContect);
  41.                     //    //向服务器发送数据
  42.                     //    udpClient.Send(inputToBeSent, inputToBeSent.Length);
  43.                     //}
  44.                 for (int i = 0; i < mUserList.GetLength(0); i++)
  45.                 {
  46.                     if (mUserList[i, 10].oContent.ToString() == "1" && mUserList[i, 12].oContent.ToString() != "N/A" && mUserList[i, 12].oContent.ToString() != "")
  47.                     {
  48.                         mContect[0].oContent = mUserList[i, 12].oContent.ToString();
  49.                         UdpClient udpClient = new UdpClient(mUserList[i, 12].oContent.ToString(), SAMPLEUDPPORT);
  50.                         IPHostEntry remoteHostEntry = Dns.GetHostByName(mUserList[i, 12].oContent.ToString());
  51.                         IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteHostEntry.AddressList[0], SAMPLEUDPPORT);
  52.                         Byte[] inputToBeSent = new Byte[256];
  53.                         //构造要发送的数据
  54.                         using (DataConvert data = new DataConvert())
  55.                         {
  56.                             inputToBeSent = data.UdpSetBuffer(Enum.ServiceKey.SERVERINFO_IP_QUERY, Enum.MsgCategory.COMMON, mContect);
  57.                             //向服务器发送数据
  58.                             if (inputToBeSent!=null)
  59.                                 udpClient.Send(inputToBeSent, inputToBeSent.Length);
  60.                         }
  61.                     }
  62.                 }
  63.                 //}
  64.             }
  65.             catch (Exception e)
  66.             {
  67.                 Console.WriteLine(e.Message+"123");
  68.             }
  69.         }

你可能感兴趣的:(UDPC#发送接受聊天)