#include #include #include #include #include #include #include #include #include #define WWWPORT 80 /* argv[1] will be hostname, argv[2] will be page address. */ int main(int argc, char** argv) { int cfd; // socket file descriptor int len, n; // generic char buf[1024]; struct sockaddr_in saddr; // server addr info if (argc<2) exit(1); // set server info saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = inet_addr(argv[1]); saddr.sin_port = htons(WWWPORT); // create socket record in local os cfd = socket(AF_INET,SOCK_STREAM,0); // contact server, three-way handshake, connect(cfd,(struct sockaddr*)&saddr,sizeof(saddr)); if (cfd<0) exit(1); // connection established // form request sprintf(buf,"GET /home/%s\n",argv[2]); len = strlen(buf)+1; write(cfd,buf,len); // read and print result; n = 1; while (n>0) { n = read(cfd,buf,1023); buf[n] = 0; // make sure string is 0-terminated printf("%s",buf); } printf("\n"); close(cfd); exit(0); }