Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 1.00 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <arpa/inet.h>
  7.  
  8. #define port 80
  9. /*
  10.  *connect, send @ receive
  11.  *in one function
  12.  */
  13.  
  14. int socks(char *host) {
  15.   int sock;
  16.   char buffer[1024];
  17.   char message[] = "GET / HTTP/1.0\r\n\r\n";
  18.   struct sockaddr_in sock_struct;
  19.  
  20.  
  21.   /* socket descriptorx */
  22.   sock=socket(AF_INET, SOCK_STREAM, 0);
  23.  
  24.   sock_struct.sin_family=AF_INET;
  25.   sock_struct.sin_port=htons(port);
  26.   sock_struct.sin_addr.s_addr=gethostbyname(host);
  27.  
  28.   //bind(sock, (const)sock_struct, sizeof(sock_struct));
  29.   if((connect(sock, (struct sockaddr *)&sock_struct, sizeof(sock_struct))) < 0) {
  30.     perror("socket error");
  31.     exit(2);
  32.   }
  33.   send(sock, message, sizeof(message), 0);
  34.   recv(sock, buffer, sizeof(buffer), 0);
  35.  
  36.   printf(buffer);
  37.  
  38.   close(sock);
  39. }
  40.  
  41. int main(int argc, char **argv) {
  42.  
  43.   if (argc < 2) {
  44.     fprintf(stderr, "usage: http <host>\n");
  45.     return;
  46.   }
  47.   socks(argv[1]);
  48.   return 0;
  49. }