controlserver.cs

来自「C常用算法程序集,一部比较经典的程序算法合集」· CS 代码 · 共 62 行

CS
62
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Net ;
using System.Net .Sockets ;
using System.Threading ;
namespace mychatserver
{
  public class controlserver
    {
      public  TcpListener client;
        
        public controlserver()
        {            
        }
        public OutDelegate havenewuser;

        public void startserver()
        {
            IOxml xml1 = new IOxml();
            IPAddress addr = IPAddress.Parse(xml1.GetIP());
           int  port = xml1.GetPort();
            client = new TcpListener(addr,port);
            client.Start();
            while (true)
            {
                while (!client.Pending())
                {
                    Thread.Sleep(1000);
                }
                ConnectionThread newconnection = new ConnectionThread();
                newconnection.threadListener = this.client;
                newconnection.newuserinfocoming += havenewuser;
                Thread newthread = new Thread(new ThreadStart(newconnection.HandleConnection));                
                newthread.Start();
               
            }
        }
        class ConnectionThread
        {
            public event OutDelegate newuserinfocoming;
            public TcpListener threadListener;
            private static int connections = 0;
            public void HandleConnection()
            {
                try
                {
                    int recv;
                    byte[] data = new byte[1024];
                    TcpClient client = threadListener.AcceptTcpClient();
                    connections++;
                    string str = client.Client.RemoteEndPoint.ToString() + "登陆........时间:" + System.DateTime.Now.ToString();
                    newuserinfocoming(str);
                }
                catch
                {
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?