Commit e243597b authored by Maia Jejelava's avatar Maia Jejelava
Browse files

Chargen server added. Little updates in other things.

parent 0478911f
//
// Created by user on 27.11.21.
//
#include "Socket.h"
struct Args {
int client_fd;
};
void *thread_handler(void *args) {
char *buf;
buf = calloc(1, BUFFER_SIZE);
int client_fd = ((struct Args *) args)->client_fd;
fd_set read, write;
FD_ZERO(&read);
FD_ZERO(&write);
int offset = 0;
while (1) {
FD_SET(client_fd, &read);
FD_SET(client_fd, &write);
//TODO select_wrapper
select(client_fd + 1, &read, &write, 0, 0);
if (FD_ISSET(client_fd, &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);
break;
}
}
if (FD_ISSET(client_fd, &write)) {
for (int i = 0; i < 72; i++) {
buf[i] = ((i + offset) % (126 - 32)) + 32;
}
buf[BUFFER_SIZE - 1] = '\n';
offset++;
offset = offset % 95;
send_wrapper(client_fd, buf, strlen(buf), 0);
sleep(1);
}
}
return NULL;
}
int main(int argc, char **argv) {
int server_fd, client_fd;
struct sockaddr_in server_addr;
char buf[BUFFER_SIZE];
//(1)The creation of the TCP socket is identical to the client code
server_fd = socket_wrapper(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, 0, sizeof(server_addr)); //socket address structure
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(
INADDR_ANY); //specify the IP address allows the server to accept a client //connection on any interface,
server_addr.sin_port = htons(strtol(argv[1], NULL, 10));
bind_wrapper(server_fd, (const struct sockaddr *) &server_addr, (socklen_t) sizeof(server_addr));
//(2)The bind() function shall assign a local socket address address to a socket identified by descriptor socket
//(3)Socket converted in listening socket, incomming connections from clients will be accepted by kernel
//server_fd = listening descriptor
//constant LISTENQ for max number of connections
listen_wrapper(server_fd, LISTENQ);
struct sockaddr_in client;
int client_size = sizeof(client);
struct Args *thread_arguments;
thread_arguments = (struct Args *) malloc(sizeof(struct Args));
pthread_t thread_id;
while (1) {
client_fd = accept_wrapper(fd, (struct sockaddr *) &client, (socklen_t *) &client_size);
thread_arguments->client_fd = client_fd;
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);
}
close_wrapper(fd);
exit(0);
}
......@@ -28,14 +28,14 @@ int main(int argc, char **argv) {
while(!done) {
//Wo für intressiere ich mich beim select? das wird in readset aufgenommen
//kann ich was von stin oder TCP Verbindung lesen?
FD_SET(0, &rset); //stdin
FD_SET(fd, &rset); //TCP Verbindung
if (stdin_open == 0)
FD_SET(0, &rset); //stdin
//Größter fd +1 , Readset intessiert uns
select(fd + 1, &rset, NULL, NULL, NULL);
//0 ist lesbar, also wenn von stdin was lesbar ist
if (FD_ISSET(0, &rset)) {
if (FD_ISSET(0, &rset) && stdin_open == 0) {
//lese von stdin
len = read(0, (void *) buf, sizeof(buf));
//solange lesen bis stin zuende ist (Strg+D )
......@@ -58,7 +58,6 @@ int main(int argc, char **argv) {
done = 1;
else
printf("%.*s", (int)len, buf);
// write soll genommen werden, buggt aber
}
}
......@@ -66,13 +65,3 @@ int main(int argc, char **argv) {
return (0);
}
/*TODO Wie geht man aus dieser Schleife raus
wenn Strg +D also stdin geschlossen, dann ein FIN senden Shoutdown "shout br"
aber man muss weiter bereit sein zu lesen
erst wenn read /recive 0 liefern rausgehen
wenn von stin eine 0 kommt als return Wert, nicht weiter auf stin betrachen, sonst Endlosschleife(select sagt dann immer stin ist immer noch lesbar)
wichtig wenn man char.gen.server hat der so lange redet bis fin kommt
*/
......@@ -19,7 +19,7 @@ main(int argc, char **argv)
memset(&server_addr, 0, sizeof(server_addr)); //socket address structure
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //specify the IP address allows the server to accept a client //connection on any interface,
server_addr.sin_port = htons(strtol(argv[1], NULL, 10));
server_addr.sin_port = htons(strtol(argv[1], NULL, 10));
bind_wrapper(server_fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr)); //(2)The bind() function shall assign a local socket address address to a socket identified by descriptor socket
......
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