Download

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
kpnkpnAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
MoondancerConnect With a Mentor Commented:
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
 
krydeaCommented:
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
 
krydeaCommented:
:| why did je delete bought??????????

damn! i answerd 1 of them!!!?!?!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
kpnkpnAuthor Commented:
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
 
kpnkpnAuthor Commented:
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
 
AxterCommented:
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
 
krydeaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.