/** A basic client, note that he will create the sockets, not the server * @param path Path to the server socket */ void client_socket(char *path){ lsocket*main_socket=make_socket(path); /* Connect to the given socket address */ open_socket(main_socket,S_IWUSR); /* Creation of the two sockets */ sockets=open_communication(); /* Send the socket to listen to */ if (handshake(main_socket,sockets)<0) OUT("The server didn't accepted connection"); /* Acknoledge user requests */ bor_signal(SIGINT,gotcha,0); while (user_request(sockets)); /* Properly close connection */ self_terminate(); }
/*-----------main--------------- * This function opens a socket, and listens for any * client trying to connect, and then forms the connection * then it reads what the client is requesting * and replies with the result of that request -------------------------------*/ int main(int argc, char **argv){ /* variable declaration:*/ int comm_fd, listen_fd; //the file descriptor for the socket used for communication int portno; //the port number to connect to char directory[MAX_STR_LEN]; //the directory to look in for the requested file char request[MAX_STR_LEN]; // the http request received from the client char response[MAX_RES_LEN]; // the response to send back to the client // check to see if there are enough arguments provided by the user if(argc < 3) { fprintf(stderr,"ERROR: please provide the port number and the location of the file in the program arguments"); exit(1); } /* grab the port number and the directory from * the program arguments*/ portno = atoi(argv[1]); strncpy(directory, argv[2], MAX_STR_LEN); // attempt to open a connection listen_fd = open_communication(portno); // run until alt+C is pressed by the user while(1){ // listen for an attempted connection from the server listen(listen_fd, 10); // attempt to accept the connection comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL); // check to see if the connection is successfully made if(comm_fd < 0) { fprintf(stderr,"ERROR: unable to accept communications"); exit(0); } //write zeros to the memory pointed to by "request" and "response" character strings bzero(request, MAX_STR_LEN); bzero(response, MAX_RES_LEN); // attempt to read the request from the client if(read(comm_fd,request,MAX_STR_LEN) < 0) { //the read failed, so report it and exit fprintf(stderr,"ERROR: failed to read from the client"); close(comm_fd); exit(1); } // parse the request and form a response perform_http(request, response, directory); //send the response back to the client if(write(comm_fd,response,MAX_RES_LEN) < 0) { // the write failed, so report it and exit fprintf(stderr,"ERROR: failed to write to the client"); close(comm_fd); exit(1); } // close the socket used for the communication close(comm_fd); } }