Re: HTTP client program
From: Jan Geertsma (jan_at_islief.com)
Date: 05/06/04
- Next message: Naim R. El-Far: "Simulating Network Latency"
- Previous message: P Gentry: "Re: Firewall box configuration"
- In reply to: Nachiketh: "HTTP client program"
- Next in thread: DoesntMatter: "Re: HTTP client program"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 06 May 2004 19:32:05 +0300
Change the localhost to an external host and do a tcpdump/snoop/ethereal
trace to see what the packages look like
Jan
Nachiketh wrote:
> hi,
> i have written a HTTP client to connect to my local host and retrive a
> static webpage on my machine. i have got rid of the syntax errors, but
> am not able to debug the program. if neone can plz help me out wid
> this,
> -Nachiketh
> Code :
> #include<stdio.h>
> #include<sys/socket.h>
> #include<sys/types.h>
> #include<arpa/inet.h>
> #include<string.h>
> #define SERV_PORT 80
> #define SERV_ADDR "127.0.0.1"
> int readn(int fd,char* msg,int maxlen)
> {
> int n=1,rc;
> char c;
> printf("reading");
> for(n=1;n<maxlen;n++)
> {
> if((rc=read(fd,&c,1))==1)
> {
> printf("%c",&c);
> *msg++=c;
> if(c=='\n')
> {
> printf("%s",msg);
> break;
> }
> }
> else if(rc==0)
> {
> if(n==1)
> return 0; /* EOF, no data read */
> else
> break; /* EOF, some data was read */
> }
> else
> return -1; /*error */
>
> }
>
> *msg=0;
> return n;
> }
>
> int writen(int sockfd,char*msg,int nbytes)
> {
> int nleft,nwritten;
> nleft=nbytes;
> printf("Start writing Bytes = %d Mesg=%s\n", nbytes,msg);
> while(nleft>0)
> {
> nwritten=write(sockfd,msg,nleft);
> printf("WRITEN0 :nwritten=%d\n", nwritten);
> if(nwritten <= 0)
> { printf("WRITEN 1:%d",nwritten);
> return(nwritten); //error
> }
> printf("WRITE01:%d\n",nwritten);
> nleft-=nwritten;
> msg+=nwritten;
> }
> printf("WRITEN2 :%d,%d\n",nbytes,nleft);
> return(nbytes-nleft);
> }
> main()
> {
> struct sockaddr_in sock;
> int sfd,n,i;
> char buff[200],reply[250];
> buff[0] =0, reply[0] = 0;
> sfd = socket(AF_INET,SOCK_STREAM,0);
> if (sfd < 0) { printf("Unable to open socket\n"); exit(0); }
> bzero((char*)&sock,sizeof(socket));
> sock.sin_family = AF_INET;
> sock.sin_port = htons(SERV_PORT);
> sock.sin_addr.s_addr = inet_addr(SERV_ADDR);
>
> if(connect(sfd,(struct sockaddr *)&sock,sizeof(sock))<0)
> { printf("Unable to connect to server\n");
>
> }
> else
> {
>
> strcpy(buff,"GET /home/nachi/LatestVersion.html HTTP/1.0\r\n");
> printf("%s",buff);
> n = strlen(buff);
> printf("main:N = %d\n",n);
> i = writen(sfd,buff,n);
> printf("\n%d\n",i);
> if(i != n)
> printf (" Write Error \n");
> //printf("success0!");
> else {
> printf("success1!");
> if(readn(sfd,reply,250)<0)
> {
> printf("read error\n");
> }
> //printf("success2!");
> else
> printf ("read: %s ",reply);
> }
> }
>
> close(sfd);
> }
- Next message: Naim R. El-Far: "Simulating Network Latency"
- Previous message: P Gentry: "Re: Firewall box configuration"
- In reply to: Nachiketh: "HTTP client program"
- Next in thread: DoesntMatter: "Re: HTTP client program"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]