项目中一个小问题的扩展~绝对酷酷
由于项目需要,制作的一个小东东,可以随便下载任意类型的文件,从服务器上,单线程~对于这个玩意儿可以随意扩展制作自己的CRWLER~~LA啦~~看看吧
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <winsock.h>
#include <iostream.h>
#include <fstream.h>
void GetSOL(LPCSTR lpServerName, LPCSTR lpFileName);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
void main()
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
// Initialize WinSock.dll
nRet = WSAStartup(wVersionRequested, &wsaData);//success:0
if (nRet)
{
fprintf(stderr,"\nWSAStartup(): %d\n", nRet);
WSACleanup();
return;
}
if (wsaData.wVersion != wVersionRequested) // Check WinSock version
{
fprintf(stderr,"\nWinSock version not supported\n");
WSACleanup();
return;
}
_setmode(_fileno(stdout), _O_BINARY); // Set "stdout" to binary mode
GetSOL("localhost", "/download/my.cab"); // Get sol file
WSACleanup();
}
////////////////////////////////////////////////////////////
void GetSOL(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
ofstream outfile;
int i;
i=0;
outfile.open("d:\\my.cab", ios::binary | ios::out | ios::trunc,0);//clear the content of sol file
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
outfile.close();
break;
}
//fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0)
{
outfile.close();
break;
}
// Write to stdout
//fwrite(szBuffer, nRet, 1, stdout);
outfile.close();
outfile.open("d:\\my.cab", ios::binary | ios::app | ios::out,0);
while(i<nRet){
outfile << szBuffer[i]; //buffer to file!
i++;
}
i=0;
}
closesocket(Socket);
}
#include <fcntl.h>
#include <io.h>
#include <winsock.h>
#include <iostream.h>
#include <fstream.h>
void GetSOL(LPCSTR lpServerName, LPCSTR lpFileName);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
void main()
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
// Initialize WinSock.dll
nRet = WSAStartup(wVersionRequested, &wsaData);//success:0
if (nRet)
{
fprintf(stderr,"\nWSAStartup(): %d\n", nRet);
WSACleanup();
return;
}
if (wsaData.wVersion != wVersionRequested) // Check WinSock version
{
fprintf(stderr,"\nWinSock version not supported\n");
WSACleanup();
return;
}
_setmode(_fileno(stdout), _O_BINARY); // Set "stdout" to binary mode
GetSOL("localhost", "/download/my.cab"); // Get sol file
WSACleanup();
}
////////////////////////////////////////////////////////////
void GetSOL(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
ofstream outfile;
int i;
i=0;
outfile.open("d:\\my.cab", ios::binary | ios::out | ios::trunc,0);//clear the content of sol file
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
outfile.close();
break;
}
//fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0)
{
outfile.close();
break;
}
// Write to stdout
//fwrite(szBuffer, nRet, 1, stdout);
outfile.close();
outfile.open("d:\\my.cab", ios::binary | ios::app | ios::out,0);
while(i<nRet){
outfile << szBuffer[i]; //buffer to file!
i++;
}
i=0;
}
closesocket(Socket);
}