c++ socket 模拟 http

来源:互联网 发布:淘宝行业层级 编辑:程序博客网 时间:2024/06/05 02:41
// 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 _UNICODEint 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;}

这里写图片描述

0 0
原创粉丝点击