{"record":{"id":"19dc2d964cb50357","repo":"Anuken/Mindustry","slug":"alreadyconnected","errorCode":null,"errorMessage":"alreadyconnected","messagePattern":"alreadyconnected","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"core/src/mindustry/net/Net.java","lineNumber":180,"sourceCode":"        active = true;\n        server = false;\n    }\n\n    /**\n     * Connect to an address.\n     */\n    public void connect(String ip, int port, Runnable success){\n        streams.clear();\n        currentStream = null;\n\n        try{\n            if(!active){\n                Events.fire(new ClientServerConnectEvent(ip, port));\n                provider.connectClient(ip, port, success);\n                active = true;\n                server = false;\n            }else{\n                throw new IOException(\"alreadyconnected\");\n            }\n        }catch(IOException e){\n            showError(e);\n        }\n    }\n\n    /**\n     * Host a server at an address.\n     */\n    public void host(int port) throws IOException{\n        provider.hostServer(port);\n        active = true;\n        server = true;\n\n        Time.runTask(60f, platform::updateRPC);\n    }\n\n    /**","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/Anuken/Mindustry/blob/f695ad7e60323ebced984fa26d0bcf0bc54296b4/core/src/mindustry/net/Net.java#L162-L198","documentation":"Net.connect rejects a second connection: if Net is already active (connected or hosting) it throws IOException 'alreadyconnected' rather than allowing overlapping connections. The client must disconnect first.","triggerScenarios":"Net.connect(ip, port, success) is called while Net.active is true (already connected or hosting).","commonSituations":"Double-clicking join; retrying connect without disconnect; joining while still closing a previous session; connect issued while hosting.","solutions":["Disconnect (Net.disconnect()) before connecting again.","Guard the call: check !Net.active() (and not hosting) first.","Debounce the connect UI action to prevent double-invocation."],"exampleFix":"// before\nNet.connect(ip, port, this::onConnected); // throws if already active\n\n// after\nif (!Net.active()) {\n    Net.connect(ip, port, this::onConnected);\n}","handlingStrategy":"validation","validationCode":"// Prevent overlapping connections.\nif(Net.active()) {\n    throw new IllegalStateException(\"Already connected/hosting; disconnect first.\");\n}\nNet.connect(ip, port, success);","typeGuard":"boolean canConnect(){ return !Net.active(); }","tryCatchPattern":"try {\n    Net.connect(ip, port, success);\n} catch(IOException e) {\n    if(\"alreadyconnected\".equals(e.getMessage())) { Net.disconnect(); Net.connect(ip, port, success); }\n    else throw e;\n}","preventionTips":["Disconnect before (re)connecting.","Guard connect calls with !Net.active().","Debounce join UI to avoid double-invocation."],"tags":["networking","state","client","validation"],"backgroundTag":null,"analyzedSha":"f695ad7e60323ebced984fa26d0bcf0bc54296b4","analyzedAt":"2026-08-14T04:31:16.262Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}