Created
October 9, 2012 05:57
-
-
Save scottjg/3855490 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <errno.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#define STATS_IN_PORT 8126 | |
#define STATS_OUT_IP "127.0.0.1" | |
#define STATS_OUT_PORT 18126 | |
int main(int argc, char **argv) | |
{ | |
int sock, out_sock, s; | |
struct sockaddr_in us = { 0 }, sender = { 0 }, them = { 0 }; | |
socklen_t socklen = sizeof(us); | |
static char buffer[65535]; | |
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
if (sock < 0) { | |
perror("socket error"); | |
return 1; | |
} | |
us.sin_family = AF_INET; | |
us.sin_port = htons(STATS_IN_PORT); | |
us.sin_addr.s_addr = htonl(INADDR_ANY); | |
out_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
if (out_sock < 0) { | |
perror("socket error"); | |
return 1; | |
} | |
them.sin_family = AF_INET; | |
them.sin_port = htons(STATS_OUT_PORT); | |
s = inet_aton(STATS_OUT_IP, &them.sin_addr); | |
if (s < 0) { | |
perror("inet_aton failed"); | |
return 1; | |
} | |
if (bind(sock, (struct sockaddr *)&us, sizeof(us)) < 0) { | |
perror("bind failed"); | |
return 1; | |
} | |
while (1) { | |
int i; | |
unsigned char shard = 0xff; | |
s = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&sender, &socklen); | |
if (s < 0) { | |
if (errno != EINTR) { | |
perror("recvfrom failed"); | |
return 1; | |
} | |
} | |
for (i = 0; i < s; i++) | |
shard += buffer[i]; | |
sendto(out_sock, buffer, s, 0, (struct sockaddr *)&them, sizeof(them)); | |
//printf("%d %s\n", shard, buffer); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment