aboutsummaryrefslogtreecommitdiff
path: root/overflow_tcp.c
blob: cafeaec5d2d657bf369419bf9892e6578f686e2d (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

#define MAXLINE 1024
#define BUFLEN 256
#define SERV_PORT 3000
#define LISTENQ 8

int main (int argc, char **argv)
{
  int listenfd, connfd, n, line = 0, status;
  pid_t childpid, w;
  socklen_t clilen;
  char buf[BUFLEN];
  struct sockaddr_in cliaddr, servaddr;

  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) <0) {
    perror("socket");
    exit(1);
  }

  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(SERV_PORT);

  if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) != 0) {
    perror("bind");
    exit(2);
  }
  if (listen(listenfd, LISTENQ) != 0) {
    perror("listen");
    exit(3);
  }

  fprintf(stderr, "Server running on port %d ...\n", SERV_PORT);
  for (;;) {
    memset(buf, 0, BUFLEN);
    clilen = sizeof(cliaddr);
    connfd = accept (listenfd, (struct sockaddr *) &cliaddr, &clilen);
    if (connfd < 0) break;
    fprintf(stderr, "Client connected.\n");
    if ((childpid = fork ()) == 0 ) {
      while ((n = recv(connfd, buf, MAXLINE,0)) > 0)  {
        fprintf(stderr, "[%d] Received string(%d): %s", line, n, buf);
        memset(buf, 0, BUFLEN);
        line++;
      }
      exit(1);
    }
    if ((w = wait(&status))) {
      if (WIFEXITED(status)) {
        kill(w, SIGCHLD);
      }
    }
  }

  close(listenfd);
  return (0);
}