Skip to content

Instantly share code, notes, and snippets.

@vvavrychuk
Created April 19, 2017 05:46
Show Gist options
  • Save vvavrychuk/f5851fe1d24b07605289a4e28a0a1e34 to your computer and use it in GitHub Desktop.
Save vvavrychuk/f5851fe1d24b07605289a4e28a0a1e34 to your computer and use it in GitHub Desktop.
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
WSADATA wsaData = { 0 };
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == INVALID_SOCKET) {
return EXIT_FAILURE;
}
if (argc != 4) {
printf("Usage: echo_dgram_client ip port message\n");
return EXIT_FAILURE;
}
unsigned ip = inet_addr(argv[1]);
unsigned port = atoi(argv[2]);
const char *message = argv[3];
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = ip;
int bytes_sent = sendto(sock, message, strlen(message), 0, (const struct sockaddr*)&sa, sizeof(sa));
if (bytes_sent == -1) {
fprintf(stderr, "sendto failed!\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment