Beispiel #1
0
        /// <summary>
        /// 给客户端发送消息
        /// </summary>
        /// <param name="data"></param>
        private static void SendMsg(string data)
        {
            //给所有连接上的客户端返回数据
            NetBufferWriter writer = new NetBufferWriter();

            writer.WriteString("Get Message:" + data);
            byte[] buffer = writer.Finish();
            foreach (Socket socket in m_clientSocketList)
            {
                socket.Send(buffer);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 客户端连接请求监听
        /// </summary>
        static void ClientConnectListen()
        {
            while (true)
            {
                //为新的客户端连接创建一个Socket对象
                Socket clientSocket = m_serverSocket.Accept();
                m_clientSocketList.Add(clientSocket);
                Console.WriteLine("客户端{0}成功连接", clientSocket.RemoteEndPoint.ToString());

                //向连接的客户端发送连接成功的数据
                NetBufferWriter writer = new NetBufferWriter();
                writer.WriteString("Connected Server Success");
                clientSocket.Send(writer.Finish());

                //每个客户端连接创建一个线程来接受该客户端发送的消息
                Thread thread = new Thread(RecieveMessage);
                thread.Start(clientSocket);
            }
            // ReSharper disable once FunctionNeverReturns
        }