We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If port is 0, can the server bind to a random free port?
Android
private static int findFreePort() { ServerSocket socket = null; try { socket = new ServerSocket(0); socket.setReuseAddress(true); int port = socket.getLocalPort(); try { socket.close(); } catch (IOException e) { // Ignore IOException on close() } return port; } catch (IOException e) { } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { } } } throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on"); }
iOS
#import <netinet/in.h> - (NSUInteger) _availablePort { struct sockaddr_in addr4; bzero(&addr4, sizeof(addr4)); addr4.sin_len = sizeof(addr4); addr4.sin_family = AF_INET; addr4.sin_port = 0; // set to 0 and bind to find available port addr4.sin_addr.s_addr = htonl(INADDR_ANY); int listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (bind(listeningSocket, (const void*)&addr4, sizeof(addr4)) == 0) { struct sockaddr addr; socklen_t addrlen = sizeof(addr); if (getsockname(listeningSocket, &addr, &addrlen) == 0) { struct sockaddr_in* sockaddr = (struct sockaddr_in*)&addr; close(listeningSocket); return ntohs(sockaddr->sin_port); } } return 0; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
If port is 0, can the server bind to a random free port?
Android
iOS
The text was updated successfully, but these errors were encountered: