Advertisement
krot

http-client.c

Sep 16th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. /* http-client.c
  2.  *
  3.  * Copyright (c) 2000 Sean Walton and Macmillan Publishers.  Use may be in
  4.  * whole or in part in accordance to the General Public License (GPL).
  5.  *
  6.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  7.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  8.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  9.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  10.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  11.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  12.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  13.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  14.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  15.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16.  * SUCH DAMAGE.
  17. */
  18.  
  19. /*****************************************************************************/
  20. /*** http-client.c                                                         ***/
  21. /***                                                                       ***/
  22. /*** This program shows what the HTTP server sends to the client.  First,  ***/
  23. /*** it opens a TCP socket to the server.  Then, it sends the request      ***/
  24. /*** "GET <resource> HTTP/1.0\n\n" (the second newline is needed for the   ***/
  25. /*** "message-end" message.  Lastly it prints out the reply.               ***/
  26. /*****************************************************************************/
  27.  
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <sys/socket.h>
  33. #include <resolv.h>
  34. #include <errno.h>
  35.  
  36. #define MAXBUF  1024
  37. void PANIC(char *msg);
  38. #define PANIC(msg)  {perror(msg); abort();}
  39.  
  40. int main(int Count, char *Strings[])
  41. {   int sockfd, bytes_read;
  42.     struct sockaddr_in dest;
  43.     char buffer[MAXBUF];
  44.  
  45.     /*---Make sure we have the right number of parameters---*/
  46.     if ( Count != 3 )
  47.         PANIC(stderr, "usage: testport <IP-addr> <send-msg>\n");
  48.     if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  49.         PANIC("Socket");
  50.  
  51.     /*---Initialize server address/port struct---*/
  52.     bzero(&dest, sizeof(dest));
  53.     dest.sin_family = AF_INET;
  54.     dest.sin_port = htons(80); /*default HTTP Server port */
  55.     if ( inet_addr(Strings[1], &dest.sin_addr.s_addr) == 0 )
  56.         PANIC(Strings[1]);
  57.  
  58.     /*---Connect to server---*/
  59.     if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
  60.         PANIC("Connect");
  61.  
  62.     sprintf(buffer, "GET %s HTTP/1.0\n\n", Strings[2]);
  63.     send(sockfd, buffer, strlen(buffer), 0);
  64.  
  65.     /*---While there's data, read and print it---*/
  66.     do
  67.     {
  68.         bzero(buffer, sizeof(buffer));
  69.         bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
  70.         if ( bytes_read > 0 )
  71.             printf("%s", buffer);
  72.     }
  73.     while ( bytes_read > 0 );
  74.  
  75.     /*---Clean up---*/
  76.     close(sockfd);
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement