// Headers (put this in stdafx.h if using precompiled headers)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <conio.h>
// Library needed
#pragma comment(lib, "Ws2_32.lib")
// Defines
#define LIGHTS L"192.168.1.230"
#define BUFLEN ( 450 )
#define PORT (40002)
void die(const char *str)
{
fprintf(stderr, "error: %s\n",str);
WSACleanup();
exit(1);
}
int main(void)
{
WSADATA w ;
struct sockaddr_in si_other;
int s, slen = sizeof(si_other);
unsigned char data[BUFLEN];
if (WSAStartup(MAKEWORD(2, 2), &w) != 0) {
fprintf(stderr, "could not initialise winsock\n");
exit(0);
}
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
die("socket create error");
}
memset((char *)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (1 != InetPton(AF_INET, LIGHTS, &si_other.sin_addr)) {
die("InetPton failed");
}
printf("press space to stop\n");
while (!_kbhit()) {
for (int i = 0; i < BUFLEN; i+=3) {
data[i ] = 0;
data[i+1] = 0;
data[i+2] = i>>1;
}
if (sendto(s, (const char*)data, BUFLEN, MSG_DONTROUTE, (struct sockaddr *) &si_other, slen) == -1) {
die("couldn't sendto");
}
}
_getch();
closesocket(s);
WSACleanup();
return 0;
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.