文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C# Socket 编程之 TcpListener (转载)

C# Socket 编程之 TcpListener (转载)

时间:2011-02-02  来源:黒田

 

view sourceprint?
    public partial class COMService {
        private int maxLink = 100000;
        private int currentLinked;
      private ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
        public void Start() {
  
            Thread thread = new Thread(new ParameterizedThreadStart(ShowStat));
            thread.IsBackground = true;
            thread.Start();
              
            TcpListener server = new TcpListener(new System.Net.IPEndPoint(0, 8090));
            server.Start(100);
            tcpClientConnected.Reset();
            IAsyncResult result = server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);
            tcpClientConnected.WaitOne();
        }
  
        private void ShowStat(object o) {
            while (true) {
                lock (typeof(COMService)) {
                    Console.WriteLine("当前连接数:" + currentLinked + "/" + maxLink);
                }
                Thread.Sleep(2000);
            }
        }
  
        private void Acceptor(IAsyncResult o) {
            TcpListener server = o.AsyncState as TcpListener;
            Debug.Assert(server != null);
            TcpClient client = null;
            try {
                client = server.EndAcceptTcpClient(o);
                System.Threading.Interlocked.Increment(ref currentLinked);
                  
            } catch {
                  
            }
            IAsyncResult result = server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);
            if (client == null) {
                return;
            } else {
                Thread.CurrentThread.Join();
            }
            Close(client);
        }
  
        private void Close(TcpClient client) {
            if (client.Connected) {
                client.Client.Shutdown(SocketShutdown.Both);
            }
            client.Client.Close();
            client.Close();
  
            System.Threading.Interlocked.Decrement(ref currentLinked);
        }
    }
}

以下是Client的代码:

 

view sourceprint?
public class ClientPool {
    private static List<TcpWork> clients = new List<TcpWork>();
 
    private static int freeCount;
 
    private static int workCount;
 
    private static int maxAllowed = 2;
 
    private static int minClients = 2;
    /// <summary>
    /// create new instance
    /// </summary>
    private ClientPool() {
    }
 
    private static ClientPool instance;
    private static readonly object syncInstanceObj = new object();
    public static ClientPool Singleton {
        get {
            if (instance == null) {
                lock (syncInstanceObj) {
                    if (instance == null) {
                        instance = new ClientPool();
                    }
                }
            }
            return instance;
        }
    }
 
    private static readonly object syncObj = new object();
 
    public TcpWork GetClient() {
            try {
            TcpWork work = new TcpWork();
            work.Connect("127.0.0.1", 8090);
            work.LingerState = new LingerOption(false, 3);
            work.IsWork = true;
            work.Expired = false;
            workCount++;
            lock (syncObj) {
                clients.Add(work);
            }
            Console.WriteLine(workCount);
            return work;
        } catch (Exception ex){
            Console.WriteLine(ex.Message);
            return null;
        }
    }
}

Client模拟多线程并发:

 

 

view sourceprint?
class Program {
    static void Main(string[] args) {
        for (int i = 0; i < 1000; i++) {
            ThreadPool.QueueUserWorkItem(new WaitCallback(Work), null);
        }
        Console.ReadKey();
    }
 
    private static void Work(object o) {
        ClientPool.Singleton.GetClient();
    }
}

 

 

从这个编程模型可以看出,高并发的服务器不光需要满足有多少个并发连接数,每秒创建多少个连接数也是个重要指标~~

 

实际运行上看看,TcpListener每秒大概能创建两个连接,其他的连接会被拒绝,保持的长连接数1000的样子。很明显,TcpListener要被咔嚓掉了~~

 

注意,以上代码仅仅是用来测·试连接用的,这种写法会导致服务器端的连接无法释放~~~~

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载