Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9.  
  10.  
  11. /* VSIE client program uses TCP protocol to connect to the remote http server.
  12. The program will take 2 input arguments:
  13. 1) command option, get (receive) or head (send)
  14. 2) http URL address
  15. */
  16.  
  17. #define MAX 80
  18. #define MAX2 1024
  19. #define http "HTTP/1.1"
  20. #define TRUE 1
  21. #define FALSE 0
  22. #define HEADERSTOP "nn"
  23. main(int argc, char *argv[])
  24. {
  25. unsigned char *e;
  26. char command[MAX];
  27. char server[MAX];
  28. char path[MAX];
  29. char filename[MAX]= "";
  30. char httpString[MAX];
  31. int i, x, f, n, length = 0;
  32. int numBytes = 0;
  33. int getData = TRUE;
  34. int getFlag = FALSE;
  35. int flag = FALSE;
  36. int headFlag = FALSE;
  37. FILE *in;
  38.  
  39. int sk;
  40. unsigned char buf[MAX2];
  41. struct sockaddr_in remote;
  42. struct hostent *hp;
  43. struct servent *sp;
  44. short port = 0;
  45.  
  46.  
  47. // parse input arguments
  48. sscanf(argv[2],"%[^'/']%s",server,path);
  49.  
  50. if (strcmp(argv[1],"-get") == 0)
  51. {
  52. sprintf(command, "GET");
  53. getFlag = TRUE;
  54. }
  55. else if (strcmp(argv[1],"-head") == 0)
  56. {
  57. sprintf(command, "HEAD");
  58. }
  59.  
  60. //build http 1.1 GET or HEAD message
  61. sprintf(httpString,"%s %s %snHost: %snn", command, path,http,server);
  62.  
  63. printf("command = %s, server = %s, path = %sn", command, server, path);
  64. printf("httpString = %sn",httpString);
  65.  
  66. //parse filename from path
  67. length = strlen(path);
  68. x=0;
  69. f=0;
  70.  
  71. for(i = 0; i < length; i++)
  72. {
  73. //printf("path[%d] = %c n",i,path[i]);
  74. if ((flag == TRUE) & (f == 2))
  75. {
  76. filename[x] = path[i];
  77. x++;
  78. }
  79.  
  80. if (path[i] == '/')
  81. {
  82. flag = TRUE;
  83. f++;
  84. }
  85.  
  86. }
  87.  
  88. printf("filename = %sn", filename);
  89.  
  90. //if command = get, open filename
  91. //if(command == "-get")
  92. if (getFlag == TRUE)
  93. {
  94. if((in = fopen (filename,"w")) == NULL)
  95. {
  96. //printf("FAILURE: opening input file %sn",filename);
  97. perror("FAILURE: opening input file");
  98. exit(1);
  99. }
  100. printf("file opened successfullyn");
  101. }
  102.  
  103. //get internet address of host & port number of http service
  104. hp = gethostbyname(server);
  105. if (hp == NULL)
  106. {
  107. printf("Can't find host name. %sn", server);
  108. exit (1);
  109. }
  110.  
  111. //copy the h_addr (source) to s_add (destination) for n bytes specified by length
  112. bcopy(hp->h_addr,&remote.sin_addr.s_addr,hp->h_length);
  113.  
  114. /* get the port number */
  115. sp = getservbyname("http", "tcp");
  116. if (sp == NULL)
  117. {
  118. printf("can't find port # %dn",sp->s_port);
  119. exit (1);
  120. }
  121. port = sp->s_port;
  122. remote.sin_port = sp->s_port;
  123. printf("port = %d, port = %d n", port, remote.sin_port);
  124.  
  125. //create socket for http server - socket type: Sock_Stream, protocol: TCP
  126. sk = socket(AF_INET,SOCK_STREAM,0);
  127. if (sk < 0)
  128. {
  129. perror("error opening socket");
  130. exit(1);
  131. }
  132. remote.sin_family = AF_INET;
  133.  
  134. //initiate connection to the server address w/ TCP socket
  135. if (connect(sk, (struct sockaddr *) &remote, sizeof(remote)) < 0)
  136. {
  137. printf("connect fails!n");
  138. exit(1);
  139. }
  140. printf("connection successfuln");
  141.  
  142. //send http message
  143. printf("send message:%sn", httpString);
  144. //send(sk,httpString,strlen(httpString)+1,0);
  145. if(send(sk,httpString,sizeof(httpString),0) < 0)
  146. {
  147. printf("send() failed");
  148. //exit(1);
  149. }
  150.  
  151. n = 1;
  152. //Loop until all data received
  153. while(getData == TRUE)
  154. {
  155. //wait for and print the return message
  156. numBytes = recv(sk,buf,sizeof(buf),0);
  157. if (numBytes < 0)
  158. {
  159. perror("error reading from socket");
  160. break;
  161. }
  162. else if (numBytes < MAX2)
  163. {
  164. getData = FALSE;
  165. printf("***end while loop****n");
  166. }
  167. if (headFlag == FALSE){
  168. e = memchr(buf, 'n', sizeof(buf));
  169.  
  170. while (*(e+1) != 'r'){
  171. e = memchr(e+1, 'n', sizeof(buf));
  172. }
  173. headFlag = TRUE;
  174. }
  175.  
  176. printf("n****number of bytes received %d****n",numBytes);
  177.  
  178. //saved the retrieved content into the file (input argument)
  179.  
  180. if (getFlag == TRUE)
  181. {
  182. //printf("write outputn");
  183. printf("%.*sn", (numBytes-763), buf);
  184. if(e != NULL){
  185. fwrite(e, numBytes, 1, in);
  186. e = NULL;
  187. }else{
  188. fwrite(buf, numBytes, 1, in);
  189. }
  190.  
  191. }
  192. n++;
  193. } // end while()
  194.  
  195. //close socket & file
  196. close(sk);
  197.  
  198. if(fclose(in) !=0)
  199. {
  200. perror("FAILURE: Closing input file");
  201. exit(1);
  202. }
  203.  
  204. return 0;
  205. } //end main()
  206.  
  207. ****number of bytes received 1024****
  208. HTTP/1.1 200 OK
  209.  
  210. Date: Tue, 08 Apr 2014 17:37:10 GMT
  211.  
  212. Server: Apache/2.2.22 (Ubuntu)
  213.  
  214. Last-Modified: Tue, 18 Feb 2014 19:41:06 GMT
  215.  
  216. ETag: "1724117-9fb-4f2b373fef880"
  217.  
  218. Accept-Ranges: bytes
  219.  
  220. Content-Length: 2555
  221.  
  222. Vary: Accept-Encoding
  223.  
  224. Content-Type: text/html
  225.  
  226. strtok - split the string into lines
  227. strstr - find a string that contains the words you are looking for
  228. sscanf - scan the line for the value of the integer
  229.  
  230. #include <stdio.h>
  231. #include <string.h>
  232.  
  233. int main(void) {
  234. char httpString[]="HTTP/1.1 200 OKn"
  235. "Date: Tue, 08 Apr 2014 17:37:10 GMTn"
  236. "Server: Apache/2.2.22 (Ubuntu)n"
  237. "Last-Modified: Tue, 18 Feb 2014 19:41:06 GMTn"
  238. "ETag: "1724117-9fb-4f2b373fef880"n"
  239. "Accept-Ranges: bytesn"
  240. "Content-Length: 2555n"
  241. "Vary: Accept-Encodingn"
  242. "Content-Type: text/htmln";
  243. printf("string is %sn", httpString);
  244. char *line;
  245. line = strtok(httpString, "n");
  246. while(line != NULL) {
  247. if (strstr(line, "Content-Length:")!= NULL) {
  248. int theNumber;
  249. sscanf(line, "Content-Length: %d", &theNumber);
  250. printf("The number is %dn", theNumber);
  251. }
  252. line = strtok(NULL, "n");
  253. }
  254. return 0;
  255. }
  256.  
  257. string is HTTP/1.1 200 OK
  258. Date: Tue, 08 Apr 2014 17:37:10 GMT
  259. Server: Apache/2.2.22 (Ubuntu)
  260. Last-Modified: Tue, 18 Feb 2014 19:41:06 GMT
  261. ETag: "1724117-9fb-4f2b373fef880"
  262. Accept-Ranges: bytes
  263. Content-Length: 2555
  264. Vary: Accept-Encoding
  265. Content-Type: text/html
  266.  
  267. The number is 2555
  268.  
  269. char searchString[] = "Content-length:";
  270. int offset, number;
  271. offset = strstr(httpString, searchString);
  272. sscanf(offset + strlen(searchString), "%d", &number);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement