81static int output_socket(
unsigned short port,
const char *host,
int bufsize)
84 struct addrinfo hints;
86 char saddr[INET_ADDRSTRLEN];
89 memset(&hints,
'\0',
sizeof(hints));
90 hints.ai_flags = AI_CANONNAME;
91 hints.ai_socktype = SOCK_DGRAM;
92 hints.ai_protocol = IPPROTO_UDP;
93 hints.ai_family = PF_INET;
95 snprintf(p, 6,
"%d", port);
98 if (getaddrinfo(host, p, &hints, &res)) {
101 fprintf(stderr,
"Address not found: %s\n", host);
103 fprintf(stderr,
"%s: %s\n", gai_strerror(err), host);
107 inet_ntop(AF_INET, &((
struct sockaddr_in *) res->ai_addr)->sin_addr,
108 saddr,
sizeof(saddr));
109 printf(
"connecting to %s (%s) port %d\n",
110 res->ai_canonname, saddr, port);
112 if ((s = socket(res->ai_family, res->ai_socktype, 0)) == -1) {
120 if (connect(s, res->ai_addr, res->ai_addrlen)) {
162 struct sockaddr_in addr;
169 fprintf(stderr,
"%s buffer_size port host...\n", argv[0]);
173 if ((bufsize = atoi(argv[1])) <= 0) {
174 fprintf(stderr,
"invalid buffer size: %d\n", bufsize);
177 switch (argv[1][strlen(argv[1]) - 1]) {
185 bufsize *= 1024*1024;
189 qsize = bufsize / 8000;
191 printf(
"using UDP send and receive buffer size of %d bytes\n", bufsize);
193 if ((port = atoi(argv[2])) <= 0) {
194 fprintf(stderr,
"invalid port: %d\n", port);
201 if ((sock_in = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
202 perror(
"input socket");
209 addr.sin_family = AF_INET;
210 addr.sin_addr.s_addr = INADDR_ANY;
211 addr.sin_port = htons(port);
212 if (bind(sock_in, (
struct sockaddr *) &addr,
213 sizeof(
struct sockaddr_in))) {
218 printf(
"listening on *:%d\n", port);
224 fprintf(stderr,
"not enough memory for replica array");
228 for (i = 0; i <
count; i++) {
240 if (pthread_create(&thread, NULL,
writer, NULL)) {
241 fprintf(stderr,
"cannot create writer thread\n");
247 fcntl(sock_in, F_SETFL, O_NONBLOCK);
251 || (err = errno) == EAGAIN)) {
277 printf(
"read: %s\n", strerror(err));