c - unable to get my proxy server to send http requested information to client- Socket programming -
my objective of programs implement proxy server can connected single client , allow http request.
the requirements
- to create c based client-server architecture using sockets
- the proxy server should able accept , service single client's http requests
code
client.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]){ int sockfd, portnum, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if(argc < 3){ fprintf(stderr, "usage %s hostname port\n", argv[0]); exit(1); } portnum = atoi(argv[2]); sockfd= socket(af_inet, sock_stream, 0); if(sockfd <0){ perror("error opening socket"); exit(1); } server= gethostbyname(argv[1]); if(sockfd == null){ fprintf(stderr,"error, no such host\n"); exit(1); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = af_inet; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portnum); if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0){ fprintf(stderr,"error, on connecting"); exit(1); } printf("please enter host name: "); bzero(buffer,256); fgets(buffer,255,stdin); n=write(sockfd,buffer,strlen(buffer)); if(n<0){ printf("error writing socket"); exit(1); } bzero(buffer,256); n=read(sockfd,buffer,255); if(n<0){ printf("error reading socket"); exit(1); } printf("%s\n", buffer); return 0; }
proxy server.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <netdb.h> #include <string.h> int main(int argc, char *argv[]){ int sockfd, newsockfd, portnum, clilen; char buffer[256], hostname[256]; pid_t p_id; struct sockaddr_in serv_addr, cli_addr; int n, pid; if(argc < 2){ fprintf(stderr, "error, no port provided!\n"); exit(1); } sockfd = socket(af_inet, sock_stream, 0);//socket made if(sockfd < 0){ fprintf(stderr,"error opening socket!!"); exit(1); } bzero((char *) &serv_addr, sizeof(serv_addr)); portnum = atoi(argv[1]);//port num serv_addr.sin_family = af_inet; serv_addr.sin_addr.s_addr = htonl(inaddr_any); serv_addr.sin_port = htons(portnum); if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){ fprintf(stderr,"error on binding"); exit(1); } if( listen(sockfd, 5) < 0){ printf("error on listen"); exit(1); } // accept clilen = sizeof(cli_addr); do{ newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if(newsockfd<0){ fprintf(stderr,"error on accept\n"); exit(1); } pid=fork(); if(pid==0){ bzero(buffer, 256); n= read(newsockfd, buffer, 255); if(n<0){//message client fprintf(stderr,"error reading socket\n"); exit(1); } strcpy(hostname, buffer); printf("here hostname : %s\n", hostname); //variables used acsessing webserver? int sockwb, wbport, x; struct sockaddr_in webser_addr; struct hostent *wbhost; char webbuf[510];//sending webserver wbport =80;//port used access web server sockwb = socket(af_inet, sock_stream, 0); if(sockwb <0){ printf("error opeing websocket\n"); exit(1); } wbhost= gethostbyname(hostname); printf("%s", wbhost->h_name); if(sockwb == -1){ printf("no such web host\n"); exit(1); } bzero((char*) &webser_addr, sizeof(webser_addr)); webser_addr.sin_family = af_inet; bcopy((char *)wbhost->h_addr, (char *)&webser_addr.sin_addr.s_addr, wbhost->h_length); webser_addr.sin_port = htons(wbport); if(connect(sockwb,(struct sockaddr *)&webser_addr,sizeof(webser_addr)) < 0){ printf("error on web connecting\n"); exit(1); } bzero(webbuf,510); strcpy(webbuf, "get http://"); strcat(webbuf, hostname); strcat(webbuf, " http/1.1"); printf("%s\n", webbuf); x=write(sockwb,webbuf,strlen(webbuf)); if(x<0){ printf("error writing web sock"); exit(1); } bzero(webbuf,510); x=read(sockwb,webbuf,510); if(n<0){ printf("error reading web socket"); exit(1); } n = write(newsockfd, webbuf,255 );//write client if (n<0){ fprintf(stderr,"error writing socket"); exit(1); } printf("%s\n", webbuf); }//end of if pid==0 printf("closing client"); close(newsockfd);//closing client socket }while(1); return 0; }
i have been able implement simple client-server exchange sockets, issue http request. have access web server through port 80 using method.
the way have input client sending proxy server should : www.name.com
the server doesn't seem doing after initial connection client.
as usual, null-terminated strings not managed here. when following construct used
send(sock, buff, strlen(buf));
the trailing null-terminated character not sent. on receiving side, there 2 problems. first, recv(sock, buff, sizeof(buff));
can receive unpredictable amount of bytes - 1 size of string provided. need read until read message. here comes dilemma - how know how long actual message? answer either agree on size of strings in advance, , send many bytes (regardless of actual string length) or prefix the string it's size in message. second problem if luck whole string read, not null-terminated, , such, unusable in program. has null-terminated manually.
Comments
Post a Comment