this initial multithreaded server doesn't serve anything.
authorerubow <erubow>
Thu, 1 Mar 2007 19:16:17 +0000 (19:16 +0000)
committererubow <erubow>
Thu, 1 Mar 2007 19:16:17 +0000 (19:16 +0000)
Robust/src/Runtime/DSTM/interface/dstmserver.c [new file with mode: 0644]
Robust/src/Runtime/DSTM/interface/servertest.c [new file with mode: 0644]

diff --git a/Robust/src/Runtime/DSTM/interface/dstmserver.c b/Robust/src/Runtime/DSTM/interface/dstmserver.c
new file mode 100644 (file)
index 0000000..998ff51
--- /dev/null
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include "dstmserver.h"
+
+#define LISTEN_PORT 2153
+#define BACKLOG 10 //max pending connections
+#define RECIEVE_BUFFER_SIZE 1500
+
+void *dstmListen()
+{
+       int listenfd, acceptfd;
+       struct sockaddr_in my_addr;
+       struct sockaddr_in client_addr;
+       socklen_t addrlength = sizeof(struct sockaddr);
+       pthread_t thread_dstm_accept;
+       int i;
+
+       listenfd = socket(PF_INET, SOCK_STREAM, 0);
+       if (listenfd == -1)
+       {
+               perror("socket");
+               exit(1);
+       }
+
+       my_addr.sin_family = AF_INET;
+       my_addr.sin_port = htons(LISTEN_PORT);
+       my_addr.sin_addr.s_addr = INADDR_ANY;
+       memset(&(my_addr.sin_zero), '\0', 8);
+
+       if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
+       {
+               perror("bind");
+               exit(1);
+       }
+       
+       if (listen(listenfd, BACKLOG) == -1)
+       {
+               perror("listen");
+               exit(1);
+       }
+
+       printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
+       while(1)
+       {
+               acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
+               pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
+       }
+       pthread_exit(NULL);
+}
+
+void *dstmAccept(void *acceptfd)
+{
+       int numbytes;
+       char buffer[RECIEVE_BUFFER_SIZE];
+       int fd_flags = fcntl((int)acceptfd, F_GETFD);
+       printf("Recieved connection: fd = %d\n", (int)acceptfd);
+       do
+       {
+               numbytes = recv((int)acceptfd, (void *)buffer, sizeof(buffer), 0);
+               buffer[numbytes] = '\0';
+               if (numbytes == -1)
+               {
+                       perror("recv");
+                       pthread_exit(NULL);
+               }
+               else
+               {
+                       printf("Read %d bytes from %d\n", numbytes, (int)acceptfd);
+                       printf("%s", buffer);
+               }
+               
+       } while (numbytes != 0);
+       if (close((int)acceptfd) == -1)
+       {
+               perror("close");
+               pthread_exit(NULL);
+       }
+       else
+               printf("Closed connection: fd = %d\n", (int)acceptfd);
+       pthread_exit(NULL);
+}
+
+
diff --git a/Robust/src/Runtime/DSTM/interface/servertest.c b/Robust/src/Runtime/DSTM/interface/servertest.c
new file mode 100644 (file)
index 0000000..98bd9e2
--- /dev/null
@@ -0,0 +1,11 @@
+#include <pthread.h>
+#include "dstmserver.h"
+
+int main()
+{
+       pthread_t thread_listen;
+       pthread_create(&thread_listen, NULL, dstmListen, NULL); 
+       pthread_join(thread_listen, NULL);
+       return 0;
+}
+