Solved

Download

Posted on 2002-03-27
7
295 Views
Last Modified: 2010-04-02
I'm using the free command line compiler from borland and I want to download a file from the web as easy as posible. Can anyone give me an example?

Regarding this link, edited by Moderator from title line
http://www.myserver.com/temp.txt
0
Comment
Question by:kpnkpn
7 Comments
 

Expert Comment

by:krydea
ID: 6901852
it's verry easy!
do you got anny question's??
btw: you have to link it to winsock.lib(adding the lib in to the exe to use it laterthis is just a arg like bcc bla.c +l winsock.lib but i don't know that one so read for that the reference or ellse download lcc win 32 good compiler and free!

//
// Pass the server name and full path of the file on the
// command line and redirect the output to a file. The program
// prints messages to stderr as it progresses.
//
// Example:
//            GetHTTP www.idgbooks.com /index.html > index.html
//

#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <winsock.h>


void GetHTTP(LPCSTR lpServerName, LPCSTR lpFileName);

// Helper macro for displaying errors
#define PRINTERROR(s)      \
            fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())

////////////////////////////////////////////////////////////

void main(int argc, char **argv)
{
      WORD wVersionRequested = MAKEWORD(1,1);
      WSADATA wsaData;
      int nRet;

      //
      // Check arguments
      //
      if (argc != 3)
      {
            fprintf(stderr,
                  "\nSyntax: GetHTTP ServerName FullPathName\n");
            return;
      }

      //
      // Initialize WinSock.dll
      //
      nRet = WSAStartup(wVersionRequested, &wsaData);
      if (nRet)
      {
            fprintf(stderr,"\nWSAStartup(): %d\n", nRet);
            WSACleanup();
            return;
      }
      
      //
      // Check WinSock version
      //
      if (wsaData.wVersion != wVersionRequested)
      {
            fprintf(stderr,"\nWinSock version not supported\n");
            WSACleanup();
            return;
      }

      //
      // Set "stdout" to binary mode
      // so that redirection will work
      // for .gif and .jpg files
      //
      _setmode(_fileno(stdout), _O_BINARY);

      //
      // Call GetHTTP() to do all the work
      //
      GetHTTP(argv[1], argv[2]);

      //
      // Release WinSock
      //
      WSACleanup();
}

////////////////////////////////////////////////////////////

void GetHTTP(LPCSTR lpServerName, LPCSTR lpFileName)
{
      //
      // Use inet_addr() to determine if we're dealing with a name
      // or an address
      //
      IN_ADDR            iaHost;
      LPHOSTENT      lpHostEntry;

      iaHost.s_addr = inet_addr(lpServerName);
      if (iaHost.s_addr == INADDR_NONE)
      {
            // Wasn't an IP address string, assume it is a name
            lpHostEntry = gethostbyname(lpServerName);
      }
      else
      {
            // It was a valid IP address string
            lpHostEntry = gethostbyaddr((const char *)&iaHost,
                                    sizeof(struct in_addr), AF_INET);
      }
      if (lpHostEntry == NULL)
      {
            PRINTERROR("gethostbyname()");
            return;
      }


      //      
      // Create a TCP/IP stream socket
      //
      SOCKET      Socket;      

      Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (Socket == INVALID_SOCKET)
      {
            PRINTERROR("socket()");
            return;
      }


      //
      // Find the port number for the HTTP service on TCP
      //
      LPSERVENT lpServEnt;
      SOCKADDR_IN saServer;

      lpServEnt = getservbyname("http", "tcp");
      if (lpServEnt == NULL)
            saServer.sin_port = htons(80);
      else
            saServer.sin_port = lpServEnt->s_port;


      //
      // Fill in the rest of the server address structure
      //
      saServer.sin_family = AF_INET;
      saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);


      //
      // Connect the socket
      //
      int nRet;

      nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
      if (nRet == SOCKET_ERROR)
      {
            PRINTERROR("connect()");
            closesocket(Socket);
            return;
      }

      
      //
      // Format the HTTP request
      //
      char szBuffer[1024];

      sprintf(szBuffer, "GET %s\n", lpFileName);
      nRet = send(Socket, szBuffer, strlen(szBuffer), 0);
      if (nRet == SOCKET_ERROR)
      {
            PRINTERROR("send()");
            closesocket(Socket);      
            return;
      }

      //
      // Receive the file contents and print to stdout
      //
      while(1)
      {
            // Wait to receive, nRet = NumberOfBytesReceived
            nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
            if (nRet == SOCKET_ERROR)
            {
                  PRINTERROR("recv()");
                  break;
            }

            fprintf(stderr,"\nrecv() returned %d bytes", nRet);

            // Did the server close the connection?
            if (nRet == 0)
                  break;
            // Write to stdout
        fwrite(szBuffer, nRet, 1, stdout);
      }
      closesocket(Socket);      
}
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 6902589
This is a DUPLICATE QUESTION, and I will reject the proposed answer, refund points and close this item for you.  Additionally, you can avoid these problems by not hitting submit more than once, and/or if the application hangs, using the RELOAD function versus browser controls such as refresh/F5 or back button.  In addition, there are access problems when a hyperlink is posted in the topic of the question, and I will need to modify this on your other open question for you.  Please include links in the comment area versus the topic or subject line.

krydea -->  You LOCKED both duplicate questions with a proposed answer, please do not do this, as it is a disservice to the Asker and this site.  Please read the Question/Answer information on the bottom of this page as well as the HELP DESK Guidelines and Member Agreement.  If you have questions in this regard, please post a zero point question in the Community Support topic area where we can continue this discussion.

Thanks,
Moondancer - EE Moderator

0
 

Expert Comment

by:krydea
ID: 6902957
:| why did je delete bought??????????

damn! i answerd 1 of them!!!?!?!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kpnkpn
ID: 6903756
hm.. all of the answers have been Rejected Answer by someone who thinks he is admin. I want to give ju the points krydea :(
0
 

Author Comment

by:kpnkpn
ID: 6903765
Moondancer can you please delete this dublicated questions so I can get back my 500 question points that EE took from me because of this bug?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6905141
kpnkpn,
If you want to award the points to krydea, you still can on your original question.
http://www.experts-exchange.com/cplusprog/Q.20282193.html

The admin assigned this question zero points, because it was a duplicate question.

I believe the admin also rejected both posted answers, because the expert posted an answer in both duplicate questions, which is agaisnt EE policy.

krydea,
To avoid this in the future, post only one answer.  Do NOT post an answer in both duplicate questions.

Most C++ experts post their answers as comments.
Also, I suggest you post your answer as a comment.  This would have also avoided this problem.
0
 

Expert Comment

by:krydea
ID: 6905199
oke Axter, i will!
btw: i got a qestion i know you can answer... about activeX(OCX libaries) in vc++6.0 c++ programmed not MFC:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20282364

plz help.....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

861 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

24 Experts available now in Live!

Get 1:1 Help Now