Untitled

mail@pastecode.io avatar
unknown
csharp
8 months ago
1.1 kB
5
Indexable
Never
 public class Program
    {
        private static TcpListener _tcpListener;

        static void Main(string[] args)
        {
            StartServer();

            Console.ReadLine();
        }

        public static void StartServer()
        {
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            int port = 8888;

            _tcpListener = new TcpListener(ipAddress, port);
            _tcpListener.Start();

            Console.WriteLine("Listening on port 8888.");

            while (true)
            {
                TcpClient client = _tcpListener.AcceptTcpClient();
                Console.WriteLine("Client was connected.");

                NetworkStream stream = client.GetStream();

                byte[] nameBuffer = new byte[4096];
                int nameBytesRead = stream.Read(nameBuffer, 0, 4096);
                string nameClient = Encoding.ASCII.GetString(nameBuffer, 0, nameBytesRead);
                Console.WriteLine(nameClient);
            }

            _tcpListener.Stop();
        }
    }
Leave a Comment