udp-ping/server.c
josh d4349c9538 added udp-ping program
git-svn-id: svn://anubis/misc/udp-ping@234 bd8a9e45-a331-0410-811e-c64571078777
2010-02-18 03:59:41 +00:00

50 lines
1.1 KiB
C

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN sizeof(struct timeval)
#define PORT 9876
void diep(char * s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, slen = sizeof(si_other);
struct timeval buf;
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (struct sockaddr *) &si_me, sizeof(si_me)) == -1)
diep("bind");
for (;;)
{
if (recvfrom(s, &buf, BUFLEN, 0,
(struct sockaddr *) &si_other, &slen) == -1)
diep("recvfrom()");
if (sendto(s, &buf, BUFLEN, 0,
(struct sockaddr *) &si_other, slen) == -1)
diep("sendto()");
}
close(s);
return 0;
}