To create a TCP server on a specific port in C#, you can use the TcpListener class from the System.Net.Sockets namespace.
The following code snippet demonstrates how to create a TCP server on port 8080:
main.cs895 chars33 linesIn this example, ipAddress is set to IPAddress.Any, which means the server will listen for incoming connections on all available network interfaces. If you want to restrict the server to a specific network interface, you can set ipAddress to the IP address of that interface.
The TcpListener instance is then created with the ipAddress and port parameters. The listener.Start() method is called to begin listening for incoming connections on this endpoint.
The while (true) loop listens for incoming client connections using the listener.AcceptTcpClient() method, which blocks until a client connects. When a client connects, a new TcpClient instance is returned, representing the connected client.
You can then handle the client connection in a separate thread or just run the code sequentially to handle it inline with the loop.
gistlibby LogSnag