c++ socket 模拟 http

// GetURLContext.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <windows.h>
#pragma comment(lib,"Ws2_32.lib")

#define DEFAULT_BUFLEN 1024
#define DEFAULT_PORT 80
#undef _UNICODE
int count = 0;
void myStrcat(char* _Destination,int start,char* _Source) {
    for (int i = 0; i < start; i++, count++) {
        _Destination[count] = _Source[i];
    }
}
int main()
{
    int iResult;
    WSADATA wsaData;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;

    int recvbuflen = DEFAULT_BUFLEN;
    char *sendbuf = "GET / HTTP/1.1\r\n\r\n";
    char recvbuf[DEFAULT_BUFLEN] = "\0";
    char htmlContent[DEFAULT_BUFLEN * 16] = "\0";

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);//初始化
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error: %d\n", iResult);
        system("pause");
        return 1;
    }

    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建socket
    if (ConnectSocket == INVALID_SOCKET) {
        wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        system("pause");
        return 1;
    }

    clientService.sin_family = AF_INET;// IPv4 网络协议的套接字类型,AF_INET6 则是 IPv6 的
    clientService.sin_addr.s_addr = inet_addr("192.168.1.253");//baidu.com
    clientService.sin_port = htons(DEFAULT_PORT);

    iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));//连接
    if (iResult == SOCKET_ERROR) {
        wprintf(L"connect failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        system("pause");
        return 1;
    }

    iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);//发送
    if (iResult == SOCKET_ERROR) {
        wprintf(L"send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        system("pause");
        return 1;
    }
    printf("Bytes Sent: %d\n", iResult);

    iResult = shutdown(ConnectSocket, SD_SEND);//关闭连接
    if (iResult == SOCKET_ERROR) {
        wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        system("pause");
        return 1;
    }

    // 等待数据直到对方关闭连接

    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if (iResult > 0) {
            wprintf(L"Bytes received: %d\n", iResult);
            myStrcat(htmlContent, iResult, recvbuf);
        }
        else if (iResult == 0) {
            wprintf(L"Connection closed\n");
        }
        else {
            wprintf(L"recv failed with error: %d\n", WSAGetLastError());
        }
    } while (iResult > 0);


    // 关闭socket
    iResult = closesocket(ConnectSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"close failed with error: %d\n", WSAGetLastError());
        WSACleanup();
        system("pause");
        return 1;
    }

    WSACleanup();

    wprintf(L"html:\n\n%S\n", htmlContent);
    system("pause");
    return 0;
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/osummertime/article/details/72082120