Solved

Getting a file from another host in C

Posted on 1997-08-10
6
266 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
Most of the applications these days are on Cloud. Cloud is ubiquitous with many service providers in the market. Since it has many benefits such as cost reduction, software updates, remote access, disaster recovery and much more.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now