{"record":{"id":"53ffa8a2ff7e7900","repo":"EllanJiang/GameFramework","slug":"initialize-network-channel-failure-networkmanager","errorCode":null,"errorMessage":"Initialize network channel failure.","messagePattern":"Initialize network channel failure\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"critical","filePath":"GameFramework/Network/NetworkManager.TcpWithSyncReceiveNetworkChannel.cs","lineNumber":66,"sourceCode":"            /// 连接到远程主机。\n            /// </summary>\n            /// <param name=\"ipAddress\">远程主机的 IP 地址。</param>\n            /// <param name=\"port\">远程主机的端口号。</param>\n            /// <param name=\"userData\">用户自定义数据。</param>\n            public override void Connect(IPAddress ipAddress, int port, object userData)\n            {\n                base.Connect(ipAddress, port, userData);\n                m_Socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);\n                if (m_Socket == null)\n                {\n                    string errorMessage = \"Initialize network channel failure.\";\n                    if (NetworkChannelError != null)\n                    {\n                        NetworkChannelError(this, NetworkErrorCode.SocketError, SocketError.Success, errorMessage);\n                        return;\n                    }\n\n                    throw new GameFrameworkException(errorMessage);\n                }\n\n                m_NetworkChannelHelper.PrepareForConnecting();\n                ConnectAsync(ipAddress, port, userData);\n            }\n\n            protected override bool ProcessSend()\n            {\n                if (base.ProcessSend())\n                {\n                    SendAsync();\n                    return true;\n                }\n\n                return false;\n            }\n\n            protected override void ProcessReceive()","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Network/NetworkManager.TcpWithSyncReceiveNetworkChannel.cs#L48-L84","documentation":"Identical to the TcpNetworkChannel guard but in TcpWithSyncReceiveNetworkChannel.Connect: the Socket created for the synchronous-receive TCP channel was null, so GameFramework throws GameFrameworkException (or fires NetworkChannelError with NetworkErrorCode.SocketError). The channel cannot proceed to ConnectAsync without a socket.","triggerScenarios":"new Socket(addressFamily, Stream, Tcp) failing/returning null in the sync-receive channel variant; unsupported address family; platform without TCP socket support.","commonSituations":"Using the sync-receive channel on platforms where socket allocation fails; IPv6-only addresses on IPv4-only devices; restricted sandbox environments.","solutions":["Use an IPv4 address or ensure the OS/device supports the requested AddressFamily.","Confirm the target platform supports TCP sockets (not WebGL); switch to the async TcpNetworkChannel where appropriate.","Handle NetworkChannelError (SocketError) with a user-visible failure and retry flow.","Check that no custom socket replacement/factory is returning null."],"exampleFix":"// before\nsyncChannel.Connect(ipAddress, port, userData); // socket allocation failed\n// after\ntry { syncChannel.Connect(ipAddress, port, userData); }\ncatch (GameFrameworkException) { ShowConnectionError(); }","handlingStrategy":"try-catch","validationCode":"if (!Socket.OSSupportsIPv4 && !Socket.OSSupportsIPv6) return false; // sockets unavailable","typeGuard":null,"tryCatchPattern":"try { syncChannel.Connect(ip, port, userData); } catch (GameFrameworkException ex) when (ex.Message == \"Initialize network channel failure.\") { FallbackToOtherChannelOrReport(); }","preventionTips":["Confirm the platform supports TCP before choosing the sync-receive channel","Check address family support before Connect","Register NetworkChannelError for SocketError handling","Keep a fallback channel type for restricted environments"],"tags":["network","tcp","socket-init","gameframework"],"backgroundTag":"module-init-failed","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}