Solved

Getting a file from another host in C

Posted on 1997-08-10
6
267 Views
Last Modified: 2013-12-23
I want that my C program reads a file located in another host,  passing it  the URL location as parameter.
0
Comment
Question by:gambito042797
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:pxh
ID: 1582231
What server function does the other host have?
0
 

Author Comment

by:gambito042797
ID: 1582232
I need the necesary instructions to make my program behaves as does a browser. For example, if you type in a browser http://www.host.xxx/path/filename.gif, then you get in the browser that file, wathever SO and functions the server you adressed has, well, that server must have a Web Server serving those files. I think this must be done by doing that my program send a CGI request such as

GET /docX.html HTTP/1.0
Accept: www/source
Accept: text/html
Accept: image/gif
User-Agent:  Lynx/2.2  libwww/2.14
From:  myname@myhost.xxx
     * blank line *

and then the program must receive an answer from the server similar to this one:

HTTP/1.0 200 OK
Date: Monday, 02-Feb-96 23:04:12 GMT
Server: NCSA/1.1
MIME-version: 1.0
Last-modified: Monday, 15-May-96 23:33:16 GMT
Content-type: text/html
Content-length: 2345
     * blank line *
<HTML><HEAD><TITLE> . . . the HTML page


I do know how to implement the server CGI part, but what I want is to implement the client behavior in my program (not a full browser, only be able to get a file from another server), that is, how does my program has to send the request and how does it has to wait for the answer.


0
 
LVL 5

Expert Comment

by:julio011597
ID: 1582233
This just involves a bit of socket programming - it's like a client which telnets to port 80 of a server machine.

But, are you aware that, when a web server sends, say, an image, it usually sends it encoded?

-julio
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:gambito042797
ID: 1582234
Yes, I've put above the encoding. The difference in image files is the line Content-type that will be:

Content-type: image/gif

and after the blank line will come the binary data of the file instead of the code for an html page. But thanks.
0
 
LVL 5

Accepted Solution

by:
julio011597 earned 200 total points
ID: 1582235
Ok, i'll give you the code.

It should be easy to customize, but feel free to add comments if you need any clarifications.


-//--
/* url_get: get an url from a web server (port 80) */
/* Please, check the include files on your platform */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>     /* bzero() and bcopy() */
#include <netdb.h>      /* gethostbyname() */
#include <sys/socket.h> /* socket() and connect() */
#include <arpa/inet.h>  /* htons() */
#include <sys/errno.h>  /* errno */

int main(void)
{
  /* this are the parameters */
  /* in case you need to make this a function */
  FILE *answer = stdout;
  char host[] = "www.experts-exchange.com";
  char url[] = "/";
  char err_str[80];

  int sock;
  struct hostent *hep;
  struct sockaddr_in sai;
  FILE *infile, *outfile;
  char buff[BUFSIZ];

  if(!(hep = gethostbyname(host))) {
    strcpy(err_str, "gethostbyname error");
    return errno;
  }
  if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    strcpy(err_str, "socket error");
    return errno;
  }

  bzero((char *)&sai, sizeof(struct sockaddr_in));
  sai.sin_family = AF_INET;
  sai.sin_port = htons((unsigned short)80);
  bcopy(hep->h_addr, (char *)&(sai.sin_addr.s_addr), hep->h_length); /* note: could just be 'sai.sin_addr' */

  if(connect(sock, (struct sockaddr *)&sai, sizeof(sai)) == -1) {
    strcpy(err_str, "connect error");
    return errno;
  }

  if(!(infile = fdopen(sock, "r"))) {
    strcpy(err_str, "fdopen(in) error");
    return errno;
  }
  if(!(outfile = fdopen(sock, "w"))) {
    strcpy(err_str, "fdopen(out) error");
    return errno;
  }

  fprintf(outfile, "GET %s HTTP1.0\n\n", url);
  fflush(outfile);

  while(fgets(buff, BUFSIZ, infile)) fputs(buff, answer);
  fflush(answer);

  fclose(infile); fclose(outfile);
  close(sock);

  return EXIT_SUCCESS;
}
--//--

As you may see, this is a self standing program, so that you may check it from the command line.

Just, remember to compile it first ;)

Cheers, julio
0
 

Author Comment

by:gambito042797
ID: 1582236
It worked!!!  I changed  the host and the path and worked fine.
Thanks a lot Julio.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Forwarding web requests to different web servers 15 119
logging buffered 8 48
Lightweight Networking 9 43
How to simulate latency? 5 30
Let’s list some of the technologies that enable smooth teleworking. 
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question