Created
February 10, 2015 01:39
-
-
Save technion/5322442ef4298755b90b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Example socket code - find a free port | |
* [email protected] | |
* 10/2/2015 | |
* Example only - not production tested | |
*/ | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#define STARTPORT 8081 /* Start port range to check for a free port */ | |
#define ENDPOINT 8090 | |
int bind_open_port(int sock); | |
int main() | |
{ | |
int sock; | |
uint16_t port; | |
/* Generic socket creation, ipv4, TCP */ | |
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (sock < 0) { | |
perror("Failed to create socket"); | |
exit(EXIT_FAILURE); | |
} | |
port = bind_open_port(sock); | |
printf("Successfully bound to socket %d\n", port); | |
(void)close(sock); | |
return 0; | |
} | |
/* Cycles from STARTPORT to ENDPORT, looking for a port that is | |
* available for bind(). | |
* int sock: Valid socket | |
* Returns: port number, or -1 for error | |
*/ | |
int bind_open_port(int sock) | |
{ | |
struct sockaddr_in servAddr; | |
uint16_t port; | |
for(port = STARTPORT; port <= ENDPOINT; port++) { | |
memset(&servAddr, 0, sizeof(servAddr)); | |
servAddr.sin_family = AF_INET; | |
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
servAddr.sin_port = htons(port); | |
if (bind(sock, (struct sockaddr *) &servAddr, | |
sizeof(servAddr)) < 0) { | |
if(errno == EADDRINUSE) { | |
/* This is the "port in use" error */ | |
printf("Port %d is in use\n", port); | |
continue; | |
} | |
/* Reaching here indicates a different error */ | |
perror("Failed to bind"); | |
return(-1); | |
} | |
return port; | |
} | |
printf("Unable to find a valid port"); | |
return(-1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment