7 #include "dstmserver.h"
9 #define LISTEN_PORT 2153
10 #define BACKLOG 10 //max pending connections
11 #define RECIEVE_BUFFER_SIZE 1500
15 int listenfd, acceptfd;
16 struct sockaddr_in my_addr;
17 struct sockaddr_in client_addr;
18 socklen_t addrlength = sizeof(struct sockaddr);
19 pthread_t thread_dstm_accept;
22 listenfd = socket(PF_INET, SOCK_STREAM, 0);
29 my_addr.sin_family = AF_INET;
30 my_addr.sin_port = htons(LISTEN_PORT);
31 my_addr.sin_addr.s_addr = INADDR_ANY;
32 memset(&(my_addr.sin_zero), '\0', 8);
34 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
40 if (listen(listenfd, BACKLOG) == -1)
46 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
49 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
50 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
55 void *dstmAccept(void *acceptfd)
58 char buffer[RECIEVE_BUFFER_SIZE];
59 int fd_flags = fcntl((int)acceptfd, F_GETFD);
60 printf("Recieved connection: fd = %d\n", (int)acceptfd);
63 numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
64 buffer[numbytes] = '\0';
72 printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
76 } while (numbytes != 0);
77 if (close((int)acceptfd) == -1)
83 printf("Closed connection: fd = %d\n", (int)acceptfd);