Exemple #1
0
        private void ReceiveMessage(IAsyncResult iar)
        {
            SocketConnectInstance sci = (SocketConnectInstance)iar.AsyncState;
            Socket clientSocket       = sci.Socket;
            int    size = clientSocket.EndReceive(iar);
            string msg  = Encoding.UTF8.GetString(sci.Buffer, 0, size);

            Console.WriteLine("{0}:{1} Receive Message:{2}", DateTime.Now, sci.Id, msg);
            if ("quit".Equals(msg) || String.Empty.Equals(msg))
            {
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                lock (Mutex)
                {
                    if (socketPool.ContainsKey(sci.Id))
                    {
                        socketPool.Remove(sci.Id);
                        Console.WriteLine("Close client socket({0}) successfully");
                    }
                }
                return;
            }
            if (clientSocket.Connected)
            {
                this.Send(clientSocket, Encoding.UTF8.GetBytes("Get message"));
                sci.Socket.BeginReceive(sci.Buffer, 0, SocketConnectInstance.MAX_BUFFER_SIZE, 0, new AsyncCallback(ReceiveMessage), sci);
            }
        }
Exemple #2
0
        public void Start()
        {
            IPAddress  address = IPAddress.Parse(config.IP);
            Socket     socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep     = new IPEndPoint(address, config.Port);

            socket.Bind(iep);
            socket.Listen(1024);
            Console.WriteLine("Server is running at port:{0}....", this.config.Port);
            while (true)
            {
                Console.WriteLine("Waiting for a client...");
                Socket clientSocket       = socket.Accept();
                SocketConnectInstance sci = new SocketConnectInstance(clientSocket);
                sci.Shutdown += this.ShutdownClient;
                lock (Mutex)
                {
                    int count = socketPool.Count + 1;
                    if (count > MAX_CONNECTION)
                    {
                        clientSocket.Shutdown(SocketShutdown.Both);
                        clientSocket.Close();
                    }
                    else
                    {
                        Console.WriteLine("Eastabled with a client(id={0}) and will receive message", sci.Id);
                        socketPool.Add(sci.Id, sci);
                        clientSocket.BeginReceive(sci.Buffer, 0, SocketConnectInstance.MAX_BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveMessage), sci);
                        Console.WriteLine("There are {0} active connection in pool", socketPool.Count);
                    }
                }
            }
        }
Exemple #3
0
        public void Start()
        {
            Console.WriteLine("start client");
            Socket     clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress  address      = IPAddress.Parse(this.config.IP);
            IPEndPoint iep          = new IPEndPoint(address, this.config.Port);

            clientSocket.Connect(iep);
            SocketConnectInstance sci = new SocketConnectInstance(clientSocket);

            clientSocket.BeginReceive(sci.Buffer, SocketConnectInstance.MAX_BUFFER_SIZE, 0, SocketFlags.None, new AsyncCallback(ReceiveMessage), sci);
            this.Timer = new Timer(new TimerCallback(this.HeartBeat), clientSocket, 5000, 5000);
            Console.WriteLine("Connection is establed");
            while (true)
            {
                Console.WriteLine("Please input message to send to server...");
                string msg = Console.ReadLine();
                if (clientSocket.Connected)
                {
                    clientSocket.Send(Encoding.UTF8.GetBytes(msg));
                }
                else
                {
                    break;
                }
                if (String.Empty.Equals(msg) || "quit".Equals(msg.ToLower()))
                {
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                    Console.WriteLine("Client quit");
                    break;
                }
            }
        }
Exemple #4
0
        private void ReceiveMessage(IAsyncResult iar)
        {
            SocketConnectInstance sci = (SocketConnectInstance)iar.AsyncState;
            Socket clientSocket       = sci.Socket;
            int    size = clientSocket.EndReceive(iar);
            string msg  = Encoding.UTF8.GetString(sci.Buffer, 0, size);

            Console.WriteLine("{0}:{1} Receive Message:{2}", DateTime.Now, sci.Id, msg);
            if ("quit".Equals(msg))
            {
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                return;
            }
            sci.Socket.BeginReceive(sci.Buffer, 0, SocketConnectInstance.MAX_BUFFER_SIZE, 0, new AsyncCallback(ReceiveMessage), sci);
        }
Exemple #5
0
        private void AcceptConnection(IAsyncResult iar)
        {
            Socket serverSocket       = (Socket)iar.AsyncState;
            Socket clientSocket       = serverSocket.EndAccept(iar);
            SocketConnectInstance sci = new SocketConnectInstance(clientSocket);

            sci.Shutdown += this.ShutdownClient;
            lock (Mutex)
            {
                int index = socketPool.Count + 1;
                if (index > MAX_CONNECTION)
                {
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
                socketPool.Add(sci.Id, sci);
            }            Thread thread = new Thread(sci.ReceiveMessage);

            thread.Start();
        }