egametang/ET · critical · Exception

bind error: {ipEndPoint}

Error message

bind error: {ipEndPoint}

What it means

TService constructor binds a TCP acceptor socket to ipEndPoint and wraps any Bind failure. Like the UDP case, it fails when the port is taken, the address is not local, or privileges are insufficient.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Network/TService.cs:57

		public TService(AddressFamily addressFamily, ServiceType serviceType)
		{
			this.ServiceType = serviceType;
		}

		public TService(IPEndPoint ipEndPoint, ServiceType serviceType)
		{
			this.ServiceType = serviceType;
			this.acceptor = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
			// 容易出问题,先注释掉,按需开启
			//this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
			this.innArgs.Completed += this.OnComplete;
			try
			{
				this.acceptor.Bind(ipEndPoint);
			}
			catch (Exception e)
			{
				throw new Exception($"bind error: {ipEndPoint}", e);
			}
			
			this.acceptor.Listen(1000);
			
			this.AcceptAsync();
		}

		public override IPEndPoint GetBindPoint()
		{
			return this.acceptor.LocalEndPoint as IPEndPoint;
		}

		private void OnComplete(object sender, SocketAsyncEventArgs e)
		{
			switch (e.LastOperation)
			{
				case SocketAsyncOperation.Accept:
					this.Queue.Enqueue(new TArgs() {SocketAsyncEventArgs = e});

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Free the port (kill the other process) before binding.
  2. Use IPAddress.Any or a confirmed local interface address.
  3. Inspect the wrapped SocketException.SocketErrorCode (AddressAlreadyInUse, AddressNotAvailable).

Example fix

// before
this.acceptor.Bind(ipEndPoint);
// after
try { this.acceptor.Bind(ipEndPoint); }
catch (SocketException sx) { throw new Exception($"tcp bind {ipEndPoint} failed {sx.SocketErrorCode}", sx); }
Defensive patterns

Strategy: try-catch

Validate before calling

static bool IsPortFreeTCP(IPEndPoint ep)
{
    try { using var s = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); s.Bind(ep); return true; }
    catch { return false; }
}

Type guard

null

Try / catch

try { service = new TService(ipEndPoint, serviceType); }
catch (Exception e) when (e.Message.StartsWith("bind error"))
{ Log.Error($"tcp bind {ipEndPoint} failed: {e.InnerException?.Message}"); throw; }

Prevention

When it happens

Trigger: A previous instance still holding the TCP port, binding to an IP not assigned to the host, or two services configured to the same outer/inner port.

Common situations: Hot-restart racing the OS TIME_WAIT release, duplicate port in config, another dev server instance running.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/017ba8f18b4cc8c3. Report an issue: GitHub.