Commit 61cd2235 authored by Gero Hülsbusch's avatar Gero Hülsbusch
Browse files

Update chargen_server_TCP.c

parent 9fbedf6a
......@@ -41,10 +41,12 @@ struct Args {
void *thread_handler(void *args) {
char *buf;
//allocate memmory init it with 0
buf = calloc(1, BUFFER_SIZE);
int client_fd = ((struct Args *) args)->client_fd;
//sets
fd_set read, write;
FD_ZERO(&read);
FD_ZERO(&write);
......@@ -52,13 +54,17 @@ void *thread_handler(void *args) {
int offset = 0;
while (1) {
//put client in read write
FD_SET(client_fd, &read);
FD_SET(client_fd, &write);
//TODO select_wrapper
//pick one read or wirte connection, double check if the connection is still ready
select(client_fd + 1, &read, &write, 0, 0);
//is client part of read set if so recive
if (FD_ISSET(client_fd, &read)) {
//when all is read
if (recv_wrapper(client_fd, buf, sizeof(buf), 0) == 0) {
printf("Client fd %i closed a connection in thread id: %lu\n", client_fd, (unsigned long int) pthread_self());
close_wrapper(client_fd);
......@@ -66,11 +72,17 @@ void *thread_handler(void *args) {
}
}
//is client_fd in write set ?
if (FD_ISSET(client_fd, &write)) {
//rfc specifies this kind of output
//rfc wants 72char in a line
for (int i = 0; i < 72; i++) {
//generate the chars
buf[i] = ((i + offset) % (126 - 32)) + 32;
}
//stop after 72 lines
buf[BUFFER_SIZE - 1] = '\n';
offset++;
......@@ -105,6 +117,7 @@ int main(int argc, char **argv) {
//constant LISTENQ for max number of connections
listen_wrapper(server_fd, LISTENQ);
struct sockaddr_in client;
int client_size = sizeof(client);
......@@ -112,10 +125,14 @@ int main(int argc, char **argv) {
thread_arguments = (struct Args *) malloc(sizeof(struct Args));
pthread_t thread_id;
while (1) {
//accept connection
client_fd = accept_wrapper(fd, (struct sockaddr *) &client, (socklen_t *) &client_size);
//struct for the thread_arguments
thread_arguments->client_fd = client_fd;
//cerate thread
pthread_create(&thread_id, NULL, thread_handler, (void *) thread_arguments);
printf("Client fd %i is connected to thread id: %lu\n", client_fd, (unsigned long int) thread_id);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment