[C++] 纯文本查看 复制代码 #include <WinSock2.h>
void SocketTest(){
WSADATA wsaData;
//初始化socket库
WSAStartup(MAKEWORD(2, 0), &wsaData);
//创建
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN sa;
sa.sin_family = AF_INET;
//设置链接服务端的端口
sa.sin_port = htons(80);//端口号
sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//尝试连接服务器
if (connect(sock, (SOCKADDR*)&sa, sizeof(sa)) == SOCKET_ERROR)
{
MessageBoxA(0, "卧槽 炸了", "", 0);
}
//与服务器进行通信
char aSendString[] =
"GET /announcement.html HTTP/1.1\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0\r\n";
char result[128];
send(sock, aSendString, sizeof(aSendString) - 1, 0);//发包测试
recv(sock, result, sizeof(result) - 1, 0);
MessageBoxA(0, result, 0, 0);
}
新手socket编程,执行到recv(sock, result, sizeof(result) - 1, 0);那一步就卡住 卡了几十秒后服务器就会返回一个408 然后软件这里提示一行乱码
|