11 #define LISTEN_PORT 2153
12 #define BACKLOG 10 //max pending connections
13 #define RECIEVE_BUFFER_SIZE 1500
18 //todo:initialize main object store
19 //do we want this to be a global variable, or provide
20 //separate access funtions and hide the structure?
22 if (mhashCreate(HASH_SIZE, LOADFACTOR))
25 if (lhashCreate(HASH_SIZE, LOADFACTOR))
28 pthread_t threadListen;
29 pthread_create(&threadListen, NULL, dstmListen, NULL);
36 int listenfd, acceptfd;
37 struct sockaddr_in my_addr;
38 struct sockaddr_in client_addr;
39 socklen_t addrlength = sizeof(struct sockaddr);
40 pthread_t thread_dstm_accept;
43 listenfd = socket(PF_INET, SOCK_STREAM, 0);
50 my_addr.sin_family = AF_INET;
51 my_addr.sin_port = htons(LISTEN_PORT);
52 my_addr.sin_addr.s_addr = INADDR_ANY;
53 memset(&(my_addr.sin_zero), '\0', 8);
55 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
61 if (listen(listenfd, BACKLOG) == -1)
67 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
70 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
71 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
76 void *dstmAccept(void *acceptfd)
79 char buffer[RECIEVE_BUFFER_SIZE];
80 int fd_flags = fcntl((int)acceptfd, F_GETFD);
81 printf("Recieved connection: fd = %d\n", (int)acceptfd);
84 numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
85 buffer[numbytes] = '\0';
93 printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
97 } while (numbytes != 0);
98 if (close((int)acceptfd) == -1)
104 printf("Closed connection: fd = %d\n", (int)acceptfd);