{"record":{"id":"157657792ab369de","repo":"2dust/v2rayN","slug":"failed-to-establish-udp-association-with-socks5-pr","errorCode":null,"errorMessage":"Failed to establish UDP association with SOCKS5 proxy.","messagePattern":"Failed to establish UDP association with SOCKS5 proxy\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v2rayN/ServiceLib.UdpTest/UdpTestService.cs","lineNumber":103,"sourceCode":"        }\n\n        // No port specified, use default\n        return (targetServerHost, _udpTest.GetDefaultTargetPort());\n    }\n\n    public async Task<TimeSpan> SendUdpRequestAsync(string targetServerHost, int socks5Port, TimeSpan operationTimeout)\n    {\n        using var cts = new CancellationTokenSource(operationTimeout);\n        var cancellationToken = cts.Token;\n        var udpRequestPacket = _udpTest.BuildUdpRequestPacket();\n        if (udpRequestPacket == null || udpRequestPacket.Length == 0)\n        {\n            throw new InvalidOperationException(\"Failed to build UDP request packet.\");\n        }\n        using var channel = new Socks5UdpChannel(\"127.0.0.1\", socks5Port);\n        if (!await channel.EstablishUdpAssociationAsync(cancellationToken).ConfigureAwait(false))\n        {\n            throw new Exception(\"Failed to establish UDP association with SOCKS5 proxy.\");\n        }\n\n        var (targetHost, targetPort) = ParseHostAndPort(targetServerHost);\n\n        byte[] udpReceiveResult = null;\n\n        // Get minimum round trip time from two attempts\n        var roundTripTime = TimeSpan.MaxValue;\n\n        for (var attempt = 0; attempt < 2; attempt++)\n        {\n            try\n            {\n                var stopwatch = new Stopwatch();\n                stopwatch.Start();\n                await channel.SendAsync(targetHost, targetPort, udpRequestPacket).ConfigureAwait(false);\n                var (_, receiveResult) = await channel.ReceiveAsync(cancellationToken).ConfigureAwait(false);\n                stopwatch.Stop();","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/2dust/v2rayN/blob/e01717d8326a4f5060b335523590c5fda943fe03/v2rayN/ServiceLib.UdpTest/UdpTestService.cs#L85-L121","documentation":"Thrown when EstablishUdpAssociationAsync returns false, meaning the TCP control handshake to the SOCKS5 proxy failed at some stage: TCP connect, method negotiation, UDP ASSOCIATE command reply, or parsing the relay bind address. It is a generic catch-all because the handshake returns false for several distinct failures rather than throwing.","triggerScenarios":"SendUdpRequestAsync opens a Socks5UdpChannel on 127.0.0.1:socks5Port and awaits EstablishUdpAssociationAsync; it returns false due to a SocketException on connect, a bad SOCKS5 version/no-auth reply (ver!=0x05 or method!=0x00), a non-success UDP ASSOCIATE reply (rep!=0x00), or an unparseable relay address.","commonSituations":"SOCKS5 proxy not running on the expected local port; proxy requires auth (no 'no-auth' method 0x00); proxy does not support UDP ASSOCIATE; wrong port passed (e.g. HTTP/SOCKS inbound port instead of SOCKS5 inbound); firewall/loopback binding issue; proxy crashed or still starting.","solutions":["Confirm a SOCKS5 inbound listener is up on socks5Port (e.g. netstat/ss, or a SOCKS5 TCP curl through it).","Ensure the proxy offers 'no-auth' (method 0x00); this channel sends only [0x05,0x01,0x00] and rejects any other method.","Verify socks5Port is the SOCKS5 inbound port, not an HTTP/mixed inbound.","Retry after the proxy has fully started, or increase the start-up wait before invoking SendUdpRequestAsync.","If the proxy only supports username/password auth, extend the handshake to offer method 0x02."],"exampleFix":"// before - single attempt, opaque failure\nif (!await channel.EstablishUdpAssociationAsync(ct))\n{\n    throw new Exception(\"Failed to establish UDP association with SOCKS5 proxy.\");\n}\n\n// after - retry once and distinguish failure stages\nvar ok = false;\nfor (var i = 0; i < 2 && !ok; i++)\n{\n    ok = await channel.EstablishUdpAssociationAsync(ct);\n}\nif (!ok) throw new Exception(\"SOCKS5 UDP ASSOCIATE handshake failed; verify the proxy offers no-auth and supports UDP on port \" + socks5Port);","handlingStrategy":"retry","validationCode":"// Pre-flight: confirm a SOCKS5 listener is up before the UDP test\nusing var probe = new TcpClient();\ntry { await probe.ConnectAsync(\"127.0.0.1\", socks5Port, ct); }\ncatch { throw new Exception($\"No SOCKS5 listener on 127.0.0.1:{socks5Port}.\"); }","typeGuard":"static bool IsPlausibleSocks5Port(int port) => port > 0 && port < 65536;","tryCatchPattern":"try\n{\n    if (!await channel.EstablishUdpAssociationAsync(ct))\n        throw new Exception(\"SOCKS5 UDP ASSOCIATE handshake failed; verify no-auth + UDP support on port \" + socks5Port);\n}\ncatch (SocketException ex)\n{\n    // proxy not reachable / refused; retry after proxy startup or report\n}","preventionTips":["Confirm the SOCKS5 inbound offers 'no-auth' (method 0x00).","Ensure UDP ASSOCIATE is supported by the core/proxy and UDP egress is allowed.","Pass the SOCKS5 inbound port (not HTTP/mixed) as socks5Port.","Allow the proxy to fully start before testing."],"tags":["socks5","udp","handshake","proxy","network"],"backgroundTag":null,"analyzedSha":"e01717d8326a4f5060b335523590c5fda943fe03","analyzedAt":"2026-08-13T10:10:23.416Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}