#ifndef __csocket_h
#define __csocket_h
#include
"stdio.h"
#include
"Winsock2.h"
Class Csocket
{
PUBLIC:
Csocket
Char * szremoteaddr,
INT iPort);
Csocket
INT iPort);
CSocket (socket socket);
~ Csocket ();
Csocket * accept
Void);
Void Close
Void);
Int Read
Void * PDATA,
unsigned
INT ILEN);
Int write
Void * PDATA,
unsigned
INT ILEN);
Private:
Socket m_socket;
WSADATA M_WSADATA;
SockAddr_in m_sockaddr;
Void Reset
unsigned
INT iPort);
}
Class CsocketException
{
PUBLIC:
CsocketException
Char * sztext)
{
STRCPY (m_sztext, sztext);
}
~ Csocketexception () {};
Char * getText () {
Return (m_sztext);
Private:
Char m_sztext [128];
}
#ENDIF
// __ csocket_h
/ ** * @Param iport local listenning port * @throws csocketexception is Server Socket Could Not Be created * /
CSocket :: CSocket
INT iPort
{
RESET (iPort);
IF (bind (m_socket, (sockaddr *) & m_sockaddr,
SizeOf (SockAddr))! = 0)
Throw CsocketException
Bind () failed ");
IF (listen (m_socket, 0)! = 0)
Throw CsocketException
"accept () failed");
}
/ ** * @Param Szremoteaddr Remote Machine Address * @Param IremotEport Server Listenning Port * @Throws Csocketexception if Client Socket Could Not Be created * /
CSocket :: CSocket
Char * szremoteaddr,
INT iPort
{
IF (! szremoteaddr)
Throw CsocketException
"Invalid parameters");
RESET (iPort);
// first guess => Try to resolve it as ip @
M_SockAddr.sin_addr.s_addr = inet_addr (szremoteaddr);
IF (m_sockaddr.sin_addr.s_addr == inaddr_none)
{
// screwed => Try to resolve it as name
LPHOSTENT LPHOST = gethostbyName (SzremoteAddr);
IF (! lphost)
Throw CsocketException ("Unable to Solve this Address);
M_SockAddr.sin_addr.s_addr = ** (
INT **) (lphost-> h_addr_list);
}
// Actually Performs Connection
IF (Connect (m_socket, (sockaddr *) & m_sockaddr,
SizeOf (SockAddr))! = 0)
Throw CsocketException
"Connect () failed");
}
/ ** * Create a socket for data transfert * @Param Socket The socket descriptor for this new object * /
CSocket :: CSocket (Socket Socket)
{
m_socket = socket;
}
/ ** * deStructor * /
Csocket :: ~ csocket ()
{
CLOSE ();
}
/ ** * Wait for incomming connections on server socket * @return csocket new data socket for this incoving client. Can be null is anything wrong * /
Csocket * csocket :: acception ()
{
Int nlen =
SIZEOF (SOCKADDR);
Socket Socket = Accept (m_socket, (sockaddr *) & m_sockaddr, & nlen;
IF (socket == -1)
Return (NULL);
Return
New Csocket (Socket);
}
/ ** * Close Current Socket * /
Void Csocket :: Close ()
{
IF (m_socket! = invalid_socket)
CloseSocket (M_Socket);
}
/ ** * Read data available in socket or waits for incomming informations * @param pData Buffer where informations will be stored * @param iLen Max length of incomming data * @return Number of bytes read or -1 if anything went wrong * /
Int csocket :: read
Void * PDATA,
unsigned
Ilen
{
IF (! pData ||! inglen)
Return (-1);
Return (RECV (M_Socket,
CHAR *) PDATA, ILEN, 0);
}
/ ** * Initialisation Common to all conditionrs * /
Void Csocket :: RESET (
unsigned
INT iPort
{
// Initialize Winsock
IF (WSAStartup (Makeword (2,0), & m_wsadata)! = 0)
Throw CsocketException
"Wsastartup () failed");
// Actually create the socketm_socket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
IF (m_socket == invalid_socket)
Throw CsocketException
"Socket () failed");
// SockAddr Initialision
MEMSET (& M_SockAddr, 0,
SizeOf (SockAddr));
m_sockaddr.sin_family = af_INet;
m_sockaddr.sin_port = htons (iPort);
m_sockaddr.sin_addr.s_addr = incdr_any;
}
/ ** * @Param pdata buffer to be Sent * @Param isize number of service @return the number of service @return the number of service @Return the number of service
Int Csocket :: Write
Void * PDATA,
unsigned
Int isize)
{
IF (! pdata ||! isize)
Return (-1);
Return ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((5)
INT) Send (m_socket, (lpcstr) PDATA, ISIZE, 0));
}