写一个使用WSASend()和WSARecv()函数进行异步操作的列子
时间: 2024-03-05 14:55:05 浏览: 48
好的,下面是一个使用WSASend()和WSARecv()函数进行异步操作的示例:
```c++
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
int main() {
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
std::cout << "WSAStartup failed: " << result << std::endl;
return 1;
}
struct addrinfo* addrResult = nullptr, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
result = getaddrinfo("localhost", DEFAULT_PORT, &hints, &addrResult);
if (result != 0) {
std::cout << "getaddrinfo failed: " << result << std::endl;
WSACleanup();
return 1;
}
SOCKET connectSocket = INVALID_SOCKET;
for (struct addrinfo* ptr = addrResult; ptr != nullptr; ptr = ptr->ai_next) {
connectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (connectSocket == INVALID_SOCKET) {
std::cout << "socket failed: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
result = connect(connectSocket, ptr->ai_addr, static_cast<int>(ptr->ai_addrlen));
if (result == SOCKET_ERROR) {
closesocket(connectSocket);
connectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(addrResult);
if (connectSocket == INVALID_SOCKET) {
std::cout << "Unable to connect to server!" << std::endl;
WSACleanup();
return 1;
}
char sendBuffer[DEFAULT_BUFLEN] = "Hello, server!";
char recvBuffer[DEFAULT_BUFLEN];
DWORD sendBytes = 0, recvBytes = 0, flags = 0;
WSABUF sendWsaBuf, recvWsaBuf;
sendWsaBuf.buf = sendBuffer;
sendWsaBuf.len = static_cast<ULONG>(strlen(sendBuffer));
recvWsaBuf.buf = recvBuffer;
recvWsaBuf.len = DEFAULT_BUFLEN;
result = WSASend(connectSocket, &sendWsaBuf, 1, &sendBytes, flags, nullptr, nullptr);
if (result == SOCKET_ERROR) {
std::cout << "WSASend failed: " << WSAGetLastError() << std::endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
result = WSARecv(connectSocket, &recvWsaBuf, 1, &recvBytes, &flags, nullptr, nullptr);
if (result == SOCKET_ERROR) {
std::cout << "WSARecv failed: " << WSAGetLastError() << std::endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
std::cout << "Bytes sent: " << sendBytes << std::endl;
std::cout << "Bytes received: " << recvBytes << std::endl;
std::cout << "Server says: " << recvBuffer << std::endl;
closesocket(connectSocket);
WSACleanup();
return 0;
}
```
该示例连接到本地主机的默认端口(27015),并向服务器发送“Hello, server!”的消息。发送和接收操作都是异步的,因此在发送和接收完成之前,程序将不会阻塞。当发送和接收完成时,将输出发送和接收的字节数,并输出服务器发送的响应消息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)