Adding KeepAliveMaxRequest in request header in C socket programming -
i have written c client code need add connection:keepalivemaxrequest in request header. argv[1] takes hostname argument , argv[2] takes port number argument.the code follows:
#include<stdio.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<netdb.h> void error(char *msg){ perror(msg); exit(0); } int main(int argc , char *argv[]) { int sockfd,portno,noofrequests; struct sockaddr_in serv_addr; struct hostent *server; char *message , server_reply[2000]; int m=0; sockfd = socket(af_inet , sock_stream , 0); portno=atoi(argv[2]); noofrequests=10; server = gethostbyname(argv[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(portno); if (connect(sockfd ,(struct sockaddr *)&serv_addr , sizeof(serv_addr)) < 0) exit(0); puts("connected\n"); while(m<noofrequests){ message = "get /index.sample.html http/1.1\r\n\r\n"; if( send(sockfd , message , strlen(message) , 0) < 0) { exit(0); } puts("data sent\n"); if( recv(sockfd, server_reply , 2000 , 0) < 0) puts("recv failed"); puts(server_reply); m++; } return 0; }
Comments
Post a Comment