#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include "stdafx.h"
#include <stdlib.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 1024
#define DEFAULT_PORT 80
void myStrcat(char * desstination, int start, int size, char * source) {
*(desstination += start);
for (int i = 0; i < size; i++) {
* desstination++ = * source++;
}
}
int main() {
system("chcp 65001");
//----------------------
// Declare and initialize variables.
int iResult;
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
int recvbuflen = DEFAULT_BUFLEN;
char * sendbuf = "GET /index.php?act=member.login HTTP/1.1\r\n"
"Host: m.xnote.cn\r\n"
"Connection: keep-alive\r\n\r\n";
//"Upgrade-Insecure-Requests: 1 "
//"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 "
//"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 "
//"Accept-Encoding: gzip, deflate, sdch "
//"Accept-Language: zh-CN,zh;q=0.8 ";
//----------------------
// Initialize Win sock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error: %d ", iResult);
system("pause");
return 1;
}
//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error: %ld ", WSAGetLastError());
WSACleanup();
system("pause");
return 1;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
ADDRINFOW hints;
ADDRINFOW * result = NULL;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
iResult = GetAddrInfoW(L"m.xnote.cn", NULL, &hints, &result);
if (iResult == -1) {
wprintf(L"GetAddrInfoW with error: %d ", WSAGetLastError());
closesocket(ConnectSocket);
FreeAddrInfoW(result);
WSACleanup();
system("pause");
return 1;
}
wchar_t ipstringbuffer[46];
DWORD ipbufferlength = 46;
iResult = WSAAddressToString((LPSOCKADDR)result->ai_addr, (DWORD)result->ai_addrlen, NULL, ipstringbuffer, &ipbufferlength);
if (iResult) {
wprintf(L"WSAAddressToString failed with %u ", WSAGetLastError());
closesocket(ConnectSocket);
FreeAddrInfoW(result);
WSACleanup();
system("pause");
return 1;
}
wprintf(L"IPv4 address %ws ", ipstringbuffer);
iResult = InetPton(AF_INET, ipstringbuffer, &clientService.sin_addr);
if (iResult != 1) {
wprintf(L"IP adders with error: %d ", WSAGetLastError());
closesocket(ConnectSocket);
FreeAddrInfoW(result);
WSACleanup();
system("pause");
return 1;
}
clientService.sin_port = htons(DEFAULT_PORT);
//----------------------
// Connect to server.
iResult = connect(ConnectSocket, (SOCKADDR * )&clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR) {
wprintf(L"connect failed with error: %d ", WSAGetLastError());
closesocket(ConnectSocket);
FreeAddrInfoW(result);
WSACleanup();
system("pause");
return 1;
}
//----------------------
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
wprintf(L"send failed with error: %d ", WSAGetLastError());
closesocket(ConnectSocket);
FreeAddrInfoW(result);
WSACleanup();
system("pause");
return 1;
}
printf("Bytes Sent: %d \n", iResult);
char * recvbuf = (char *)malloc(DEFAULT_BUFLEN * sizeof(char));
char * printbuf = (char * )malloc(DEFAULT_BUFLEN * sizeof(char));
memset(printbuf, 0, sizeof(printbuf));
int printbufSize = 0;
int time_out = 2000; // 2秒
setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&time_out, sizeof(time_out));
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
{
wprintf(L"Bytes received: %d \n", iResult);
printbufSize += iResult;
printbuf = (char *)realloc(printbuf, printbufSize);
myStrcat(printbuf, printbufSize - iResult, iResult, recvbuf);
}
else if (iResult == 0)
wprintf(L"Connection closed \n");
else
wprintf(L"recv failed with error: %d \n", WSAGetLastError());
} while (iResult > 0);
*(printbuf + printbufSize) = '\0';
wprintf(L"%S\n", printbuf);
// close the socket
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"close failed with error: %d ", WSAGetLastError());
FreeAddrInfoW(result);
WSACleanup();
system("pause");
return 1;
}
FreeAddrInfoW(result);
WSACleanup();
return 0;
}
c++ socket Get
最新推荐文章于 2024-05-07 23:34:43 发布