Ejemplo n.º 1
0
        //打开服务 更新版
        private void OpenServer2()
        {
            IPAddress  localIP = IPAddress.Parse(strIP);
            IPEndPoint ipp     = new IPEndPoint(localIP, strPort);

            server = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);

            server.Bind(ipp);       //本地地址绑定到套接字
            server.Listen(20);      //开始监听   挂起连接队列的最大长度 20
            richTextBox1.Text = "服务器开启监听!!!";
            //while (true)

            for (int o = 0; o <= ThreadCount; o++)
            {
                Thread serverthread = new Thread(
                    () =>
                {
                    Invoke((MethodInvoker)
                           //BeginInvoke((MethodInvoker)
                           (() =>
                    {
                        Socket clientConn = server.Accept();
                        sign = true;
                        if (clientConn.Connected == true)
                        {
                            richTextBox1.Text += "\n一个客户端连入";
                            List <Object> info = null;
                            int i = 1;                                              //暂时的定义循环次数
                            while (sign)
                            {
                                Byte[] byteNum2 = new Byte[1024];

                                clientConn.Receive(byteNum2, byteNum2.Length, 0);                       //接收数据

                                info = (ClassSerializers.DeSerializebinary(new MemoryStream(byteNum2) as MemoryStream)) as List <Object>;
                                //信息提取  反序列化
                                List <Object> infolist = new List <object>();                       //用来存储客户端发来的信息
                                infolist.Add(IsLoginOrRegister(info));                              //代表注册或登陆成功  数据存储or数据对比  还有下线

                                infolist = SendAboutLoOrRe(infolist);                               //将需要发送给客户端的信息发送给客户端

                                byte[] data = ClassSerializers.Serializebinary(infolist).ToArray(); // 将信息序列化为byte[]
                                clientConn.Send(data, data.Length, 0);                              //发送信息
                                if (--i == 0)
                                {
                                    sign = false;                                       //结束循环
                                }
                            }
                            richTextBox1.Text += "\n一个客户端关闭连接";
                        }
                    })
                           );
                });
                serverthread.IsBackground = true;
                serverthread.Start();
            }
        }
Ejemplo n.º 2
0
        //打开服务 更新版2 需要调用的函数
        private void TheServerThread()
        {
            try
            {
                Socket clientConn = server.Accept();
                sign = true;
                if (clientConn.Connected == true)
                {
                    OperateAboutUIControl_RichTextBox(richTextBox1, "客户端连入------");
                    List <Object> info = null;
                    int           i    = 1;                         //暂时的定义循环次数
                    while (sign)
                    {
                        Byte[] byteNum2 = new Byte[1024];

                        clientConn.Receive(byteNum2, byteNum2.Length, 0);               //接收数据

                        info = (ClassSerializers.DeSerializebinary(new MemoryStream(byteNum2) as MemoryStream)) as List <Object>;
                        //信息提取  反序列化
                        List <Object> infolist = new List <object>();                       //用来存储客户端发来的信息

                        infolist.Add(IsLoginOrRegister(info));                              //代表注册或登陆成功  数据存储or数据对比  还有下线

                        infolist = SendAboutLoOrRe(infolist);                               //将需要发送给客户端的信息发送给客户端

                        byte[] data = ClassSerializers.Serializebinary(infolist).ToArray(); // 将信息序列化为byte[]
                        clientConn.Send(data, data.Length, 0);                              //发送信息
                        if (--i == 0)
                        {
                            sign = false;                               //结束循环
                        }
                    }
                    OperateAboutUIControl_RichTextBox(richTextBox1, "客户端退出------");
                }
            }
            catch
            {
                return;
            }
        }