int main(int argc, char **argv) { signal(SIGINT, interupt_handler); char uri[MAX_STR_LEN]; char hostname[MAX_STR_LEN]; char identifier[MAX_STR_LEN]; int port; if (argc != 1) { strcpy(uri, argv[1]); } else { puts("ERROR: SimpClient requires a uri as a arguement.\n"); exit(1); } printf("\nOpenning URI: %s\n", uri); parse_URI(uri, hostname, &port, identifier); printf("Hostname: %s port: %d identifier: %s\n", hostname, port, identifier); sockid = open_connection(hostname, port); printf("Hostname: %s\nport: %d\nidentifier: %s\n", hostname, port, identifier); perform_http(sockid, hostname, identifier); return 0; }
void main(int argc, char **argv) { char uri[MAX_STR_LEN]; char hostname[MAX_STR_LEN]; char identifier[MAX_STR_LEN]=""; char ip[100]; int sockid, port; //printf("Open URI: "); //scanf("%s", uri); //printf("Testing Begin...n"); //test for parse if(parse_URI(argv[1], hostname, &port, identifier) == 0) { printf("Errorr in Parasingn "); exit(1); } //get the ip address if(hostname_to_ip(hostname, ip)) { printf("Error resolving: %s\n", hostname); exit(1); } printf("port----->%d\n",port); // gernerate socket id sockid = open_connection(hostname, port, ip, identifier); // send and receive message perform_http(sockid, identifier, hostname); }
int main(int argc, char* argv[]) { atexit (cleanExit); int port; int option = 1; char ident[MAX_STR_LEN]; // Process CMD Line Args [BEGIN] //////////////////////////////////////////////////////////////////////////// if(argc==1) { port = SERVER_PORT_ID; } else if (argc==3) { port = atoi(argv[1]); strcpy(ident, argv[2]); } else if (argc==2) { port = atoi(argv[1]); } else { DEBUG_PRINT(("Wrong number of Arguments: exiting")); return 0; } //////////////////////////////////////////////////////////////////////////// // Process CMD Line Args [END] // Setup Socket [BEGIN] //////////////////////////////////////////////////////////////////////////// char str[MAX_STR_LEN]; int listen_fd, comm_fd; struct sockaddr_in servaddr; listen_fd = socket(AF_INET, SOCK_STREAM, 0); bzero( &servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htons(INADDR_ANY); servaddr.sin_port = htons(port); // tells kernal to force reuse of the address preventing binding problems setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(int)); bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr)); listen(listen_fd, 10); comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL); bzero( str, MAX_STR_LEN); read(comm_fd,str,MAX_STR_LEN); //////////////////////////////////////////////////////////////////////////// // Setup Socket [END] printf("Processing Request - %s",str); perform_http(comm_fd, str, ident); cleanExit(); }
/*---------------------main() routine--------------------------* * tasks for main * generate socket and get socket id, * Accept request from client and generate new socket * Communicate with client and close new socket after done *---------------------------------------------------------------------------*/ main(int argc, char *argv[]) { //Declare variables and clear memory where necessary. int newsockid; /* return value of the accept() call */ int my_port; struct sockaddr_in my_addr, client_addr; memset(&my_addr,'\0', sizeof(my_addr)); memset(&client_addr, '\0', sizeof(client_addr)); //Check that we have the proper arguments in the command line if (argc != 3) { printf("Error: Wrong number of arguments\nShould take the form 'SimpServer Port# Directory'\n"); } my_port = atoi(argv[1]); strcpy(directory,argv[2]); //Open socket and bind. if ((socketfd = socket(AF_INET, SOCK_STREAM, getprotobyname("tcp")->p_proto)) < 0) { printf("Error opening socket\n"); cleanExit(); } //Setup my_addr my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htons(INADDR_ANY); my_addr.sin_port = htons(my_port); //Bind the socket to our IP if ((bind(socketfd, (struct sockaddr*)&my_addr, sizeof(my_addr))) < 0) { printf("Error binding socket to port\n"); cleanExit(); } if ((listen(socketfd,MAX_BACKLOG)) < 0 ) { printf("Error listening on port %d",my_port); } //Wait until there is a connection request to perform HTTP while (1) { int comm_fd; if ((comm_fd = accept(socketfd, (struct sockaddr*) NULL, NULL)) < 0) { printf("Error accepting socket\n"); }else { perform_http(comm_fd); } close(comm_fd); } cleanExit(); }
main(int argc, char **argv) { char uri[MAX_STR_LEN]; char hostname[MAX_STR_LEN]; char identifier[MAX_STR_LEN]; int sockid, port; port = 80; printf("Open URI: "); scanf("%s", uri); parse_URI(uri,hostname,&port,identifier); sockid = open_connection(hostname,port); perform_http(sockid,identifier,uri,hostname); }
main(int argc, char *argv) { char uri[MAX_STR_LEN]; char hostname[MAX_STR_LEN]; char identifier[MAX_STR_LEN]; int sockid, port; // Read input from the terminal, and extract information from URI for an HTTP request printf("Open URI: "); scanf("%s", uri); parse_URI(uri, hostname, &port, identifier); printf("\n--- REQUEST ---\n%s\nHost: %s\nPort: %d\n---------------\n", identifier, hostname, port); strcat(identifier,"\r\n\r\n"); // Open socket connection, and connect to server sockid = open_connection(hostname, port); // Send an HTTP request to the server perform_http(sockid, identifier); }
/*-----------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); } }