Beispiel #1
0
 private void ContinueReadBuffer(TcpClientState internalClient, NetworkStream networkStream)
 {
     try
     {
         networkStream.BeginRead(internalClient.Buffer, 0, internalClient.Buffer.Length, HandleDatagramReceived, internalClient);
     }
     catch (ObjectDisposedException ex)
     {
         //ExceptionHandler.Handle(ex);
         Console.WriteLine(ex.ToString());
     }
 }
Beispiel #2
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (!IsRunning)
            {
                return;
            }

            try
            {
                TcpListener tcpListener = (TcpListener)ar.AsyncState;

                TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
                if (!tcpClient.Connected)
                {
                    return;
                }

                byte[]         buffer         = new byte[tcpClient.ReceiveBufferSize];
                TcpClientState internalClient = new TcpClientState(tcpClient, buffer);

                // add client connection to cache
                string tcpClientKey = internalClient.TcpClient.Client.RemoteEndPoint.ToString();
                _clients.AddOrUpdate(tcpClientKey, internalClient, (n, o) => { return(internalClient); });
                RaiseClientConnected(tcpClient);
                Interlocked.Increment(ref this.numConnectedSockets);
                Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
                                  this.numConnectedSockets);
                UpdateListView(internalClient.IP, list_C);
                // begin to read data
                NetworkStream networkStream = internalClient.NetworkStream;
                ContinueReadBuffer(internalClient, networkStream);

                // keep listening to accept next connection
                ContinueAcceptTcpClient(tcpListener);
            }
            catch (ObjectDisposedException ex)
            {
                Console.WriteLine("Program does not terminate normally! But Can be used normally.Error:ObjectDisposedExceptio");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Beispiel #3
0
        private void HandleDatagramReceived(IAsyncResult ar)
        {
            if (!IsRunning)
            {
                return;
            }
            TcpClientState internalClient = (TcpClientState)ar.AsyncState;

            try
            {
                if (!internalClient.TcpClient.Connected)
                {
                    Interlocked.Decrement(ref this.numConnectedSockets);
                    Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", this.numConnectedSockets);
                    UpdateListView(internalClient.IP, list_D);
                    return;
                }

                NetworkStream networkStream = internalClient.NetworkStream;

                int numberOfReadBytes = 0;
                try
                {
                    // if the remote host has shutdown its connection,
                    // read will immediately return with zero bytes.
                    numberOfReadBytes = networkStream.EndRead(ar);
                }
                catch (Exception ex)
                {
                    //ExceptionHandler.Handle(ex);
                    Console.WriteLine("A remote host has closed connection!!---" + internalClient.IP);
                    numberOfReadBytes = 0;
                }

                if (numberOfReadBytes == 0)
                {
                    // connection has been closed
                    TcpClientState internalClientToBeThrowAway;
                    string         tcpClientKey = internalClient.IP;
                    _clients.TryRemove(tcpClientKey, out internalClientToBeThrowAway);
                    RaiseClientDisconnected(internalClient.TcpClient);
                    //Console.WriteLine("Number:"+_clients.Count());
                    Interlocked.Decrement(ref this.numConnectedSockets);
                    Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", this.numConnectedSockets);
                    UpdateListView(tcpClientKey, list_D);
                    return;
                }

                // received byte and trigger event notification
                var receivedBytes = new byte[numberOfReadBytes];
                Array.Copy(internalClient.Buffer, 0, receivedBytes, 0, numberOfReadBytes);

                string msg = "Recevied:" + Encoding.ASCII.GetString(receivedBytes) + "  From:" + internalClient.IP;
                Console.WriteLine(msg);

                RaiseDatagramReceived(internalClient.TcpClient, receivedBytes);
                //RaisePlaintextReceived(internalClient.TcpClient, receivedBytes);

                // continue listening for tcp datagram packets
                ContinueReadBuffer(internalClient, networkStream);
            }
            catch (InvalidOperationException ex)
            {
                //ExceptionHandler.Handle(ex);
                Console.WriteLine(ex.ToString());
            }
        }