当前位置: 代码迷 >> 综合 >> CSocket
  详细解决方案

CSocket

热度:76   发布时间:2023-12-23 23:50:26.0

CSocket.h头文件

#pragma once#include<WinSock2.h>#define zjlUINT unsigned int
#define zjlINT  int
#define zjlPCHAR char*
#define zjlPCCHAR const char*
#define zjlPVOID  void*
#define zjlPCVOID const void*#define zjlNO         -1
#define zjlYES        0
#define zjlSOCK_FALSE 1    //创建套接字失败
#define zjlBIND_FALSE 2    //绑定端口失败
#define zjlCON_FALSE  3    //连接失败class Socket
{
private:SOCKET m_sock;
public:Socket();~Socket();//创建socket套接字 BOOL Create(zjlUINT nSocketPort = 0, zjlINT nSocketType = SOCK_STREAM,zjlPCHAR SocketAddress = NULL); BOOL Accept(Socket &sock, zjlPCHAR IP = NULL, zjlUINT *nPort = NULL);BOOL Connect(zjlPCHAR SocketAddress, zjlUINT nHostPort);zjlINT Listen(zjlINT nConnectBacklog = 5);//发送 zjlINT SendTo(zjlPCVOID Buff, zjlINT nBuffLen, zjlUINT nPort,zjlPCHAR HostAddress = NULL, int flag = 0);zjlINT Send(zjlPCVOID Buff, zjlINT nBuffLen, zjlINT flags = 0);//接收 zjlINT RecvFrom(zjlPCVOID Buff, zjlINT nBuffLen, zjlPCHAR SocketAddress,zjlUINT SocketPort, int nFlags = 0);zjlINT Recv(zjlPVOID Buff, zjlINT nLen, zjlINT flag = 0);BOOL GetPeerName(zjlPCHAR SocketAddress, zjlUINT &SocketPort);BOOL GetSocketName(zjlPCHAR SocketAddress, zjlUINT &SocketPort);void close();
};

CSocket.cpp文件

#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include "Socket.h"Socket::Socket()
{WSADATA data;WSAStartup(0x0202, &data);m_sock = INVALID_SOCKET;
}Socket::~Socket()
{close();
}BOOL Socket::Create(zjlUINT nSocketPort, zjlINT nSocketType, zjlPCHAR SocketAddress)
{m_sock = socket(AF_INET, nSocketType, 0);if (INVALID_SOCKET == m_sock)return zjlSOCK_FALSE;sockaddr_in sa = { AF_INET,htons(nSocketPort) };if (SocketAddress)sa.sin_addr.S_un.S_addr = inet_addr(SocketAddress);int b = bind(m_sock, (sockaddr*)&sa, sizeof(sa));if (SOCKET_ERROR == b)return zjlBIND_FALSE;return zjlYES;
}BOOL Socket::Accept(Socket & sock, zjlPCHAR IP, zjlUINT * nPort)
{sockaddr_in sa = { AF_INET };zjlINT nLen = sizeof(sa);sock.m_sock = accept(m_sock, (sockaddr*)&sa, &nLen);if (INVALID_SOCKET == sock.m_sock)return zjlSOCK_FALSE;if (IP)sa.sin_addr.S_un.S_addr = inet_addr(IP);if (nPort)*nPort = htons(sa.sin_port);return zjlYES;
}BOOL Socket::Connect(zjlPCHAR SocketAddress, zjlUINT nHostPort)
{sockaddr_in sa = { AF_INET,htons(nHostPort) };sa.sin_addr.S_un.S_addr = inet_addr(SocketAddress);int n = connect(m_sock, (sockaddr*)&sa, sizeof(sa));if (!n)return zjlCON_FALSE;return zjlYES;
}zjlINT Socket::Listen(zjlINT nConnectBacklog)
{return listen(m_sock, nConnectBacklog);
}zjlINT Socket::SendTo(zjlPCVOID Buff, zjlINT nBuffLen, zjlUINT nPort, zjlPCHAR HostAddress, int flag)
{sockaddr_in sa = { AF_INET };sendto(m_sock, (const char*)Buff, nBuffLen, 0, (sockaddr*)&sa, sizeof(sa));strcpy(HostAddress, inet_ntoa(sa.sin_addr));nPort = htons(sa.sin_port);return zjlYES;
}zjlINT Socket::RecvFrom(zjlPCVOID Buff, zjlINT nBuffLen, zjlPCHAR SocketAddress,zjlUINT SocketPort, int nFlags)
{sockaddr_in sa = { AF_INET };zjlINT nLen = sizeof(sa);int nResult = recvfrom(m_sock, (char*)Buff, nBuffLen, 0, (sockaddr*)&sa, &nLen);if (0 >= nResult)return nResult;SocketPort = htons(sa.sin_port);strcpy(SocketAddress, inet_ntoa(sa.sin_addr));return nResult;
}zjlINT Socket::Send(zjlPCVOID Buff, zjlINT nBuffLen, zjlINT flags)
{return send(m_sock, (zjlPCHAR)Buff, nBuffLen, flags);
}zjlINT Socket::Recv(zjlPVOID Buff, zjlINT nLen, zjlINT flag)
{return recv(m_sock, (zjlPCHAR)Buff, nLen, flag);
}BOOL Socket::GetPeerName(zjlPCHAR SocketAddress, zjlUINT & SocketPort)
{sockaddr_in sa = { AF_INET };zjlINT nLen = sizeof(sa);if (0 > getpeername(m_sock, (sockaddr*)&sa, &nLen))return zjlNO;SocketPort = htons(sa.sin_port);strcpy(SocketAddress, inet_ntoa(sa.sin_addr));return zjlYES;
}BOOL Socket::GetSocketName(zjlPCHAR SocketAddress, zjlUINT & SocketPort)
{sockaddr_in sa = { AF_INET };zjlINT nLen = sizeof(sa);if (0 > getsockname(m_sock, (sockaddr*)&sa, &nLen))return zjlNO;SocketPort = htons(sa.sin_port);strcpy(SocketAddress, inet_ntoa(sa.sin_addr));return zjlYES;
}void Socket::close()
{closesocket(m_sock);m_sock = INVALID_SOCKET;
}

 

  相关解决方案