Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "name_server.h"
- #include "csapp.h"
- char listen_port[PORT_LEN];
- int listen_socket = -1; // listen socket. initialized to -1.
- int num_active_clients = 0;
- client_t *clients[MAX_USERS]; // malloc(MAX_USERS * sizeof(client_t)); // array of pointers to structs representing active clients.
- void client_session(int connfd){
- rio_t rio;
- char listen_buf[MAXLINE];
- command_t command;
- args_t args;
- char *username, *password; // these pointers will serve different
- char *ip, *port; // purposes based on the current command.
- char *message;
- int user = 0;
- Rio_readinitb(&rio, connfd);
- while (Rio_readlineb(&rio, listen_buf, MAXLINE) != 0){
- if(num_active_clients == 2){
- printf("clients1 %s \n", clients[0]->username);
- printf("clients2 %s \n", clients[1]->username);
- }
- command = parse_command(listen_buf, args);
- username = args[0];
- ip = args[2];
- port = args[3];
- if(num_active_clients == 2){
- printf("clients3 %s \n", clients[1]->username);
- }
- client_t* client = malloc(sizeof(client_t));
- client->username = malloc(sizeof(char)*MAXLINE);
- client->ip = malloc(sizeof(char)*MAXLINE);
- client->port = malloc(sizeof(char)*MAXLINE);
- clients[num_active_clients] = client;
- switch (command)
- {
- case LOGIN:
- if(((strcmp("thomas", args[0]) == 0) && (strcmp("hansen", args[1]) == 0))
- || ((strcmp("william", args[0]) == 0) && (strcmp("pedersen", args[1]) == 0)) ){
- sprintf(clients[num_active_clients]->username, "%s", username);
- sprintf(clients[num_active_clients]->ip, "%s", ip);
- sprintf(clients[num_active_clients]->port, "%s", port);
- clients[num_active_clients]->logged_in = 1;
- num_active_clients ++;
- sprintf(listen_buf, "You are now logged in as %s \n", clients[num_active_clients -1]->username);
- Rio_writen(connfd, listen_buf, strlen(listen_buf));
- break;
- }
- else
- {
- sprintf(listen_buf, "Wrong Login \n");
- Rio_writen(connfd, listen_buf, strlen(listen_buf));;
- break;
- }
- case LOOKUP:
- user = 0;
- if (username != NULL){
- for (int i = 0; i < num_active_clients; i++)
- {
- if (strcmp(clients[i]->username, username) == 0){
- sprintf(listen_buf, "%s " , clients[i]->username);
- strcat(listen_buf, clients[i]->ip);
- strcat(listen_buf, " ");
- strcat(listen_buf, clients[i]->port);
- user = 1;
- }
- }
- }
- else
- {
- sprintf(listen_buf, "");
- for (int i = 0; i < num_active_clients; i++)
- {
- strcat(listen_buf, clients[i]->username);
- strcat(listen_buf, " ");
- strcat(listen_buf, clients[i]->ip);
- strcat(listen_buf, " ");
- strcat(listen_buf, clients[i]->port);
- if((i + 1) < num_active_clients){
- strcat(listen_buf, " ");
- }
- }
- user = 1;
- }
- if (user == 0){
- sprintf(listen_buf, "%s", "User does not exist, or not logged in");
- }
- strcat(listen_buf, "\n");
- user = 0;
- Rio_writen(connfd, listen_buf, strlen(listen_buf));
- break;
- case LOGOUT:
- sprintf(listen_buf, " >> Goodbye ... \n");
- clients[num_active_clients -1]->logged_in = 0;
- free(clients[num_active_clients -1]);
- num_active_clients --;
- Rio_writen(connfd, listen_buf, strlen(listen_buf));
- break;
- }
- }
- }
- void *thread(void *vargp)
- {
- int connfd = *((int *)vargp);
- Pthread_detach(pthread_self()); //line:conc:echoservert:detach
- Free(vargp); //line:conc:echoservert:free
- client_session(connfd);
- Close(connfd);
- return NULL;
- }
- int main(int argc, char **argv) {
- if (argc != MAIN_ARGNUM + 1) {
- fprintf(stderr, "Usage: %s <listening port>\n", argv[0]);
- exit(EXIT_FAILURE);
- } else if (!is_valid_port(argv[1])) {
- fprintf(stderr, ">> Invalid port number: %s\n", argv[1]);
- exit(EXIT_FAILURE);
- }
- snprintf(listen_port, PORT_LEN, "%s", argv[1]);
- for (int i = 0; i < MAX_USERS; i++)
- clients[i] = NULL;
- printf(">> Accepting peers on port %s ...\n", listen_port);
- /*
- * TODO: SETUP LISTENING SOCKET HERE
- * HINT: remember that you are free to use everything from csapp.c ;) ;)
- */
- listen_socket = Open_listenfd(argv[1]);
- socklen_t clientlen;
- int *connfdp;
- struct sockaddr_storage clientaddr;
- char client_hostname[MAXLINE], client_port[MAXLINE];
- pthread_t tid;
- int running = 1;
- while (running) {
- clientlen = sizeof(struct sockaddr_storage);
- connfdp = Malloc(sizeof(int));
- *connfdp = Accept(listen_socket, (SA *)&clientaddr, &clientlen);
- Pthread_create(&tid, NULL, thread, connfdp);
- Getnameinfo((SA *) &clientaddr, clientlen, client_hostname, MAXLINE,
- client_port, MAXLINE, 0);
- printf("Connected to (%s, %s) via thread %d\n",
- client_hostname, client_port, (int) tid);
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement