summaryrefslogtreecommitdiff
path: root/server/main.c
blob: 789d3925ea97b68a845ff0444349cf1a97893062 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <time.h>

int main() {
  int fd = socket(AF_INET, SOCK_DGRAM, 0);

  if (fd == -1)
    err(EXIT_FAILURE, "socket failed");

  struct sockaddr_in address;
  address.sin_family = AF_INET;
  address.sin_port = htons(8080);
  address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

  if (bind(fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
    close(fd);
    err(EXIT_FAILURE, "bind failed");
  }

  time_t startTime = time(NULL);

  while (time(NULL) < startTime + 60) {
    struct sockaddr connection;
    socklen_t connectionLen = sizeof(connection);
    char buff[64];

    int size = recvfrom(fd, buff, sizeof(buff), MSG_DONTWAIT,
                        (struct sockaddr *)&connection, &connectionLen);

    if (size != -1)
      printf("got a message\n");
  }

  close(fd);
  return 0;
}