Skip to content

Instantly share code, notes, and snippets.

@ssrlive
Last active March 13, 2018 05:03
Show Gist options
  • Save ssrlive/3585ea00d207afbb09654b8746159fc2 to your computer and use it in GitHub Desktop.
Save ssrlive/3585ea00d207afbb09654b8746159fc2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#endif
#define BUF_SIZE 1024
int main(int argc, char* argv[]) {
char buf[BUF_SIZE];
struct sockaddr_in self, other;
int len = sizeof(struct sockaddr_in);
int n, s, port;
if (argc < 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
return 1;
}
{
#if defined(_WIN32)
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
printf("Error at WSAStartup()\n");
return 1;
}
#endif
}
/* initialize socket */
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("socket");
return 1;
}
/* bind to server port */
port = atoi(argv[1]);
memset((char *)&self, 0, sizeof(struct sockaddr_in));
self.sin_family = AF_INET;
self.sin_port = htons(port);
self.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (struct sockaddr *) &self, sizeof(self)) == -1) {
perror("bind");
return 1;
}
while ((n = recvfrom(s, buf, BUF_SIZE, 0, (struct sockaddr *) &other, &len)) != -1) {
printf("Received from %s:%d: ",
inet_ntoa(other.sin_addr),
ntohs(other.sin_port));
fflush(stdout);
write(1, buf, n);
write(1, "\n", 1);
/* echo back to client */
sendto(s, buf, n, 0, (struct sockaddr *) &other, len);
}
#if defined(_WIN32)
closesocket(s);
#else
close(s);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment