Esempio n. 1
0
        ///<summary>
        /// Handles clientcommuniction
        /// <param name="_client"> _client which communication should be handled</param>
        ///</summary>
        private void HandleClientCommunication(Object _client)
        {
            /* TCPClient and Stream */
            TcpClient     tcpClient    = (TcpClient)_client;
            clientStream = tcpClient.GetStream();

            /* SocketInfo */
            SocketInfo socketInfo = SocketInfo.getSocketInfo(tcpClient);

            /* Client Message */
            ClientMessage clientMessage = new ClientMessage();
            clientMessage.SocketInfo = SocketInfo.getSocketInfo(tcpClient);

            /* buffer and received_message */
            Byte[] message_byte = new byte[this.MaxMessageSize];
            Int32  bytesRead;

            while ( this.Status != ServerStatus.STOPPED )
            {
                if (this.Status == ServerStatus.RUNNING)
                {
                    bytesRead = new Int32();

                    try
                    {
                        // blocks until a client sends a message
                        bytesRead = clientStream.Read(message_byte, 0, this.MaxMessageSize);
                    }

                    catch
                    {
                        // a socket error has occured
                        // throw new Exception("Server: " + "a socket error has occured; Client: " + tcpClient.Client.LocalEndPoint.ToString() + " has interrupted the connection");
                        TCPServer_StreamBasedClient_EventArgs_ClienConnectionAbort clientConnectionAborted_Event = new TCPServer_StreamBasedClient_EventArgs_ClienConnectionAbort(tcpClient);
                        this.ClientConnectionAborted(this, clientConnectionAborted_Event);

                        System.Console.WriteLine("Server: " + "a socket error has occured; Client: " + "adresse" + " has interrupted the connection");
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        // the client has disconnected from the server
                        // throw new Exception("Server: " + "a client " + tcpClient.Client.RemoteEndPoint.ToString() + " has disconnected from the server");
                        TCPServer_StreamBasedClient_EventArgs_ClientDisconnect clientDisconnect_Event = new TCPServer_StreamBasedClient_EventArgs_ClientDisconnect(tcpClient);
                        this.ClientDisconnect(this, clientDisconnect_Event);

                        System.Console.WriteLine("Server: " + "a client " + "adresse" + " has disconnected from the server");
                        break;
                    }

                    /* recieved Message from Client */
                    clientMessage.Message = this.MessageEncoding.GetString(message_byte, 0, bytesRead);

                    /* Call the Event MessageRecieved */
                    TCPServer_EventArgs_MessageRecieved tcpServer_EventArgs_MessageRecieved = new TCPServer_EventArgs_MessageRecieved(clientMessage);
                    this.MessageRecieved(this, tcpServer_EventArgs_MessageRecieved);

                }
            }

            // Close open connections
            if (clientStream != null)
            {
                clientStream.Flush();
                clientStream.Close();
            }

            // if the tcpClient isn't null
            if (tcpClient != null)
            {
                // remove tcpClient from list clients
                this.clients.RemoveAt(this.clients.IndexOf(tcpClient));
            }

            // if the tcpClient is still connected
            if (tcpClient.Connected)
            {
                // close the tcpClient
                tcpClient.Close();
            }

            // remove this thread from list clientsThread
            this.clientThreads.RemoveAt(this.clientThreads.IndexOf(Thread.CurrentThread));
        }
Esempio n. 2
0
        ///<summary>
        /// Handles clientcommuniction
        /// <param name="_client"> _client which communication should be handled</param>
        ///</summary>
        private void HandleClientCommunication(Object _client)
        {
            /* TCPClient and Stream */
            TcpClient tcpClient = (TcpClient)_client;

            clientStream = tcpClient.GetStream();

            /* SocketInfo */
            SocketInfo socketInfo = SocketInfo.getSocketInfo(tcpClient);

            /* Client Message */
            ClientMessage clientMessage = new ClientMessage();

            clientMessage.SocketInfo = SocketInfo.getSocketInfo(tcpClient);

            /* buffer and received_message */
            Byte[] message_byte = new byte[this.MaxMessageSize];
            Int32  bytesRead;


            while (this.Status != ServerStatus.STOPPED)
            {
                if (this.Status == ServerStatus.RUNNING)
                {
                    bytesRead = new Int32();

                    try
                    {
                        // blocks until a client sends a message
                        bytesRead = clientStream.Read(message_byte, 0, this.MaxMessageSize);
                    }

                    catch
                    {
                        // a socket error has occured
                        // throw new Exception("Server: " + "a socket error has occured; Client: " + tcpClient.Client.LocalEndPoint.ToString() + " has interrupted the connection");
                        TCPServer_StreamBasedClient_EventArgs_ClienConnectionAbort clientConnectionAborted_Event = new TCPServer_StreamBasedClient_EventArgs_ClienConnectionAbort(tcpClient);
                        this.ClientConnectionAborted(this, clientConnectionAborted_Event);

                        System.Console.WriteLine("Server: " + "a socket error has occured; Client: " + "adresse" + " has interrupted the connection");
                        break;
                    }


                    if (bytesRead == 0)
                    {
                        // the client has disconnected from the server
                        // throw new Exception("Server: " + "a client " + tcpClient.Client.RemoteEndPoint.ToString() + " has disconnected from the server");
                        TCPServer_StreamBasedClient_EventArgs_ClientDisconnect clientDisconnect_Event = new TCPServer_StreamBasedClient_EventArgs_ClientDisconnect(tcpClient);
                        this.ClientDisconnect(this, clientDisconnect_Event);

                        System.Console.WriteLine("Server: " + "a client " + "adresse" + " has disconnected from the server");
                        break;
                    }


                    /* recieved Message from Client */
                    clientMessage.Message = this.MessageEncoding.GetString(message_byte, 0, bytesRead);

                    /* Call the Event MessageRecieved */
                    TCPServer_EventArgs_MessageRecieved tcpServer_EventArgs_MessageRecieved = new TCPServer_EventArgs_MessageRecieved(clientMessage);
                    this.MessageRecieved(this, tcpServer_EventArgs_MessageRecieved);
                }
            }



            // Close open connections
            if (clientStream != null)
            {
                clientStream.Flush();
                clientStream.Close();
            }

            // if the tcpClient isn't null
            if (tcpClient != null)
            {
                // remove tcpClient from list clients
                this.clients.RemoveAt(this.clients.IndexOf(tcpClient));
            }

            // if the tcpClient is still connected
            if (tcpClient.Connected)
            {
                // close the tcpClient
                tcpClient.Close();
            }

            // remove this thread from list clientsThread
            this.clientThreads.RemoveAt(this.clientThreads.IndexOf(Thread.CurrentThread));
        }