Solved

Getting a file from another host in C

Posted on 1997-08-10
6
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Even if you have implemented a Mobile Device Management solution company wide, it is a good idea to make sure you are taking into account all of the major risks to your electronic protected health information (ePHI).
If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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…

734 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