Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 30th, 2006, 10:30 AM   #1
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Creating a file downloader

I know about sockets, I've created a chat program, so don't just point me to the door!

I'm trying to make a program to download a file, but I'm not having much luck, and google is being a evil, and not showing me the good stuff.

Does anyone know how I can connect to a website on port 80 and download the file which has been pointed to?

Cheers!
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Jun 30th, 2006, 10:35 AM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
You might need to look into HTTP Headers.

You can connect but the server won't know what you want it to do unless you tell it to.
Wizard1988 is offline   Reply With Quote
Old Jun 30th, 2006, 10:36 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
What's there not to understand? Just go to the link (with HTTP, duh?) and receive the information and write it to a file.

Edit: Here's a link you can play with. It contains a super small and simple client at the end.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; Jun 30th, 2006 at 10:46 AM.
nnxion is offline   Reply With Quote
Old Jun 30th, 2006, 10:37 AM   #4
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
I can't seem to recive the information, I'm sure I've had it working before, but my brain doesn't seem to want to actually work at the minute!
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Jun 30th, 2006, 10:41 AM   #5
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
This should help you http://www.perlfect.com/articles/http.shtml
Wizard1988 is offline   Reply With Quote
Old Jun 30th, 2006, 10:52 AM   #6
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Thanks, that did help clear a bit up.

Can anyone see any problems with this code?

#include <stdio.h>
#include <iostream>
#include "winsock2.h"

using namespace std;

int main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        cout << "Error at WSAStartup()\n";

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        cout << "Error at socket(): \n" << WSAGetLastError();
        WSACleanup();
        return 0;
    }

    // Connect to a server.
    sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr( "64.233.167.99" ); //google.com
    server.sin_port = htons( 80 );

    if ( connect( m_socket, (SOCKADDR*) &server, sizeof(server) ) == SOCKET_ERROR) {
        cout << "Failed to connect.\n";
        WSACleanup();
        return 0;
    }

    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "GET /imghp HTTP/1.0"; //get google image search
    char recvbuf[32] = "";
    
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
    
    while( bytesRecv == SOCKET_ERROR ) {
       bytesRecv = recv( m_socket, recvbuf, 32, 0 );
       cout << recvbuf;
       if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
           cout << "Connection Closed.\n";
           break;
        }
        if (bytesRecv < 0)
            return 0;
    }
    if (bytesSent == -1)
    {
           return 0;
    }
    system ("PAUSE");
    return 0;
    }
It just hangs at a black screen!
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Jun 30th, 2006, 11:00 AM   #7
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
It sends the header to the site. There must be something wrong with the formatting of the request.
Wizard1988 is offline   Reply With Quote
Old Jun 30th, 2006, 11:02 AM   #8
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
If you look at the website you gave me, this is what the request ought to be sent as, if I'm reading it right...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Jun 30th, 2006, 11:21 AM   #9
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
I somewhat got it to work

#include <stdio.h>
#include <iostream>
#include "winsock2.h"

using namespace std;

int main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        cout << "Error at WSAStartup()\n";

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        cout << "Error at socket(): \n" << WSAGetLastError();
        WSACleanup();
        return 0;
    }

    // Connect to a server.
    sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr( "64.233.167.99" ); //google.com
    server.sin_port = htons( 80 );

    if ( connect( m_socket, (SOCKADDR*) &server, sizeof(server) ) == SOCKET_ERROR) {
        cout << "Failed to connect.\n";
        WSACleanup();
        return 0;
    }
	cout << "After connecting" << endl;

    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "GET /index.html HTTP/1.1\n\n"; //get google image search
    char recvbuf[32] = "";
    
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );

	cout << "After connecting" << endl;
    
    while( bytesRecv == SOCKET_ERROR ) {
       bytesRecv = recv( m_socket, recvbuf, 32, 0 );
       cout << recvbuf;
       if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
           cout << "Connection Closed.\n";
           break;
        }
        if (bytesRecv < 0)
            return 0;
    }
    if (bytesSent == -1)
    {
           return 0;
    }
    system ("PAUSE");
    return 0;
    }
Wizard1988 is offline   Reply With Quote
Old Jun 30th, 2006, 11:44 AM   #10
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Got it to work in what sense? I tried compiling a slightly modified version:
#include <stdio.h>
#include <iostream>
#include "winsock2.h"

using namespace std;

int main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        cout << "Error at WSAStartup()\n";

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        cout << "Error at socket(): \n" << WSAGetLastError();
        WSACleanup();
        return 0;
    }

    // Connect to a server.
    sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr( "64.233.167.99" ); //google.com
    server.sin_port = htons( 80 );

    if ( connect( m_socket, (SOCKADDR*) &server, sizeof(server) ) == SOCKET_ERROR) {
        cout << "Failed to connect.\n";
        WSACleanup();
        return 0;
    }
	cout << "After connecting" << endl;

    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "GET /index.html HTTP/1.1\n\n"; //get google image search
    char recvbuf[32] = "";
    
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
    cout << "Bytes Sent: " << bytesSent << "\n";
    cout << "After connecting" << endl;
    
    while( bytesRecv == SOCKET_ERROR ) {
       bytesRecv = recv( m_socket, recvbuf, 32, 0 );
       cout << "Bytes Received: " << bytesRecv << "\n";
       if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
           cout << "Connection Closed.\n";
           break;
        }
        if (bytesRecv < 0)
            return 0;
    }
    if (bytesSent == -1)
    {
           return 0;
    }
    system ("PAUSE");
    return 0;
    }
And the output is:
After connecting
Bytes Sent: 26
After connecting
Bytes Received: 0
Connection Closed.

What did you get?
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC