int main(int argc, char *argv[]) { int listener, conn; pid_t pid; struct sockaddr_in servaddr; /* Create socket */ if ( (listener = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) Error_Quit("Couldn't create listening socket."); /* Populate socket address structure */ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERVER_PORT); /* Assign socket address to socket */ if ( bind(listener, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) Error_Quit("Couldn't bind listening socket."); /* Make socket a listening socket */ if ( listen(listener, LISTENQ) < 0 ) Error_Quit("Call to listen failed."); /* Loop infinitely to accept and service connections */ while ( 1 ) { /* Wait for connection */ if ( (conn = accept(listener, NULL, NULL)) < 0 ) Error_Quit("Error calling accept()"); /* Fork child process to service connection */ if ( (pid = fork()) == 0 ) { /* This is now the forked child process, so close listening socket and service request */ if ( close(listener) < 0 ) Error_Quit("Error closing listening socket in child."); Service_Request(conn); /* Close connected socket and exit */ if ( close(conn) < 0 ) Error_Quit("Error closing connection socket."); exit(EXIT_SUCCESS); } /* If we get here, we are still in the parent process, so close the connected socket, clean up child processes, and go back to accept a new connection. */ if ( close(conn) < 0 ) Error_Quit("Error closing connection socket in parent."); waitpid(-1, NULL, WNOHANG); } return EXIT_FAILURE; /* We shouldn't get here */ }
void start_server() { printf("STARTING SERVER!\n localhost:%d\n", SERVER_PORT); if(SERVER_PORT<1024)p("sudo netbase if port < 1024 !!!"); flush(); /* Create socket */ if ((listener = socket(AF_INET, SOCK_STREAM, 0)) < 0) Error_Quit("Couldn't create listening socket."); int flag = 1;// allow you to bind a local port that is in TIME_WAIT. // This is very useful to ensure you don't have to wait 4 minutes after killing a server before restarting it. setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); /* Populate socket address structure */ memset(&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERVER_PORT); /* Assign socket address to socket */ // __bind<int&,sockaddr *,unsigned long> x= bind(listener, (struct sockaddr *) &servaddr, sizeof (servaddr)); if(listener<0) Error_Quit("Couldn't bind listening socket."); /* Make socket a listening socket */ // if (listen(listener, BACKLOG) < 0) if (listen(listener, LISTENQ) < 0) Error_Quit("Call to listen failed."); printf("listening on %d port %d\n", INADDR_ANY, SERVER_PORT); p(" [debugging server doesn't work with xcode, use ./compile.sh ]"); /* Loop infinitely to accept and service connections */ while (1) { /* Wait for connection */ // NOT with XCODE -> WEBSERV conn = accept(listener, NULL, NULL); if (conn < 0) Error_Quit("Error calling accept()! debugging not supported, are you debugging?"); else p("conn = accept OK"); // WORKS FINE, but not when debugging /* Fork child process to service connection */ pid = fork(); if (pid == 0) { /* This is now the forked child process, so close listening socket and service request */ if (close(listener) < 0) Error_Quit("Error closing listening socket in child."); Service_Request(conn); /* Close connected socket and exit forked process */ if (close(conn) < 0) Error_Quit("Error closing connection socket."); exit(EXIT_SUCCESS); }else{ // p("not forked yet"); // !!?! // Service_Request(conn);// whatever } /* If we get here, we are still in the parent process, so close the connected socket, clean up child processes, and go back to accept a new connection. */ waitpid(-1, NULL, WNOHANG); if (close(conn) < 0) Error_Quit("Error closing connection socket in parent."); } Error_Quit("FORK web server failed"); return; // EXIT_FAILURE; /* We shouldn't get here */ }