File: udp-test.c

package info (click to toggle)
retroarch 1.20.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,736 kB
  • sloc: ansic: 1,207,646; cpp: 104,166; objc: 8,567; asm: 6,624; python: 3,776; makefile: 2,838; sh: 2,786; xml: 1,408; perl: 393; javascript: 10
file content (55 lines) | stat: -rw-r--r-- 1,078 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* public domain */
/* gcc -o udptest udp-test.c */

/*
   will send "RETROPAD RIGHT" indefinely to player 1
   to send to player 2 change port to 55401 and so on
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define SERVER "127.0.0.1"
#define PORT 55400

void die(char *s)
{
    perror(s);
    exit(1);
}

int main(void)
{
   struct sockaddr_in si_other;
   int s, i, slen=sizeof(si_other);

   if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
      die("socket");

   memset((char *) &si_other, 0, sizeof(si_other));
   si_other.sin_family = AF_INET;
   si_other.sin_port = htons(PORT);

   if (inet_aton(SERVER , &si_other.sin_addr) == 0)
   {
      fprintf(stderr, "inet_aton() failed\n");
      exit(1);
   }

   for (;;)
   {
      char message[10]="128";
      /* send the message */
      if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
         die("sendto()");

      /* sleep for 1 frame (60hz) */
      usleep(16*1000);
   }

   close(s);
   return 0;
}