#define RBC_PORT 80 #define RBC_ADDR "127.0.0.1"
SOCKET SendSocket;
int rc; WSADATA WSAData;
rc = WSAStartup(MAKEWORD(1, 1), &WSAData); if(rc != 0) return FALSE;
struct sockaddr_in srv_address; int namelen;
SendSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(SendSocket != INVALID_SOCKET) { memset(&srv_address,0,sizeof(SOCKADDR_IN)); srv_address.sin_addr.S_un.S_addr=INADDR_ANY; srv_address.sin_family = AF_INET; srv_address.sin_port = 0; } if(bind(SendSocket, (SOCKADDR *) &srv_address, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { closesocket(SendSocket); return FALSE; } else { namelen=sizeof(SOCKADDR_IN); getsockname(SendSocket,(SOCKADDR *)&srv_address,&namelen); return TRUE; }
SOCKADDR_IN remoteAddr; int err; int iMsg = 0; int iLength; int iEnd = 0; BYTE sBuf[4096]; char *RbcMessage[] = { "GET /index.html HTTP/1.0\r\n", "Accept: */*\r\n", "Referer: https://www.sources.ru\r\n", "Accept-Language: ru\r\n", "Accept-Encoding: gzip, deflate\r\n", "User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)\r\n", "Host: www.rbc.ru\r\n\r\n\r\n", NULL };
remoteAddr.sin_family = AF_INET; remoteAddr.sin_port = htons(RBC_PORT); remoteAddr.sin_addr.s_addr = inet_addr(RBC_ADDR);
err = connect (SendSocket, (PSOCKADDR) & remoteAddr, sizeof (remoteAddr)); if(err) { } else { do { send(SendSocket, (LPSTR)RbcMessage[iMsg], strlen(RbcMessage[iMsg]), 0); iMsg++; } while(RbcMessage[iMsg]); iLength = recv(SendSocket, (LPSTR)sBuf+iEnd, sizeof(sBuf)-iEnd, 0); } closesocket(SendSocket);
После выполнения ентого примера, переменная sBuf, поидее должна содержать заглавную страничку с запрашиваемого сайта :) |