Code:
#if defined(_WIN32)
#include <windows.h>
#include <winsock.h>
#define socket_connect connect
#define socket_close closesocket
#define socket_ioctl ioctlsocket
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl>
typedef SOCKET int
#define INVALID_SOCKET -1
#define socket_close close
#define socket_ioctl ioctl
#define socket_connect(SOCKET s, sockaddr_in* name, namelen) connect(s, (const sockaddr*) name, sizeof(sockaddr) )
#endif
class Socket
{
private:
SOCKET m_socket;
bool IsOpened;
private:
void setIsOpened(bool bVal)
{
this->IsOpened = bVal;
}
bool getIsOpened(void)
{
return this->IsOpened;
}
void setSocket(SOCKET s)
{
this->m_socket = s;
}
SOCKET getSocket(void)
{
return this->m_socket;
}
public:
Socket(void)
{
this->setIsOpened(false);
}
virtual ~Socket(void)
{
this->Close();
}
bool Open(int af, int type, int protocol)
{
if(this->getIsOpened())
return true;
SOCKET tempSock = socket(af, type, protocol);
if(tempSock == INVALID_SOCKET)
return false;
else
{
this->setIsOpened(true);
this->setSocket(tempSock);
return true;
}
}
bool Open(SOCKET tempSock)
{
if(this->getIsOpened())
return true;
if(tempSock == INVALID_SOCKET)
return false;
else
{
this->setIsOpened(true);
this->setSocket(tempSock);
return true;
}
}
bool Close(void)
{
this->Shutdown(0x02);
if( socket_close(this->getSocket()) == 0 ? true : false )
{
this->setIsOpened(false);
return true;
}
else return false;
}
bool Bind(const sockaddr* name, int namelen)
{
return bind(this->getSocket(), name, namelen) == 0 ? true : false;
}
bool Connect(const sockaddr* name, int namelen)
{
return socket_connect(this->getSocket(), name, namelen) == 0 ? true : false;
}
bool Connect(const char* host, int port, int af)
{
hostent* he;
if( (he = gethostbyname(host)) == 0 )
return false;
sockaddr_in clientService;
clientService.sin_family = af;
clientService.sin_addr.s_addr = inet_addr( inet_ntoa( (**(in_addr**)he->h_addr_list ) ) );
clientService.sin_port = htons(port);
return socket_connect(this->getSocket(), (SOCKADDR*) &clientService, sizeof(clientService) ) == 0 ? true : false;
}
bool Connect(std::string host, int port, int af)
{
return this->Connect(host.c_str(), port, af);
}
bool Shutdown(int how)
{
shutdown(this->getSocket(), how);
}
bool Receive(char* &buffer, int &receivedBytes, int flags)
{
receivedBytes = recv(this->getSocket(), buffer, (int)strlen(buffer), flags);
return receivedBytes > 0 ? true : false;
}
bool Receive(std::string &buffer, int &receivedBytes, int flags)
{
char* cBuffer;
receivedBytes = recv(this->getSocket(), cBuffer, (int)strlen(cBuffer), flags);
buffer += cBuffer;
buffer = buffer.substr(0,receivedBytes);
return receivedBytes > 0 ? true : false;
}
bool Send(char* &buffer, int &sentBytes, int flags)
{
sentBytes = send(this->getSocket(), buffer, (int)strlen(buffer), flags);
return sentBytes > 0 ? true : false;
}
bool Send(std::string &buffer, int &sentBytes, int flags)
{
char* cBuffer = (char*)buffer.c_str();
sentBytes = send(this->getSocket(), cBuffer, (int)strlen(cBuffer), flags);
return sentBytes > 0 ? true : false;
}
bool Listen(int backlog)
{
return listen(this->getSocket(), backlog) == 0 ? true : false;
}
Socket* Accept(sockaddr* addr, int* addrlen)
{
Socket* tempSock = new Socket();
tempSock->Open( accept(this->getSocket(), addr, addrlen) );
return tempSock;
}
bool getPeerName(sockaddr* name, int* namelen)
{
return getpeername(this->getSocket(), name, namelen) == 0 ? true : false;
}
bool getName(sockaddr* name, int* namelen)
{
return getsockname(this->getSocket(), name, namelen) == 0 ? true : false;
}
bool getOption(int level, int optionName, char* optionValue)
{
return getsockopt(this->getSocket(), level, optionName, optionValue, (int*)strlen(optionValue) ) == 0 ? true : false;
}
bool setOption(int level, int optionName, const char* optionValue)
{
return setsockopt(this->getSocket(), level, optionName, optionValue, (int)strlen(optionValue) ) == 0 ? true : false;
}
bool ioctl(long cmd, unsigned long* argp)
{
socket_ioctl(this->getSocket(), cmd, argp);
}
};
So , what do you think about it?