Solved

Download

Posted on 2002-03-27
7
296 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 67
Issue accessing member variable of atomic class 16 115
mixing C++ & C# in Vis Studio 2013 7 168
FMX TCameraComponent Problem 2 68
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

813 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

13 Experts available now in Live!

Get 1:1 Help Now