//************************************************************************************************************ // Filename:WLSocket.cpp
// By Luo Jiafeng
// Description:this class disign to communicate by socket between network processes on both Linux and Windows
// Date:April 29,2008
// Copyright:
// ************************************************************************************************************
#include "WLSocket.h"
// ************************************************************************
// Function:CWLSocket
// Description:Which is default constructor funcation
// Return values:None // ************************************************************************
CWLSocket::CWLSocket(void) : m_socket(INVALID_SOCKET)
{
#ifdef WIN32
WSAData wsaData;
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
#endif
}
//************************************************************************
// Function:CWLSocket
// Description:Which is over loading constructor funcation
// Return values:None
// ************************************************************************
CWLSocket::CWLSocket(SOCKET clientSocket) : m_socket(clientSocket)
{
}
//*
************************************************************************
// Function:~CWLSocket
// Description:Which is Deconstructor funcation
// Return values:None
// *************************************************************************
CWLSocket::~CWLSocket(void)
{
#ifdef WIN32
closesocket(m_socket);
m_socket = INVALID_SOCKET;
#else
close(m_socket);
m_socket = INVALID_SOCKET;
#endif
}
// *************************************************************************
// Function:Socket
// Description:To create a socket
// Parameters:
// af:[in] Address family specification
// type: [in] Type for specification the new socket
// protocol: [in] Protocol to be used with the socket that is specific to the indicated address family
// Return values:socket descriptor if OK, -1 on error
// **************************************************************************
SOCKET CWLSocket::Socket( int af /* = AF_INET */, int type /* = SOCK_STREAM */, int protocol /* = 0 */ )
{
m_socket = socket(af, type, protocol);
return m_socket;
}
// **************************************************************************
//
Function:Bind
//
Description:the function associates a local address with a socket
// Parameters:
// szAddr: [in]Null-terminated character string representing a number expressed in the Internet standard "."(dotted) notation
// nPort: [in] IP port
// Return values:Zero if OK, -1 on error
//
**************************************************************************
int CWLSocket::Bind(const char* szAddr, int nPort)
{
#ifdef WIN32
assert( m_socket != INVALID_SOCKET );
#else
assert( m_socket != INVALID_SOCKET );
#endif
sockaddr_in socketaddr;
memset(&socketaddr, '\0', sizeof(sockaddr_in));
socketaddr.sin_family = AF_INET;
socketaddr.sin_port = nPort;
socketaddr.sin_addr.s_addr = inet_addr(szAddr);
return bind(m_socket, (struct sockaddr *)&socketaddr, sizeof(struct sockaddr));
}
//
**************************************************************************
// Function:Connect
// Description:the Function connect to a server
// Parameters:
// szAddr:[in]Null-terminated character string representing a number expressed in the Internet standard "."(dotted) notation
// nPort:[in]IP port
// nReconnectTimes:the times of reconnect
// Returns:zero if OK, otherwise, the value SOCKET_ERROR is returned
//
**************************************************************************
int CWLSocket::Connect(const char* szAddr, int nPort, int nReconnectTimes /* = 1 */)
{
Socket(AF_INET, SOCK_STREAM, 0);
assert( m_socket != INVALID_SOCKET );
int nReturn = SOCKET_ERROR;
int nConnectTimes = 0;
sockaddr_in socketaddr;
memset(&socketaddr, '\0', sizeof(sockaddr_in));
socketaddr.sin_family = AF_INET;
socketaddr.sin_port = nPort;
socketaddr.sin_addr.s_addr = inet_addr(szAddr);
while (nConnectTimes < nReconnectTimes && SOCKET_ERROR == nReturn)
{
nReturn = connect(m_socket, (struct sockaddr *)&socketaddr, sizeof(struct sockaddr));
if (nReturn != SOCKET_ERROR)
{
return nReturn;
}
else
nConnectTimes++;
}
return SOCKET_ERROR;
}
//*
**************************************************************************
// Function: Listen
// Description:the function places data member m_socket a state where it is listening for an incoming connection.
// Parameters:
// szAddr:[in] Null-terminated character string representing a number expressed in the Internet standard "."(dotted) notation
// nPort:[in] IP port
// backlog:[in] Maximum length of the queue of pending connections
// Return values:If no error occurs, Listen returns zero. Otherwise, a value of SOCKET_ERROR is returned
// *
**************************************************************************
int CWLSocket::Listen(const char* szAddr, int nPort, int backlog)
{
Socket(AF_INET, SOCK_STREAM, 0);
Bind(szAddr, nPort);
return listen(m_socket, backlog);
}
//
*
************************************************************************** *
// Funcation:Accept
// Description:the funcation permits an incomming connection attempt on a socket(the data member "m_socket" )
// Parameters:
// addr:[out]Optional pointer to a buffer that receives the address of the connection entity
// addrlen:[out]Optional pointer to an integer that contains the length of the parameter addr
// Return values: If no error occurs, the function returns a value of type SOCKET that is a descriptor for the new socket.
// Otherwise, a value of INVALID_SOCKET is returned.
// *
************************************************************************** *
SOCKET CWLSocket::Accept(struct sockaddr* addr, int* addrlen)
{
#ifdef WIN32
return accept(m_socket, addr, addrlen);
#else
return accept(m_socket, addr, (socklen_t *)addrlen);
#endif
}
//*
*
************************************************************************** *
// Function: Send
// Description:the funcation sends data on a connected socket(the data member m_socket)
// Parameters:
// szBuf:[in] Buffer contained the data to be transmitted
// nLen:[in] Lengh of the data in szBuf
// flags:[in] Indicator specifying the way in which the call is made
// Return values: If no error occurs, send returns the total number of bytes sent, which can be less than the number indicated by len.
// Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling
// * *
************************************************************************** *
int CWLSocket::Send(const char* szBuf, int nLen, int flags/* = 0*/)
{
int nSendLen = 0;
while (nSendLen < nLen)
{
int nCount = send(m_socket, szBuf + nSendLen, nLen - nSendLen, flags);
if (nCount <= 0)
{
return SOCKET_ERROR;
}
else
{
nSendLen += nCount;
}
}
return nSendLen;
}
//*
* *
************************************************************************** *
// Function: Recv
// Description:the function receive data from a client or a server
// Parameters:
// szBuf:[out] Buffer for the incoming data
// nLen:[in] Lengh of the szBuf
// flags:[in] Flag specifying the way in which the call is made
// Return values:If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed,
// the return value is zero. Otherwise, a value of SOCKET_ERROR is returned
// * * *
************************************************************************** *
int CWLSocket::Recv(char* szBuf, int nLen, int flags/* = 0*/ )
{
int nRecvLen = 0;
while (nRecvLen < nLen)
{
char *pszBuf = new char[nLen];
memset(pszBuf, '\0', nLen);
int nCount = recv(m_socket, pszBuf + nRecvLen, nLen - nRecvLen, flags);
if (nCount <= 0)
{
delete pszBuf;
pszBuf = NULL;
return SOCKET_ERROR;
}
else
{
memcpy(szBuf + nRecvLen, pszBuf, nCount);//可能有问题
nRecvLen += nCount;
delete pszBuf;
pszBuf = NULL;
}
}
return nRecvLen;
}
|