Solved

Download

Posted on 2002-03-27
7
294 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

19 Experts available now in Live!

Get 1:1 Help Now