Solved

Download

Posted on 2002-03-27
7
299 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 160
C++ question 3 84
Safe conversion? 4 87
calling back function with same name and different signature 1 45
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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