Ejemplo n.º 1
0
        //线程函数,封装一个建立连接的通信套接字
        private void StartListen()
        {
            isListen = true;
            //default()只是设置为一个初始值,这里应该为null  参考网址:https://stackoverflow.com/questions/28720717/why-default-in-c-sharp-tcpclient-clientsocket-defaulttcpclient
            Socket clientSocket = default(Socket);

            while (isListen)
            {
                try
                {
                    //参考网址: http://bbs.csdn.net/topics/30100253
                    //  int accept(int sockfd, void *addr, int *addrlen);
                    //注意这个serverSocket,它是用来监听的套接字,当有用户连接上端口后会返回一个新的套接字也就是这里的clientSocket,sercerSocket还是在那儿继续监听的
                    //详细参考网址:http://www.360doc.com/content/13/0908/17/13253385_313070996.shtml
                    //返回值是一个新的套接字描述符,它代表的是和客户端的新的连接,这个socket相当于一个客户端的socket,包含的是客户端的ip和port
                    //但是它也继承字本地的监听套接字,因此它也有服务器的ip和port信息
                    if (serverSocket == null)   //如果服务停止,即serverSocket为空了,那就直接返回
                    {
                        return;
                    }
                    clientSocket = serverSocket.Accept();   //这个方法返回一个通信套接字,并用这个套接字进行通信,错误时返回-1并设置全局错误变量
                }
                catch (SocketException e)
                {
                    File.AppendAllText("E:\\Exception.txt", e.ToString() + "\r\nStartListen\r\n" + DateTime.Now.ToString() + "\r\n");
                }

                //TCP是面向字节流的
                Byte[] bytesFrom      = new Byte[4096];
                String dataFromClient = null;

                if (clientSocket != null && clientSocket.Connected)
                {
                    try
                    {
                        //Socket.Receive() 参考网址:http://blog.csdn.net/cpcpc/article/details/7245420
                        //public int Receive(  byte[] buffer,  int offset,   int size,  SocketFlags socketFlags )
                        //buffer  是byte类型的数组,存储收到的数据的位置
                        //offset  是buffer中存储所接收数据的位置
                        //size    要接收的字节数
                        //socketFlags  socketFlages值的按位组合

                        Int32 len = clientSocket.Receive(bytesFrom);    //获取客户端发来的信息,返回的就是收到的字节数,并且把收到的信息都放在bytesForm里面

                        if (len > -1)
                        {
                            String tmp = Encoding.UTF8.GetString(bytesFrom, 0, len);  //将字节流转换成字符串

                            /*try
                             * {
                             *  dataFromClient = EncryptionAndDecryption.TripleDESDecrypting(tmp);      //数据加密传输
                             * }
                             * catch (Exception e)
                             * {
                             *
                             * }
                             * catch (Exception e)
                             * {
                             *
                             * }*/
                            dataFromClient = tmp;
                            Int32 sublen = dataFromClient.LastIndexOf("$");
                            if (sublen > -1)
                            {
                                dataFromClient = dataFromClient.Substring(0, sublen);   //获取用户名

                                if (!clientList.ContainsKey(dataFromClient))
                                {
                                    clientList.Add(dataFromClient, clientSocket);   //如果用户名不存在,则添加用户名进去

                                    //BroadCast是下面自己定义的一个类,是用来将消息对所有用户进行推送的
                                    //PushMessage(String msg, String uName, Boolean flag, Dictionary<String, Socket> clientList)
                                    BroadCast.PushMessage(dataFromClient + "Joined", dataFromClient, false, clientList);

                                    //HandleClient也是一个自己定义的类,用来负责接收客户端发来的消息并转发给所有的客户端
                                    //StartClient(Socket inClientSocket, String clientNo, Dictionary<String, Socket> cList)
                                    HandleClient client = new HandleClient(txtMsg);

                                    client.StartClient(clientSocket, dataFromClient, clientList);

                                    txtMsg.BeginInvoke(new Action(() =>
                                    {
                                        txtMsg.Text += dataFromClient + "连接上了服务器\r" + DateTime.Now + "\r\n";
                                    }));
                                }
                                else
                                {
                                    //用户名已经存在
                                    clientSocket.Send(Encoding.UTF8.GetBytes("#" + dataFromClient + "#"));
                                }
                            }
                        }
                    }
                    catch (Exception ep)
                    {
                        File.AppendAllText("E:\\Exception.txt", ep.ToString() + "\r\n\t\t" + DateTime.Now.ToString() + "\r\n");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //线程函数,封装一个建立连接的通信套接字
        private void StartListen()
        {
            isListen = true;
            //default()只是设置为一个初始值,这里应该为null
            Socket clientSocket = default(Socket);

            while (isListen)
            {
                try
                {
                    //  int accept(int sockfd, void *addr, int *addrlen);
                    //注意这个serverSocket,它是用来监听的套接字,当有用户连接上端口后会返回一个新的套接字也就是这里的clientSocket,sercerSocket还是在那儿继续监听的
                    //返回值是一个新的套接字描述符,它代表的是和客户端的新的连接,这个socket相当于一个客户端的socket,包含的是客户端的ip和port
                    //但是它也继承字本地的监听套接字,因此它也有服务器的ip和port信息
                    if (serverSocket == null)   //如果服务停止,即serverSocket为空了,那就直接返回
                    {
                        return;
                    }
                    clientSocket = serverSocket.Accept();   //这个方法返回一个通信套接字,并用这个套接字进行通信,错误时返回-1并设置全局错误变量
                }
                catch (SocketException e)
                {
                    File.AppendAllText("D:\\Exception.txt", e.ToString() + "\r\nStartListen\r\n" + DateTime.Now.ToString() + "\r\n");
                }

                //TCP是面向字节流的
                Byte[] bytesFrom = new Byte[4096];
                // String dataFromClient = null;

                if (clientSocket != null && clientSocket.Connected)
                {
                    try
                    {
                        //public int Receive(  byte[] buffer,  int offset,   int size,  SocketFlags socketFlags )
                        //buffer  是byte类型的数组,存储收到的数据的位置
                        //offset  是buffer中存储所接收数据的位置
                        //size    要接收的字节数
                        //socketFlags  socketFlages值的按位组合

                        Int32 len = clientSocket.Receive(bytesFrom);    //获取客户端发来的信息,返回的就是收到的字节数,并且把收到的信息都放在bytesForm里面

                        if (len > -1)
                        {
                            var msg = (Packet)ByteHelper.Deserialize(bytesFrom);


                            if (msg != null && !clientList.ContainsKey(msg.comeNo))
                            {
                                clientList.Add(msg.comeNo, clientSocket);   //如果用户名不存在,则添加用户名进去

                                //BroadCast是下面自己定义的一个类,是用来将消息对所有用户进行推送的
                                //PushMessage(String msg, String uName, Boolean flag, Dictionary<String, Socket> clientList)
                                //Packet pct = new Packet()
                                //{
                                //    type = MessageType.Login,
                                //    comeNo=
                                //};
                                BroadCast.PushMessage(msg, clientList);

                                //HandleClient也是一个自己定义的类,用来负责接收客户端发来的消息并转发给所有的客户端
                                //StartClient(Socket inClientSocket, String clientNo, Dictionary<String, Socket> cList)
                                HandleClient client = new HandleClient(txtMsg);

                                client.StartClient(clientSocket, msg.comeNo, clientList);

                                txtMsg.BeginInvoke(new Action(() =>
                                {
                                    txtMsg.Text += msg.comeNo + "连接上了服务器\r" + DateTime.Now + "\r\n";
                                }));
                            }
                            else
                            {
                                //用户名已经存在
                                // clientSocket.Send(Encoding.UTF8.GetBytes("#" + msg.ChatAcount + "#"));
                            }
                            //dataFromClient = tmp;
                            //Int32 sublen = dataFromClient.LastIndexOf("$");
                            //if (sublen > -1)
                            //{
                            //    dataFromClient = dataFromClient.Substring(0, sublen);   //获取用户名


                            //}
                        }
                    }
                    catch (Exception ep)
                    {
                        File.AppendAllText("D:\\Exception.txt", ep.ToString() + "\r\n\t\t" + DateTime.Now.ToString() + "\r\n");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        //线程函数,封装一个建立连接的通信套接字
        private void StartListen()
        {
            isListen = true;
            //default()只是设置为一个初始值,这里应该为null
            Socket clientSocket = default(Socket);

            while (isListen)
            {
                try
                {
                    //注意这个serverSocket,它是用来监听的套接字,当有用户连接上端口后会返回一个新的套接字也就是这里的clientSocket,sercerSocket还是在那儿继续监听的
                    //返回值是一个新的套接字描述符,它代表的是和客户端的新的连接,这个socket相当于一个客户端的socket,包含的是客户端的ip和port
                    //但是它也继承字本地的监听套接字,因此它也有服务器的ip和port信息
                    if (serverSocket == null)   //如果服务停止,即serverSocket为空了,那就直接返回
                    {
                        return;
                    }
                    clientSocket = serverSocket.Accept();   //这个方法返回一个客户端发给服务端的通信套接字,并用这个套接字进行通信,错误时返回-1并设置全局错误变量
                }
                catch (SocketException e)
                {
                    File.AppendAllText("E:\\Exception.txt", e.ToString() + "\r\nStartListen\r\n" + DateTime.Now.ToString() + "\r\n");
                }

                //TCP是面向字节流的
                Byte[] bytesFrom      = new Byte[4096];//用来存储socket接收到的数据
                String dataFromClient = null;

                if (clientSocket != null && clientSocket.Connected)
                {
                    try
                    {
                        Int32 len = clientSocket.Receive(bytesFrom);    //获取客户端发来的信息,返回的就是收到的字节数,并且把收到的信息都放在bytesForm里面

                        if (len > -1)
                        {
                            String tmp = Encoding.UTF8.GetString(bytesFrom, 0, len);  //将字节流转换成字符串

                            dataFromClient = tmp;
                            Int32 sublen = dataFromClient.LastIndexOf("$");
                            if (sublen > -1)
                            {
                                dataFromClient = dataFromClient.Substring(0, sublen);   //获取用户名

                                if (!clientList.ContainsKey(dataFromClient))
                                {
                                    clientList.Add(dataFromClient, clientSocket);   //如果用户名不存在,则添加用户名进去

                                    //BroadCast是下面自己定义的一个类,是用来将消息对所有用户进行推送的
                                    //PushMessage(String msg, String uName, Boolean flag, Dictionary<String, Socket> clientList)
                                    BroadCast.PushMessage(dataFromClient + "Joined", dataFromClient, false, clientList); //用户一连接成功,会显示XXXjoined+时间

                                    //HandleClient也是一个自己定义的类,用来负责接收客户端发来的消息并转发给所有的客户端
                                    //StartClient(Socket inClientSocket, String clientNo, Dictionary<String, Socket> cList)
                                    HandleClient client = new HandleClient(txtMsg);

                                    client.StartClient(clientSocket, dataFromClient, clientList);

                                    txtMsg.BeginInvoke(new Action(() =>
                                    {
                                        txtMsg.Text += dataFromClient + "连接上了服务器\r" + DateTime.Now + "\r\n";
                                    }));
                                }
                                else
                                {
                                    //用户名已经存在
                                    clientSocket.Send(Encoding.UTF8.GetBytes("#" + dataFromClient + "#"));
                                }
                            }
                        }
                    }
                    catch (Exception ep)
                    {
                        File.AppendAllText("E:\\Exception.txt", ep.ToString() + "\r\n\t\t" + DateTime.Now.ToString() + "\r\n");
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void StartListen()
        {
            isListen = true;

            Socket clientSocket = default(Socket);

            while (isListen)
            {
                try
                {
                    if (serverSocket == null)
                    {
                        return;
                    }
                    clientSocket = serverSocket.Accept();
                }
                catch (SocketException e)
                {
                    File.AppendAllText("E:\\Exception.txt", e.ToString() + "\r\nStartListen\r\n" + DateTime.Now.ToString() + "\r\n");
                }


                Byte[] bytesFrom      = new Byte[4096];
                String dataFromClient = null;

                if (clientSocket != null && clientSocket.Connected)
                {
                    try
                    {
                        Int32 len = clientSocket.Receive(bytesFrom);

                        if (len > -1)
                        {
                            String tmp = Encoding.UTF8.GetString(bytesFrom, 0, len);  //将字节流转换成字符串

                            dataFromClient = tmp;
                            Int32 sublen = dataFromClient.LastIndexOf("$");
                            if (sublen > -1)
                            {
                                dataFromClient = dataFromClient.Substring(0, sublen);   //获取用户名

                                if (!clientList.ContainsKey(dataFromClient))
                                {
                                    clientList.Add(dataFromClient, clientSocket);   //如果用户名不存在,则添加用户名进去


                                    BroadCast.PushMessage(dataFromClient + "Joined", dataFromClient, false, clientList);

                                    //HandleClient也是一个自己定义的类,用来负责接收客户端发来的消息并转发给所有的客户端
                                    //StartClient(Socket inClientSocket, String clientNo, Dictionary<String, Socket> cList)
                                    HandleClient client = new HandleClient(txtMsg);

                                    client.StartClient(clientSocket, dataFromClient, clientList);

                                    txtMsg.BeginInvoke(new Action(() =>
                                    {
                                        txtMsg.Text += dataFromClient + "连接上了服务器\r" + DateTime.Now + "\r\n";
                                    }));
                                }
                                else
                                {
                                    //用户名已经存在
                                    clientSocket.Send(Encoding.UTF8.GetBytes("#" + dataFromClient + "#"));
                                }
                            }
                        }
                    }
                    catch (Exception ep)
                    {
                        File.AppendAllText("E:\\Exception.txt", ep.ToString() + "\r\n\t\t" + DateTime.Now.ToString() + "\r\n");
                    }
                }
            }
        }