11 #define LISTEN_PORT 2153
12 #define BACKLOG 10 //max pending connections
13 #define RECIEVE_BUFFER_SIZE 1500
16 objstr_t *mainobjstore;
17 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
21 //todo:initialize main object store
22 //do we want this to be a global variable, or provide
23 //separate access funtions and hide the structure?
25 if (mhashCreate(HASH_SIZE, LOADFACTOR))
28 if (lhashCreate(HASH_SIZE, LOADFACTOR))
31 pthread_t threadListen;
32 pthread_create(&threadListen, NULL, dstmListen, NULL);
39 int listenfd, acceptfd;
40 struct sockaddr_in my_addr;
41 struct sockaddr_in client_addr;
42 socklen_t addrlength = sizeof(struct sockaddr);
43 pthread_t thread_dstm_accept;
46 listenfd = socket(PF_INET, SOCK_STREAM, 0);
53 my_addr.sin_family = AF_INET;
54 my_addr.sin_port = htons(LISTEN_PORT);
55 my_addr.sin_addr.s_addr = INADDR_ANY;
56 memset(&(my_addr.sin_zero), '\0', 8);
58 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
64 if (listen(listenfd, BACKLOG) == -1)
70 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
73 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
74 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
79 void *dstmAccept(void *acceptfd)
82 char buffer[RECIEVE_BUFFER_SIZE];
83 int fd_flags = fcntl((int)acceptfd, F_GETFD);
84 printf("Recieved connection: fd = %d\n", (int)acceptfd);
87 numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
88 buffer[numbytes] = '\0';
96 printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
100 } while (numbytes != 0);
101 if (close((int)acceptfd) == -1)
107 printf("Closed connection: fd = %d\n", (int)acceptfd);