{"record":{"id":"9a810f8a1da37b51","repo":"TooTallNate/Java-WebSocket","slug":"could-not-get-address-of-channel-passed-to-websock","errorCode":null,"errorMessage":"Could not get address of channel passed to WebSocketServer, make sure it is bound","messagePattern":"Could not get address of channel passed to WebSocketServer, make sure it is bound","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/server/WebSocketServer.java","lineNumber":191,"sourceCode":"   * @param address      The address (host:port) this server should listen on.\n   * @param decodercount The number of {@link WebSocketWorker}s that will be used to process the\n   *                     incoming network data. By default this will be <code>Runtime.getRuntime().availableProcessors()</code>\n   * @param drafts       The versions of the WebSocket protocol that this server instance should\n   *                     comply to. Clients that use an other protocol version will be rejected.\n   * @see #WebSocketServer(InetSocketAddress, int, List, Collection) more details here\n   */\n  public WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts) {\n    this(address, decodercount, drafts, new HashSet<WebSocket>());\n  }\n\n  // Small internal helper function to get around limitations of Java constructors.\n  private static InetSocketAddress checkAddressOfExistingChannel(ServerSocketChannel existingChannel) {\n    assert existingChannel.isOpen();\n    SocketAddress addr;\n    try {\n      addr = existingChannel.getLocalAddress();\n    } catch (IOException e) {\n      throw new IllegalArgumentException(\"Could not get address of channel passed to WebSocketServer, make sure it is bound\", e);\n    }\n    if (addr == null) {\n      throw new IllegalArgumentException(\"Could not get address of channel passed to WebSocketServer, make sure it is bound\");\n    }\n    return (InetSocketAddress)addr;\n  }\n\n  /**\n   * @param existingChannel An already open and bound server socket channel, which this server will use.\n   * For example, it can be System.inheritedChannel() to implement socket activation.\n   */\n  public WebSocketServer(ServerSocketChannel existingChannel) {\n    this(checkAddressOfExistingChannel(existingChannel));\n    this.server = existingChannel;\n  }\n\n  /**\n   * Creates a WebSocketServer that will attempt to bind/listen on the given <var>address</var>, and","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/server/WebSocketServer.java#L173-L209","documentation":"WebSocketServer's constructor can accept an existing, already-bound ServerSocketChannel. checkAddressOfExistingChannel() retrieves the channel's local address to know the bind address; if getLocalAddress() throws IOException or returns null (channel open but not bound), it throws IllegalArgumentException telling you the channel must be bound.","triggerScenarios":"Passing a ServerSocketChannel to the WebSocketServer constructor that was opened but never bound (socketChannel.bind(...)), or whose bind failed / was closed after opening.","commonSituations":"Advanced setups sharing a pre-bound channel with other code, where the bind call was skipped, failed silently, or happened after constructing the server.","solutions":["Bind the channel before constructing WebSocketServer: channel.bind(new InetSocketAddress(port))","Verify channel.getLocalAddress() returns a non-null address before passing the channel in","Ensure nothing unbinds/closes the channel between binding and server construction","If you don't need an existing channel, use the WebSocketServer(address) constructors that bind internally"],"exampleFix":"// before\nServerSocketChannel ch = ServerSocketChannel.open();\nWebSocketServer server = new MyServer(new InetSocketAddress(8887), Collections.singletonList(ch));\n// after\nServerSocketChannel ch = ServerSocketChannel.open();\nch.bind(new InetSocketAddress(8887));\nWebSocketServer server = new MyServer(new InetSocketAddress(8887), Collections.singletonList(ch));","handlingStrategy":"validation","validationCode":"assert channel.isOpen();\nif (channel.getLocalAddress() == null) {\n  throw new IllegalStateException(\"ServerSocketChannel must be bound before passing to WebSocketServer\");\n}","typeGuard":"boolean isBound(ServerSocketChannel ch) throws IOException {\n  return ch != null && ch.isOpen() && ch.getLocalAddress() != null;\n}","tryCatchPattern":"try {\n  WebSocketServer server = new MyServer(addr, Collections.singletonList(channel));\n} catch (IllegalArgumentException e) {\n  // channel unbound: bind it or construct WebSocketServer from an address instead\n}","preventionTips":["Always bind() a ServerSocketChannel before handing it to the WebSocketServer constructor","Prefer the address-based WebSocketServer constructors unless you must share the channel","Check nothing closes or reconfigures the channel between bind and server start"],"tags":["websocket","server","socket","bind"],"backgroundTag":"socket-not-bound","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}