以前GM TOOLS新版聊天功能代码sample:
- private const int sampleUdpPort = 4568;
- public Thread sampleUdpThread;
- public SockApp _m_ClientEvent;
- public delegate void InvokeServer(Enum.Message_Body[,] mResult);
- private InvokeServer _iServer;
- private System.Windows.Forms.Form _form;
- public UdpUserServer(SockApp m_ClientEvent, InvokeServer iServer, System.Windows.Forms.Form form)
- {
- try
- {
- //Starting the UDP Server thread.
- _m_ClientEvent = m_ClientEvent;
- _iServer = iServer;
- _form = form;
- sampleUdpThread = new Thread(new ThreadStart(StartReceiveFrom2));
- sampleUdpThread.Start();
- Console.WriteLine("Started SampleTcpUdpServer's UDP Receiver Thread!/n");
- }
- catch (Exception e)
- {
- Console.WriteLine("An UDP Exception has occurred!" + e.ToString());
- //sampleUdpThread.Abort();
- }
- }
- public void StartReceiveFrom2()
- {
- IPHostEntry localHostEntry;
- try
- {
- //Create a UDP socket.
- Socket soUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- try
- {
- localHostEntry = Dns.GetHostByName(Dns.GetHostName());
- }
- catch(Exception)
- {
- Console.WriteLine("Local Host not found"); // fail
- return ;
- }
- IPEndPoint localIpEndPoint = new IPEndPoint(IPAddress.Any,sampleUdpPort);
- soUdp.Bind(localIpEndPoint);
- while (true)
- {
- try
- {
- Enum.Message_Body[,] mResult = null;
- string Server_Sky = null;
- Byte[] received = new Byte[256];
- IPEndPoint tmpIpEndPoint = new IPEndPoint(localHostEntry.AddressList[0], sampleUdpPort);
- EndPoint remoteEP = (tmpIpEndPoint);
- int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
- string[] ip = remoteEP.ToString().Split(':');
- if (ip[0].ToString() != tmpIpEndPoint.Address.ToString())
- {
- //int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
- using (DataConvert data = new DataConvert())
- {
- mResult = data.UdpParserBuffer(received, out Server_Sky);
- if(mResult!=null)
- _form.Invoke(_iServer, mResult);
- }
- }
- }
- catch(Exception Exp)
- {
- Console.WriteLine("A Socket Exception has occurred!" + Exp.ToString());
- }
- }
- }
- catch (SocketException se)
- {
- Console.WriteLine("A Socket Exception has occurred!" + se.ToString());
- }
- }
以下是客户端代码:
- public enum clientType {TCP, UDP}; //Type of connection the client is making.
- private const int ANYPORT = 0;
- private const int SAMPLEUDPPORT = 4568;
- public clientType cliType;
- public void SendMsg(Enum.Message_Body[] mContect)
- {
- try
- {
- //Create an instance of UdpClient.
- UdpClient udpClient = new UdpClient(mContect[0].oContent.ToString(), SAMPLEUDPPORT);
- IPHostEntry remoteHostEntry = Dns.GetHostByName(mContect[0].oContent.ToString());
- IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteHostEntry.AddressList[0], SAMPLEUDPPORT);
- Byte[] inputToBeSent = new Byte[256];
- //构造要发送的数据
- using (DataConvert data = new DataConvert())
- {
- inputToBeSent = data.SetBuffer(Enum.ServiceKey.SERVERINFO_IP_QUERY, Enum.MsgCategory.COMMON, mContect);
- //向服务器发送数据
- udpClient.Send(inputToBeSent, inputToBeSent.Length);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- public void SendMsg(Enum.Message_Body[] mContect, Enum.Message_Body[,] mUserList)
- {
- try
- {
- //if (mContect[2].oContent.ToString() == "1")
- //{
- //UdpClient udpClient = new UdpClient("192.168.24.255", SAMPLEUDPPORT);
- //IPHostEntry remoteHostEntry = Dns.GetHostByName("192.168.24.255");
- //IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteHostEntry.AddressList[0], SAMPLEUDPPORT);
- //Byte[] inputToBeSent = new Byte[256];
- ////构造要发送的数据
- //using (DataConvert data = new DataConvert())
- //{
- // inputToBeSent = data.SetBuffer(Enum.ServiceKey.SERVERINFO_IP_QUERY, Enum.MsgCategory.COMMON, mContect);
- // //向服务器发送数据
- // udpClient.Send(inputToBeSent, inputToBeSent.Length);
- //}
- for (int i = 0; i < mUserList.GetLength(0); i++)
- {
- if (mUserList[i, 10].oContent.ToString() == "1" && mUserList[i, 12].oContent.ToString() != "N/A" && mUserList[i, 12].oContent.ToString() != "")
- {
- mContect[0].oContent = mUserList[i, 12].oContent.ToString();
- UdpClient udpClient = new UdpClient(mUserList[i, 12].oContent.ToString(), SAMPLEUDPPORT);
- IPHostEntry remoteHostEntry = Dns.GetHostByName(mUserList[i, 12].oContent.ToString());
- IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteHostEntry.AddressList[0], SAMPLEUDPPORT);
- Byte[] inputToBeSent = new Byte[256];
- //构造要发送的数据
- using (DataConvert data = new DataConvert())
- {
- inputToBeSent = data.UdpSetBuffer(Enum.ServiceKey.SERVERINFO_IP_QUERY, Enum.MsgCategory.COMMON, mContect);
- //向服务器发送数据
- if (inputToBeSent!=null)
- udpClient.Send(inputToBeSent, inputToBeSent.Length);
- }
- }
- }
- //}
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message+"123");
- }
- }