日期:2014-05-19  浏览次数:20754 次

UDP服务器的问题(附代码)
using   System;
using   System.Net;
using   System.Net.Sockets;
using   System.Text;
using   System.Threading;
using   System.Data;
using   System.Data.SqlClient;

//   State   object   for   reading   client   data   asynchronously
public   class   StateObject
{
        //   Client     socket.
        public   Socket   workSocket   =   null;
        //   Size   of   receive   buffer.
        public   const   int   BufferSize   =   255;
        //   Receive   buffer.
        public   byte[]   buffer   =   new   byte[BufferSize];
        //   Received   data   string.
        public   StringBuilder   sb   =   new   StringBuilder();
}

public   class   AsynchronousSocketListener
{

        //   Incoming   data   from   client.
        public   static   string   data   =   null;

        //   Thread   signal.
        public   static   ManualResetEvent   allDone   =   new   ManualResetEvent(false);
        public   static   int   Count   =   1;
        public   AsynchronousSocketListener()
        {
        }

        public   static   void   StartListening()
        {
                byte[]   bytes   =   new   Byte[255];

                IPHostEntry   ipHostInfo   =   Dns.Resolve(Dns.GetHostName());
                IPAddress   ipAddress   =   ipHostInfo.AddressList[0];
                IPEndPoint   localEndPoint   =   new   IPEndPoint(ipAddress,   2020);
                Socket   listener   =   new   Socket(AddressFamily.InterNetwork,   SocketType.Dgram,   ProtocolType.Udp);
                try
                {
                        listener.Bind(localEndPoint);
                        IPEndPoint   sender   =   new   IPEndPoint(IPAddress.Any,   0);
                        EndPoint   tempRemoteEP   =   (EndPoint)sender;
                        try
                        {
                                while   (true)