Beispiel #1
0
void CSocket::Create()
  {
  m_nSocket = ::socket(AF_INET,
                     SOCK_STREAM,
                     0);
  if ( !isValid() )
    {
    std::cout << "errno == " << errno << "  in CSocket::Receive\n";
    throw CSocketException( "Could not create socket in CSocket::Create()." );
    }

  int on = 1;
  int nResult = ::setsockopt( m_nSocket,
                              SOL_SOCKET,
                              SO_REUSEADDR,
                              (void*)&on,
                              sizeof(on));
  if ( nResult == -1 )
    {
    throw CSocketException( "Could not 'setsockopt' SO_REUSEADDR in CSocket::Create()." );
    }
/*
  int off = 0;
  nResult = ::setsockopt( m_nSocket,
                          IPPROTO_TCP,
                          TCP_NODELAY,
                          (void*) &off,
                          sizeof(on) );
  if ( nResult == -1 )
    {
    throw CSocketException( "Could not 'setsockopt' TCP_NODELAY in CSocket::Create()." );
    }
*/
  } // void CSocket::Create()
Beispiel #2
0
CSocket::CSocket( const CSocket& src )
  {
  if ( !src.isValid() )
    {
    throw CSocketException( "Source Socket is invalid, so new socket can't be better" );
    }

  socklen_t addr_length = m_otAddr.SizeGet();
  m_nSocket      = ::accept( src.m_nSocket,
                             src.m_otAddr,
                             &addr_length );
  if ( m_nSocket <= 0 )
    {
    throw CSocketException( "Could not 'accept', new socket is invalid" );
    }

  timeval timeout;
  timeout.tv_sec  = 0;
  timeout.tv_usec = 1000; // looks like this value is ignored
  int nResult = ::setsockopt( m_nSocket,
                              SOL_SOCKET,
                              SO_RCVTIMEO,
                              (const char*)&timeout,
                              sizeof(timeout) );
  if ( nResult == -1 )
    {
    throw CSocketException( "Could not set SO_RCVTIMEO, new socket is invalid" );
    } // if ( result == -1 )

  } // CSocket::CSocket( const CSocket& src )
Beispiel #3
0
/**
 * Initialisation common to all constructors
 */
void CSocket::Reset( unsigned int iPort)
{
    // Initialize winsock
    if( WSAStartup( MAKEWORD(2,0), &m_WSAData) != 0)
        throw CSocketException( "WSAStartup() failed");

    // Actually create the socket
    m_Socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( m_Socket == INVALID_SOCKET)
        throw CSocketException( "socket() failed");

    // *** ADDED SO THAT WE CAN COMMUNICATE WITH LINUX - Ed ***
    int val=1;
    int n=setsockopt(m_Socket, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
    if(n==SOCKET_ERROR)
    {
        val = WSAGetLastError();
        printf("socket error: %i\n",val);
    }

    // sockaddr initialisation
    memset( &m_sockaddr, 0, sizeof( sockaddr));

    m_sockaddr.sin_family = AF_INET;
    m_sockaddr.sin_port = htons( iPort);
    m_sockaddr.sin_addr.s_addr = INADDR_ANY;
}
Beispiel #4
0
size_t CSocket::Send(const void* pBuffer, size_t nBufSize)
{
	ASSERT(pBuffer != nullptr);

	// Ignore, if nothing to send.
	if (nBufSize == 0)
		return 0;

	// Socket closed?
	if (m_hSocket == INVALID_SOCKET)
		throw CSocketException(CSocketException::E_SEND_FAILED, WSAENOTCONN);

	int nResult = 0;

	// Blocking socket?
	if (m_eMode == BLOCK)
	{
		// Send the entire buffer.
		nResult = send(m_hSocket, static_cast<const char*>(pBuffer), static_cast<int>(nBufSize), 0);

		if (nResult == SOCKET_ERROR)
			throw CSocketException(CSocketException::E_SEND_FAILED, CWinSock::LastError());

		ASSERT(static_cast<size_t>(nResult) == nBufSize);
	}
	// Async socket.
	else // (eMode == ASYNC)
	{
		// Allocate send buffer, on first call.
		if (m_pSendBuffer.get() == nullptr)
			m_pSendBuffer = NetBufferPtr(new CNetBuffer());

		// Append new data to existing send data.
		if (m_pSendBuffer->Append(pBuffer, nBufSize) > 0)
		{
			// Try and send the entire buffer.
			nResult = send(m_hSocket, static_cast<const char*>(m_pSendBuffer->Ptr()), static_cast<int>(m_pSendBuffer->Size()), 0);

			if (nResult != SOCKET_ERROR)
			{
				// Remove amount sent.
				m_pSendBuffer->Discard(nResult);
			}
			else // (nResult == SOCKET_ERROR)
			{
				int nLastErr = CWinSock::LastError();

				// Only an error, if not because of lack of buffer space.
				if (nLastErr != WSAEWOULDBLOCK)
					throw CSocketException(CSocketException::E_SEND_FAILED, nLastErr);
			}
		}
	}

	return nResult;
}
Beispiel #5
0
/**
 * @param iPort local listenning port
 * @throws CSocketException is server socket could not be created
 */
CSocket::CSocket( int iPort)
{
    Reset( iPort);

    //printf("constructor\n");

    if( bind( m_Socket, ( SOCKADDR *)&m_sockaddr, sizeof( sockaddr)) != 0)
        throw CSocketException( "bind() failed");

    if( listen( m_Socket, 0) != 0)
        throw CSocketException( "accept() failed");
}
Beispiel #6
0
void CSocket::Listen() const
  {
  if ( !isValid() )
    {
    throw CSocketException( "Socket is invalid in CSocket::Listen()." );
    }

  int result = ::listen( m_nSocket,
                         CLIENT_BACKLOG );
  if ( result == -1 )
    {
    throw CSocketException( "Could not listen to socket in CSocket::Listen()." );
    }
  } // void CSocket::Listen() const
Beispiel #7
0
void CSocket::SetReadBufferSize( unsigned int nSize ) throw(CSocketException)
{
    if (setsockopt(m_sockDesc, SOL_SOCKET, SO_RCVBUF, &nSize, sizeof(nSize)) == -1) 
    {
        throw CSocketException("Error in setting socket buffer size ", true);
    }
}
Beispiel #8
0
size_t CSocket::Peek(void* pBuffer, size_t nBufSize)
{
	ASSERT(m_hSocket != INVALID_SOCKET);
	ASSERT(pBuffer   != nullptr);

	int nResult = 0;

	// Blocking socket?
	if (m_eMode == BLOCK)
	{
		nResult = recv(m_hSocket, static_cast<char*>(pBuffer), static_cast<int>(nBufSize), MSG_PEEK);

		if (nResult == SOCKET_ERROR)
			throw CSocketException(CSocketException::E_PEEK_FAILED, CWinSock::LastError());
	}
	// Async socket.
	else // (eMode == ASYNC)
	{
		if (m_pRecvBuffer.get() != nullptr)
		{
			nBufSize = std::min(nBufSize, m_pRecvBuffer->Size());

			if (nBufSize != 0)
				memcpy(pBuffer, m_pRecvBuffer->Ptr(), nBufSize);

			nResult = static_cast<int>(nBufSize);
		}
	}

	return nResult;
}
Beispiel #9
0
CSocket::CSocket( SocketType type, NetworkLayerProtocol protocol ) throw(CSocketException):m_sockDesc(-1)
{
    m_sockDesc = socket(protocol, type, 0);
    if (m_sockDesc < 0)
    {
        throw CSocketException("Socket creation failed (socket())", true);
    }
}
Beispiel #10
0
unsigned short CSocket::GetLocalPort() throw(CSocketException)
{
    sockaddr_in addr;
    unsigned int addr_len = sizeof(addr);
 
    if (getsockname(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
        throw CSocketException("Fetch of local port failed (getsockname())", true);
    }
    return ntohs(addr.sin_port);
}
Beispiel #11
0
std::string CSocket::GetLocalAddress() throw(CSocketException)
{
    sockaddr_in addr;
    unsigned int addr_len = sizeof(addr);
 
    if (getsockname(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
        throw CSocketException("Fetch of local address failed (getsockname())", true);
    }
    return inet_ntoa(addr.sin_addr);
}
Beispiel #12
0
int CSocket::Recv( void *buffer, int bufferLen ) throw(CSocketException)
{
    int nBytes;
    if ((nBytes = ::recv(m_sockDesc, (void *) buffer, bufferLen, 0)) < 0) {
        throw CSocketException("Received failed (recv())", true);
    }
    char* sData = static_cast<char *>(buffer);
    sData[nBytes] = '\0';
    return nBytes;
}
Beispiel #13
0
void CSocket::Bind ( const int nPort )
  {
  if ( !isValid() )
    {
    throw CSocketException( "Socket is invalid in CSocket::Bind()." );
    }
  
  m_otAddr.sin_family      = AF_INET;
  m_otAddr.sin_addr.s_addr = INADDR_ANY; // inet_addr("127.0.0.1"); // INADDR_ANY;
  m_otAddr.sin_port        = htons ( nPort );
  
  int result = ::bind( m_nSocket,
                       m_otAddr,
                       m_otAddr.SizeGet() );
  if ( result == -1 )
    {
    throw CSocketException( "Could not bind to port in CSocket::Bind." );
    }
  } // void CSocket::Bind ( const int nPort )
Beispiel #14
0
void CSocket::Create(int nAF, int nType, int nProtocol)
{
	ASSERT(m_hSocket == INVALID_SOCKET);

	// Allocate the socket.
	m_hSocket = socket(nAF, nType, nProtocol);

	if (m_hSocket == INVALID_SOCKET)
		throw CSocketException(CSocketException::E_CREATE_FAILED, CWinSock::LastError());
}
Beispiel #15
0
void CSocket::Close( )
  {
  if ( !isValid() ) return;

  if ( ::close( m_nSocket ) == -1 )
    {
    throw CSocketException( "Could not 'close' in CSocket::Close()." );
    }

  m_nSocket = INVALID_SOCKET;
  } // void CSocket::Close( )
Beispiel #16
0
void CSocket::BindLocalAddressAndPort( const string &localAddress, unsigned short localPort /*= 0*/ )
    throw(CSocketException)
{
    // Get the address of the requested host
    sockaddr_in localAddr;
    FillAddr(localAddress, localPort, localAddr);
 
    if (bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
        throw CSocketException("Set of local address and port failed (bind())", true);
    }
}
Beispiel #17
0
size_t CSocket::Available()
{
	ASSERT(m_hSocket != INVALID_SOCKET);

	u_long lAvailable = 0;

	// Blocking socket?
	if (m_eMode == BLOCK)
	{
		fd_set  aoSockets;
		TIMEVAL oWaitTime  = { 0 };

		FD_ZERO(&aoSockets);
		FD_SET(m_hSocket, &aoSockets);

		// Check for readability on the socket.
		if (select(1, &aoSockets, nullptr, nullptr, &oWaitTime) == SOCKET_ERROR)
			throw CSocketException(CSocketException::E_SELECT_FAILED, CWinSock::LastError());

		// Anything to read?
		if (FD_ISSET(m_hSocket, &aoSockets))
		{
			// How much data is available?
			int nResult = ::ioctlsocket(m_hSocket, FIONREAD, &lAvailable);

			if (nResult == SOCKET_ERROR)
				throw CSocketException(CSocketException::E_PEEK_FAILED, CWinSock::LastError());

			if (lAvailable == 0)
				throw CSocketException(CSocketException::E_DISCONNECTED, WSAESHUTDOWN);
		}
	}
	// Async socket.
	else // (eMode == ASYNC)
	{
		if (m_pRecvBuffer.get() != nullptr)
			lAvailable = static_cast<u_long>(m_pRecvBuffer->Size());
	}

	return lAvailable;
}
Beispiel #18
0
/**
 * @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");
}
Beispiel #19
0
void CSocket::Connect(const tchar* pszHost, uint nPort)
{
	ASSERT(m_hSocket == INVALID_SOCKET);
	ASSERT(pszHost   != nullptr);
	ASSERT(nPort     <= USHRT_MAX);

	// Save parameters.
	m_strHost = pszHost;
	m_nPort   = nPort;

	sockaddr_in	addr = { 0 };

	addr.sin_family      = AF_INET;
	addr.sin_addr.s_addr = inet_addr(T2A(pszHost));
	addr.sin_port        = htons(static_cast<ushort>(nPort));

	// Host name isn't an IP Address?
	if (addr.sin_addr.s_addr == INADDR_NONE)
	{
		// Resolve host name.
		hostent* pHost = gethostbyname(T2A(pszHost));

		if (pHost == nullptr)
			throw CSocketException(CSocketException::E_RESOLVE_FAILED, CWinSock::LastError());

		memcpy(&addr.sin_addr, pHost->h_addr_list[0], pHost->h_length);
	}

	// Create the socket.
	Create(AF_INET, Type(), Protocol());

	// Connect to host.
	if (connect(m_hSocket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == SOCKET_ERROR)
		throw CSocketException(CSocketException::E_CONNECT_FAILED, CWinSock::LastError());

	// If async mode, do select.
	if (m_eMode == ASYNC)
		CWinSock::BeginAsyncSelect(this, (FD_READ | FD_WRITE | FD_CLOSE));
}
Beispiel #20
0
void CSocket::BindLocalPort( unsigned short localPort ) throw(CSocketException)
{
    // Bind the socket to its port
    sockaddr_in localAddr;
    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(localPort);
 
    if (bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
        throw CSocketException("Set of local port failed (bind())", true);
    }
}
Beispiel #21
0
size_t CSocket::Recv(void* pBuffer, size_t nBufSize)
{
	ASSERT(pBuffer  != nullptr);

	// Socket closed?
	if (m_hSocket == INVALID_SOCKET)
		throw CSocketException(CSocketException::E_RECV_FAILED, WSAENOTCONN);

	int nResult = 0;

	// Blocking socket?
	if (m_eMode == BLOCK)
	{
		nResult = recv(m_hSocket, static_cast<char*>(pBuffer), static_cast<int>(nBufSize), 0);

		if (nResult == SOCKET_ERROR)
			throw CSocketException(CSocketException::E_RECV_FAILED, CWinSock::LastError());
	}
	// Async socket.
	else // (eMode == ASYNC)
	{
		if (m_pRecvBuffer.get() != nullptr)
		{
			nBufSize = std::min(nBufSize, m_pRecvBuffer->Size());

			if (nBufSize != 0)
			{
				memcpy(pBuffer, m_pRecvBuffer->Ptr(), nBufSize);

				m_pRecvBuffer->Discard(nBufSize);
			}

			nResult = static_cast<int>(nBufSize);
		}
	}

	return nResult;
}
Beispiel #22
0
void CSocket::ConnectToHost( const string &foreignAddress, unsigned short foreignPort ) throw(CSocketException)
{
    //cout<<"\nstart Connect to host";
    // Get the address of the requested host
    sockaddr_in destAddr;
    //cout<<"\ninside Connect to host";
    FillAddr(foreignAddress, foreignPort, destAddr);
 
    //cout<<"trying to connect to host";
    // Try to connect to the given port
    if (::connect(m_sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
        throw CSocketException("Connect failed (connect())", true);
    }
    //cout<<"\n after connecting";
 
}
Beispiel #23
0
void CSocket::FillAddr( const string & localAddress, unsigned short localPort, sockaddr_in& localAddr )
{
    ////cout<<"\n Inside Fille addr:"<<localAddress <<" port:"<<localPort;
    memset(&localAddr, 0, sizeof(localAddr));  // Zero out address structure
    localAddr.sin_family = AF_INET;       // Internet address
 
    hostent *host;  // Resolve name
    if ((host = gethostbyname(localAddress.c_str())) == NULL) {
        // strerror() will not work for gethostbyname() and hstrerror() 
        // is supposedly obsolete
        throw CSocketException("Failed to resolve name (gethostbyname())");
    }
    localAddr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
 
    localAddr.sin_port = htons(localPort);     // Assign port in network byte order
    ////cout<<"\n returning from  Fille addr";
}
Beispiel #24
0
CString CSocket::ResolveStr(const tchar* pszHost)
{
	// Is already an IP address?
	if (IsAddress(pszHost))
		return pszHost;

	in_addr addr;

	// Resolve host name.
	hostent* pHost = gethostbyname(T2A(pszHost));

	if (pHost == nullptr)
		throw CSocketException(CSocketException::E_RESOLVE_FAILED, CWinSock::LastError());

	memcpy(&addr, pHost->h_addr_list[0], pHost->h_length);

	return A2T(inet_ntoa(addr));
}
Beispiel #25
0
in_addr CSocket::Resolve(const tchar* pszHost)
{
	in_addr addr;

	// Is already an IP address?
	addr.s_addr = inet_addr(T2A(pszHost));

	if (addr.s_addr == INADDR_NONE)
	{
		// Resolve host name.
		hostent* pHost = gethostbyname(T2A(pszHost));

		if (pHost == nullptr)
			throw CSocketException(CSocketException::E_RESOLVE_FAILED, CWinSock::LastError());

		memcpy(&addr, pHost->h_addr_list[0], pHost->h_length);
	}

	return addr;
}
Beispiel #26
0
void CSocket::Send( const void *buffer, int bufferLen ) throw(CSocketException)
{
    if (::send(m_sockDesc, (void *) buffer, bufferLen, 0) < 0) {
        throw CSocketException("Send failed (send())", true);
    }
}