Build the server‑side program, bind an IP address and port via TcpListener, start port listening and wait for incoming TCP connections from clients. Two implementation versions are provided: Console Application and WinForms Window Application.
Core API Reference
IPAddress: Represents the local machine IP address.IPAddress.Parse("127.0.0.1")converts an IP string into an object.127.0.0.1: Local loopback address, accessible only from the local host. To allow connections from other devices on the LAN, use your local network IP such as192.168.x.x;IPAddress.Any: Listens on all network interface IP addresses of the host machine.
TcpListener: TCP server listener class. Constructor parameters:IP Address + Port Numberlistener.Start(): Start listening. Once called, the server begins waiting for client connections.
Console Version
using System;
using System.Net;
using System.Net.Sockets;
namespace TalkService
{
class Program
{
static void Main(string[] args)
{
// Create IP object
IPAddress ip = IPAddress.Parse("127.0.0.1");
// Create TCP listener listening on port 9500
TcpListener listener = new TcpListener(ip, 9500);
// Begin listening
listener.Start();
Console.WriteLine("Listening started... Press E to exit");
ConsoleKeyInfo info;
do
{
info = Console.ReadKey();
} while (info.Key != ConsoleKey.E);
listener.Stop(); // Stop listening on exit
}
}
}Code language: JavaScript (javascript)
This code only starts the listener. AcceptTcpClient has not been called to wait for client connections; it merely keeps the process running without terminating.
WinForms Version
UI Controls: (You can add controls to your WinForms form for setting IP, port, outgoing text content and more)
textBoxIP: IP input box (default127.0.0.1)textBoxPort: Port input box (default9500)buttonStart: Start buttontextBoxInfo: Information display textbox
using System;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace SocketChatServer
{
public partial class Form1 : Form
{
IPAddress ip;
TcpListener listener;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
// Parse IP and port
ip = IPAddress.Parse(this.textBoxIP.Text);
int port = Convert.ToInt32(this.textBoxPort.Text);
listener = new TcpListener(ip, port);
// Start listening
listener.Start();
// Output log message
string log = $"Server started {DateTime.Now.ToShortTimeString()}\r\n";
this.textBoxInfo.Text = log + this.textBoxInfo.Text;
}
}
}Code language: PHP (php)
Outstanding Issues
- UI Freeze Pitfall
listener.AcceptTcpClient()is a blocking method. Calling it directly from the main UI thread inside a button click handler will lock up your window. You need to spawn a new thread or useasync/awaitto await incoming client connections asynchronously. - Port Occupation
If port 9500 is occupied by another program, constructingnew TcpListener()will throw an exception. Wrap this section with try‑catch during development for error handling. - Cross‑host Connectivity
Binding to127.0.0.1works only for local testing. To accept connections from other LAN devices, set the IP to your machine’s local network address and allow port 9500 through your firewall.
Server‑Side Port Listening