⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controlserver.cs

📁 C常用算法程序集,一部比较经典的程序算法合集
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net ;
using System.Net .Sockets ;
using System.Threading ;
using System.Collections ;
using System.IO;
namespace mychatserver
{
    public class controlserver
    {
        public TcpListener client;

        public controlserver()
        {
        }
        public OutDelegate havenewuser;
        public static Hashtable usertable = new Hashtable();
        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;

            private void makeuserinfo(string userinfo, EndPoint userip, TcpClient client)
            {
                string[] user = userinfo.Split(':');

                if (user[1].Equals("我上线了!"))
                {

                    controlserver.usertable.Add(user[0], userip);
                    string str = user[0] + "登陆服务器!........时间:" + System.DateTime.Now.ToString();
                    newuserinfocoming(str);
                    string str1 = null;//广播一下让用户知道都是谁在线
                    foreach (DictionaryEntry e in controlserver.usertable)
                        str1 = str1 + e.Key.ToString() + ":";
                    srvradvertise(str1);

                }
                else if (user[1].Equals("我下线了!"))
                {
                    controlserver.usertable.Remove(user[0]);
                    string str = user[0] + "退出服务器!........时间:" + System.DateTime.Now.ToString();
                    newuserinfocoming(str);
                    string str1 = "退出服务器:" + user[0];
                    srvradvertise(str1);

                }
                else if (user[1].Equals("her ip information"))
                {

                    string herip = (controlserver.usertable[user[0]]).ToString();
                    byte[] info = Encoding.Unicode.GetBytes(herip);
                    client.Client.Send(info, info.Length, SocketFlags.None);
                }
                else
                {
                    throw new Exception("不明信息");
                }

            }

            void srvradvertise(string str1)//通知用户谁在线
            {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9051);//也可以IPAddress .Parse ("192.168.1.255")
                if (str1 == null)
                    str1 = ";";
                byte[] hostname = Encoding.Unicode.GetBytes(str1);
              
                server.SendTo(hostname, iep);
                Thread.Sleep(6000);
                server.SendTo(hostname, iep);
                // label1.Text = Dns.GetHostName();


            }

            public void HandleConnection()
            {
                try
                {
                    int recv;
                    byte[] data = new byte[1024];
                    TcpClient client1 = threadListener.AcceptTcpClient();
                    connections++;
                    recv = client1.Client.Receive(data);
                    EndPoint userip = client1.Client.RemoteEndPoint;
                    string userinfo = Encoding.Unicode.GetString(data, 0, recv);
                    makeuserinfo(userinfo, userip, client1);//看用户是干什么的
                    client1.Close();
                }
                catch
                {
                }
            }
        }
    }
}

⌨️ 快捷键说明

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