#include #include #include #include #include #include #include void Die(char *mess) { perror(mess); exit(1); } //this is a client that establishes a TCP session with another client int main(int argc, char *argv[]) { GetImage(); } //main int GetImage(void) { #define BUFFSIZE 2048 #define SendStr "GET /cgi-bin/getimage.cgi?motion=0 HTTP/1.1\r\nHost: 192.168.0.106\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6\r\nAccept: image/png,*/*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nIf-Modified-Since: Sat, 01 Jan 2000 18:09:09 GMT\r\nCache-Control: max-age=0\r\n\r\n" int sock; struct sockaddr_in echoserver; unsigned char buffer[BUFFSIZE]; unsigned int echolen; int received = 0,status=0,bytes=0,numbytes,pasthtml; char ip[20],port[10]; FILE *fptr; //by default this computer uses a port such as 49342 to send strcpy(ip,"192.168.0.106"); strcpy(port,"80"); //1047 80 if (0) { //argc != 4) { fprintf(stderr, "USAGE: TCPecho \n"); exit(1); } /* Create the TCP socket */ if ((sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { Die("Failed to create socket\n"); } /* Construct the server sockaddr_in structure */ memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(ip); /* IP address */ echoserver.sin_port = htons(atoi(port)); /* server port */ /* Establish connection */ if (connect(sock,(struct sockaddr *)&echoserver,sizeof(echoserver))<0) { Die("Failed to connect with server\n"); } //Send the HTTP request for an image if (write(sock,SendStr,sizeof(SendStr))!= sizeof(SendStr)) { Die("Failed to send bytes to client"); } // printf("%d: %s",bytes,buffer); fptr=fopen("out.jpg","wb"); numbytes=0; pasthtml=0; while ((status=read(sock,buffer,sizeof(buffer)-1))>0) { //printf("%d: %s", status, buffer); //printf("%d\n",status); if (pasthtml<3) { //remove HTML header //basically 0d0a0d0a is the end of the header bytes=0; while(bytes3 fwrite(buffer,status,1,fptr); } // numbytes+=status; } //end while fclose(fptr); // printf("%d: %s",bytes,buffer); // fprintf(stdout,"done\n"); close(sock); exit(0); } //end GetImage