-
-
Save wankdanker/1336326 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
/* | |
* Copyright (c) 2011, Ben Noordhuis <[email protected]> | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any | |
* purpose with or without fee is hereby granted, provided that the above | |
* copyright notice and this permission notice appear in all copies. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#define assert_errno() \ | |
do { \ | |
if (errno != 0) { \ | |
fprintf(stderr, \ | |
"%s:%d: %s\n", \ | |
__FILE__, \ | |
__LINE__, \ | |
strerror(errno)); \ | |
abort(); \ | |
} \ | |
} \ | |
while (0); | |
#define E(expr) (expr); assert_errno(); | |
static int do_bind(int family) { | |
struct sockaddr_storage storage; | |
socklen_t socklen; | |
int sockfd; | |
assert(family == AF_INET || family == AF_INET6); | |
E(sockfd = socket(family, SOCK_DGRAM, 0)); | |
#if REUSEADDR | |
{ | |
int yes = 1; | |
E(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)); | |
} | |
#endif | |
memset(&storage, 0, sizeof storage); | |
if (family == AF_INET) { | |
struct sockaddr_in *addr = (struct sockaddr_in *) &storage; | |
addr->sin_family = family; | |
addr->sin_addr.s_addr = INADDR_ANY; | |
addr->sin_port = htons(12345); | |
socklen = sizeof *addr; | |
} | |
else if (family == AF_INET6) { | |
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &storage; | |
addr->sin6_family = family; | |
addr->sin6_addr = in6addr_any; | |
addr->sin6_port = htons(12345); | |
socklen = sizeof *addr; | |
} | |
else { | |
assert(0 && "bad address family"); | |
} | |
E(bind(sockfd, (struct sockaddr *) &storage, socklen)); | |
return sockfd; | |
} | |
int main(void) { | |
do_bind(AF_INET); | |
/*do_bind(AF_INET);*/ | |
sleep(1000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try running two separate instances of this compiled with -DREUSEADDR=1