利用Socket实现CAsynsocket类
时间: 2023-12-16 14:03:06 浏览: 66
CAsyncSocket的实现
3星 · 编辑精心推荐
CAsynSocket是一个异步socket类,用于实现基于Windows平台下的网络编程。它能够同时处理多个连接请求,并且能够实现非阻塞的网络通信,提高了程序的效率。
下面是利用Socket实现CAsynSocket类的示例代码:
```c++
// CAsynSocket.h 文件
#ifndef _CASYN_SOCKET_H_
#define _CASYN_SOCKET_H_
#include <winsock2.h>
#include <string>
class CAsynSocket
{
public:
CAsynSocket();
virtual ~CAsynSocket();
bool Create();
bool Bind(const std::string& address, const int port);
bool Listen(const int backlog = 5);
bool Accept(CAsynSocket& clientSocket);
bool Connect(const std::string& address, const int port);
bool Send(const char* data, const int length);
bool Receive(char* data, const int length);
void Close();
private:
SOCKET m_socket;
bool m_isCreated;
};
#endif
// CAsynSocket.cpp 文件
#include "CAsynSocket.h"
CAsynSocket::CAsynSocket()
{
m_socket = INVALID_SOCKET;
m_isCreated = false;
}
CAsynSocket::~CAsynSocket()
{
Close();
}
bool CAsynSocket::Create()
{
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
return false;
}
m_isCreated = true;
return true;
}
bool CAsynSocket::Bind(const std::string& address, const int port)
{
if (!m_isCreated)
{
return false;
}
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(address.c_str());
sockAddr.sin_port = htons(port);
int result = bind(m_socket, (sockaddr*)&sockAddr, sizeof(sockAddr));
if (result == SOCKET_ERROR)
{
return false;
}
return true;
}
bool CAsynSocket::Listen(const int backlog)
{
if (!m_isCreated)
{
return false;
}
int result = listen(m_socket, backlog);
if (result == SOCKET_ERROR)
{
return false;
}
return true;
}
bool CAsynSocket::Accept(CAsynSocket& clientSocket)
{
if (!m_isCreated)
{
return false;
}
sockaddr_in clientAddr;
int clientAddrSize = sizeof(clientAddr);
SOCKET clientSocketHandle = accept(m_socket, (sockaddr*)&clientAddr, &clientAddrSize);
if (clientSocketHandle == INVALID_SOCKET)
{
return false;
}
clientSocket.m_socket = clientSocketHandle;
clientSocket.m_isCreated = true;
return true;
}
bool CAsynSocket::Connect(const std::string& address, const int port)
{
if (!m_isCreated)
{
return false;
}
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(address.c_str());
sockAddr.sin_port = htons(port);
int result = connect(m_socket, (sockaddr*)&sockAddr, sizeof(sockAddr));
if (result == SOCKET_ERROR)
{
return false;
}
return true;
}
bool CAsynSocket::Send(const char* data, const int length)
{
if (!m_isCreated)
{
return false;
}
int result = send(m_socket, data, length, 0);
if (result == SOCKET_ERROR)
{
return false;
}
return true;
}
bool CAsynSocket::Receive(char* data, const int length)
{
if (!m_isCreated)
{
return false;
}
int result = recv(m_socket, data, length, 0);
if (result == SOCKET_ERROR)
{
return false;
}
return true;
}
void CAsynSocket::Close()
{
if (m_isCreated)
{
closesocket(m_socket);
m_isCreated = false;
}
}
```
以上代码实现了CAsynSocket类的基本功能,包括创建socket、绑定地址、监听端口、接受连接、连接远程主机、发送数据和接收数据等。可以根据需要进行扩展。
阅读全文