![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Professional Programmer
|
This should help you http://www.perlfect.com/articles/http.shtml
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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;
}
__________________
www.heldtogether.co.uk |
|
|
|
|
|
#7 |
|
Professional Programmer
|
It sends the header to the site. There must be something wrong with the formatting of the request.
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 |
|
Professional Programmer
|
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;
} |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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;
}After connecting Bytes Sent: 26 After connecting Bytes Received: 0 Connection Closed. What did you get?
__________________
www.heldtogether.co.uk |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|