Ejemplo n.º 1
0
        public override void Run(int port)
        {
            if (client != null)
            {
                return;
            }

            Running = true;

            try
            {
                Log("Connecting client...");
                client = new TcpClient("127.0.0.1", port);

                Log("Connected!");
                stream = client.GetStream();

                UponConnected?.Invoke();

                int count;
                while (Running && client.Connected)
                {
                    count = stream.Read(inBuffer, 0, inBuffer.Length);

                    if (count == 0)
                    {
                        Log("Read message of length 0, quitting read.");
                        break;
                    }

                    NetData inData = new NetData(inBuffer, count);
                    UponMessage?.Invoke(inData);
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
            finally
            {
                Log("Shutting down...");

                if (client != null)
                {
                    client.Close();
                    client = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }

                Running = false;
            }
        }
Ejemplo n.º 2
0
        public override void Run(int port)
        {
            if (listener != null)
            {
                return;
            }

            Running = true;

            try
            {
                listener = new TcpListener(base.GetLocalAddress(), port);
                listener.Start(1);

                Log("Waiting for client connection...");
                client = listener.AcceptTcpClient();
                stream = client.GetStream();

                Log("Got client connection!");
                UponConnected?.Invoke();

                int count;
                while (Running && client.Connected)
                {
                    count = stream.Read(inBuffer, 0, inBuffer.Length);

                    if (count == 0)
                    {
                        Log("Read message of length 0, quitting read.");
                        break;
                    }

                    NetData inData = new NetData(inBuffer, count);
                    UponMessage?.Invoke(inData);
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
            finally
            {
                Log("Shutting down...");

                if (client != null)
                {
                    client.Close();
                    client = null;
                }

                if (listener != null)
                {
                    listener.Stop();
                    listener = null;
                }

                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }

                Running = false;
            }
        }