From 4b2603707f23150edd056e3ec636816c3939f100 Mon Sep 17 00:00:00 2001 From: erubow Date: Thu, 1 Mar 2007 19:16:17 +0000 Subject: [PATCH] this initial multithreaded server doesn't serve anything. --- .../src/Runtime/DSTM/interface/dstmserver.c | 87 +++++++++++++++++++ .../src/Runtime/DSTM/interface/servertest.c | 11 +++ 2 files changed, 98 insertions(+) create mode 100644 Robust/src/Runtime/DSTM/interface/dstmserver.c create mode 100644 Robust/src/Runtime/DSTM/interface/servertest.c diff --git a/Robust/src/Runtime/DSTM/interface/dstmserver.c b/Robust/src/Runtime/DSTM/interface/dstmserver.c new file mode 100644 index 00000000..998ff514 --- /dev/null +++ b/Robust/src/Runtime/DSTM/interface/dstmserver.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#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 index 00000000..98bd9e2a --- /dev/null +++ b/Robust/src/Runtime/DSTM/interface/servertest.c @@ -0,0 +1,11 @@ +#include +#include "dstmserver.h" + +int main() +{ + pthread_t thread_listen; + pthread_create(&thread_listen, NULL, dstmListen, NULL); + pthread_join(thread_listen, NULL); + return 0; +} + -- 2.34.1