Anuken/Mindustry · error · IOException

alreadyconnected

Error message

alreadyconnected

What it means

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.

Source

Thrown at core/src/mindustry/net/Net.java:180

        active = true;
        server = false;
    }

    /**
     * Connect to an address.
     */
    public void connect(String ip, int port, Runnable success){
        streams.clear();
        currentStream = null;

        try{
            if(!active){
                Events.fire(new ClientServerConnectEvent(ip, port));
                provider.connectClient(ip, port, success);
                active = true;
                server = false;
            }else{
                throw new IOException("alreadyconnected");
            }
        }catch(IOException e){
            showError(e);
        }
    }

    /**
     * Host a server at an address.
     */
    public void host(int port) throws IOException{
        provider.hostServer(port);
        active = true;
        server = true;

        Time.runTask(60f, platform::updateRPC);
    }

    /**

View on GitHub (pinned to f695ad7e60)

Solutions

  1. Disconnect (Net.disconnect()) before connecting again.
  2. Guard the call: check !Net.active() (and not hosting) first.
  3. Debounce the connect UI action to prevent double-invocation.

Example fix

// before
Net.connect(ip, port, this::onConnected); // throws if already active

// after
if (!Net.active()) {
    Net.connect(ip, port, this::onConnected);
}
Defensive patterns

Strategy: validation

Validate before calling

// Prevent overlapping connections.
if(Net.active()) {
    throw new IllegalStateException("Already connected/hosting; disconnect first.");
}
Net.connect(ip, port, success);

Type guard

boolean canConnect(){ return !Net.active(); }

Try / catch

try {
    Net.connect(ip, port, success);
} catch(IOException e) {
    if("alreadyconnected".equals(e.getMessage())) { Net.disconnect(); Net.connect(ip, port, success); }
    else throw e;
}

Prevention

When it happens

Trigger: Net.connect(ip, port, success) is called while Net.active is true (already connected or hosting).

Common situations: Double-clicking join; retrying connect without disconnect; joining while still closing a previous session; connect issued while hosting.

Related errors


AI-assisted analysis of Anuken/Mindustry@f695ad7e60 (2026-08-14). Data as JSON: /api/errors/19dc2d964cb50357. Report an issue: GitHub.