Solved

Getting a file from another host in C

Posted on 1997-08-10
6
259 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
Comment Utility
What server function does the other host have?
0
 

Author Comment

by:gambito042797
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gambito042797
Comment Utility
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
Comment Utility
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
Comment Utility
It worked!!!  I changed  the host and the path and worked fine.
Thanks a lot Julio.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

I was recently sitting at a desk at work with one of my colleagues and needed some information on my home computer. He watched as I turned on my home computer, established a remote session into it, got the information I needed and then shut it down …
#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now