only a few small changes from windows version. instructions same for both linux and OSX, assuming gcc is installed already etc.
save as test.c, compile with
gcc -std=c99 test.c
run as
./a.out
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define LIGHTS "192.168.1.230"
#define BUFLEN ( 450 )
#define PORT (40002)
void changemode(int dir);
void die(const char *str)
{
fprintf(stderr, "error: %s\n",str);
changemode(0);
exit(1);
}
void changemode(int dir)
{
static struct termios oldt, newt;
if ( dir == 1 )
{
tcgetattr( STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
}
else
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
int _kbhit (void)
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET (STDIN_FILENO, &rdfs);
select(STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &rdfs);
}
int main(void)
{
struct sockaddr_in si_other;
int s, slen = sizeof(si_other);
unsigned char data[BUFLEN];
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);
si_other.sin_addr.s_addr=inet_addr( LIGHTS );
printf("press space to stop\n");
changemode(1);
while (!_kbhit()) {
for (int i = 0; i < BUFLEN; i+=3) {
data[i ] = 0;
data[i+1] = i>>1;
data[i+2] = 0;
}
if (sendto(s, (const char*)data, BUFLEN, 0, (struct sockaddr *) &si_other, slen) == -1) {
die("couldn't sendto");
}
}
getchar();
changemode(0);
close(s);
return 0;
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.