{"record":{"id":"017ba8f18b4cc8c3","repo":"egametang/ET","slug":"bind-error-ipendpoint-017ba8","errorCode":null,"errorMessage":"bind error: {ipEndPoint}","messagePattern":"bind error: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Network/TService.cs","lineNumber":57,"sourceCode":"\t\tpublic TService(AddressFamily addressFamily, ServiceType serviceType)\n\t\t{\n\t\t\tthis.ServiceType = serviceType;\n\t\t}\n\n\t\tpublic TService(IPEndPoint ipEndPoint, ServiceType serviceType)\n\t\t{\n\t\t\tthis.ServiceType = serviceType;\n\t\t\tthis.acceptor = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);\n\t\t\t// 容易出问题，先注释掉，按需开启\n\t\t\t//this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);\n\t\t\tthis.innArgs.Completed += this.OnComplete;\n\t\t\ttry\n\t\t\t{\n\t\t\t\tthis.acceptor.Bind(ipEndPoint);\n\t\t\t}\n\t\t\tcatch (Exception e)\n\t\t\t{\n\t\t\t\tthrow new Exception($\"bind error: {ipEndPoint}\", e);\n\t\t\t}\n\t\t\t\n\t\t\tthis.acceptor.Listen(1000);\n\t\t\t\n\t\t\tthis.AcceptAsync();\n\t\t}\n\n\t\tpublic override IPEndPoint GetBindPoint()\n\t\t{\n\t\t\treturn this.acceptor.LocalEndPoint as IPEndPoint;\n\t\t}\n\n\t\tprivate void OnComplete(object sender, SocketAsyncEventArgs e)\n\t\t{\n\t\t\tswitch (e.LastOperation)\n\t\t\t{\n\t\t\t\tcase SocketAsyncOperation.Accept:\n\t\t\t\t\tthis.Queue.Enqueue(new TArgs() {SocketAsyncEventArgs = e});","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Network/TService.cs#L39-L75","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hot-restart racing the OS TIME_WAIT release, duplicate port in config, another dev server instance running.","solutions":["Free the port (kill the other process) before binding.","Use IPAddress.Any or a confirmed local interface address.","Inspect the wrapped SocketException.SocketErrorCode (AddressAlreadyInUse, AddressNotAvailable)."],"exampleFix":"// before\nthis.acceptor.Bind(ipEndPoint);\n// after\ntry { this.acceptor.Bind(ipEndPoint); }\ncatch (SocketException sx) { throw new Exception($\"tcp bind {ipEndPoint} failed {sx.SocketErrorCode}\", sx); }","handlingStrategy":"try-catch","validationCode":"static bool IsPortFreeTCP(IPEndPoint ep)\n{\n    try { using var s = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); s.Bind(ep); return true; }\n    catch { return false; }\n}","typeGuard":"null","tryCatchPattern":"try { service = new TService(ipEndPoint, serviceType); }\ncatch (Exception e) when (e.Message.StartsWith(\"bind error\"))\n{ Log.Error($\"tcp bind {ipEndPoint} failed: {e.InnerException?.Message}\"); throw; }","preventionTips":["Probe the port before constructing the service.","Ensure no duplicate instance is bound to the same port.","Bind to verified local addresses."],"tags":["network","tcp","socket","csharp"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}