示例#1
0
//------------------------------------------------------------------------------
//
// Close() - Close socket and free up any memory allocated for the socket
//
//------------------------------------------------------------------------------
bool CSimpleSocket::Close(void)
{
	bool bRetVal = false;

	//--------------------------------------------------------------------------
	// delete internal buffer
	//--------------------------------------------------------------------------
	if (m_pBuffer != NULL)
	{
		delete [] m_pBuffer;
		m_pBuffer = NULL;
	}

	//--------------------------------------------------------------------------
	// if socket handle is currently valid, close and then invalidate
	//--------------------------------------------------------------------------
	if (IsSocketValid())
	{
		if (CLOSE(m_socket) != CSimpleSocket::SocketError)
		{
			m_socket = INVALID_SOCKET;
			bRetVal = true;
		}
	}

	TranslateSocketError();

	return bRetVal;
}
示例#2
0
//------------------------------------------------------------------------------
//
// Send() - Send data on a valid socket
//
//------------------------------------------------------------------------------
int32 CPassiveSocket::Send(const uint8 *pBuf, size_t bytesToSend)
{
    SetSocketError(SocketSuccess);
    m_nBytesSent = 0;

    switch(m_nSocketType)
    {
       case CSimpleSocket::SocketTypeUdp:
        {
            if (IsSocketValid())
            {
                if ((bytesToSend > 0) && (pBuf != NULL))
                {
                    m_timer.Initialize();
                    m_timer.SetStartTime();

                    m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stClientSockaddr, sizeof(m_stClientSockaddr));

                    m_timer.SetEndTime();

                    if (m_nBytesSent == CSimpleSocket::SocketError)
                    {
                        TranslateSocketError();
                    }
                }
            }
            break;
        }
       default:
           CSimpleSocket::Send(pBuf, bytesToSend);
           break;
    }

    return m_nBytesSent;
}
示例#3
0
//------------------------------------------------------------------------------
//
// Open() - Create a connection to a specified address on a specified port
//
//------------------------------------------------------------------------------
bool CActiveSocket::Open(const char *pAddr, uint16 nPort)
{
    bool bRetVal = false;

    if (IsSocketValid() == false)
    {
        SetSocketError(CSimpleSocket::SocketInvalidSocket);
        return bRetVal;
    }

    if (pAddr == NULL)
    {
        SetSocketError(CSimpleSocket::SocketInvalidAddress);
        return bRetVal;
    }

    if (nPort == 0)
    {
        SetSocketError(CSimpleSocket::SocketInvalidPort);
        return bRetVal;
    }

    switch (m_nSocketType)
    {
    case CSimpleSocket::SocketTypeTcp :
    {
        bRetVal = ConnectTCP(pAddr, nPort);
        break;
    }
    case CSimpleSocket::SocketTypeUdp :
    {
        bRetVal = ConnectUDP(pAddr, nPort);
        break;
    }
    case CSimpleSocket::SocketTypeRaw :
        break;
    default:
        break;
    }

    //--------------------------------------------------------------------------
    // If successful then create a local copy of the address and port
    //--------------------------------------------------------------------------
    if (bRetVal)
    {
        socklen_t nSockLen = sizeof(struct sockaddr);

        memset(&m_stServerSockaddr, 0, nSockLen);
        getpeername(m_socket, (struct sockaddr *)&m_stServerSockaddr, &nSockLen);

        nSockLen = sizeof(struct sockaddr);
        memset(&m_stClientSockaddr, 0, nSockLen);
        getsockname(m_socket, (struct sockaddr *)&m_stClientSockaddr, &nSockLen);

        SetSocketError(SocketSuccess);
    }

    return bRetVal;
}
示例#4
0
int CSocket::Send(char* pBuf,int len)
{
	if(!IsSocketValid() || !isConnected)
	{
		return 0;
	}
	if(pBuf==NULL || len<1)
	{
		return 0;
	}
	sendCount=send(csocket,pBuf,len,0);
	if(sendCount<=0)
	{
		cout<<GetSocketError()<<endl;
	}
	return sendCount; 
}
//------------------------------------------------------------------------------
//
// SetSocketDscp() 
//
//------------------------------------------------------------------------------
bool TDSocket::SetSocketDscp(i32 nDscp)
{
	bool  bRetVal = true;
	i32 nTempVal = nDscp;

	nTempVal <<= 4;
	nTempVal /= 4;

	if (IsSocketValid())
	{
		if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_TOS, &nTempVal, sizeof(nTempVal)) == SocketError)
		{
			TranslateSocketError();
			bRetVal = false;
		}
	}

	return bRetVal;
}
示例#6
0
int CSocket::Receive(int strLen)
{
	recvCount=0;
	if(!IsSocketValid() || !isConnected)
	{
		return recvCount;
	} 
	if(strLen<1)
	{
		return recvCount;
	} 
	if(buffer!=NULL)
	{
		delete buffer;
		buffer=NULL;
	} 
	buffer=new char[strLen];
	SetSocketError(SocketEnum::Success); 
	while(1){
		cout << "wait rec" << endl;
		recvCount=recv(csocket,buffer,strLen,0) ; 
		cout << "rec over" << endl;
		if(recvCount>0){
			buffer[recvCount]='\0';
			if(IsExit())
			{
				Send(buffer,recvCount);
				delete buffer;
				buffer=NULL;
				recvCount=0; 
				break;
			}else{
				for (int i = 0; i < recvCount; i++)
				{
					cout << buffer[i] << endl;
				}
				cout<<buffer<<endl;
			}
		}
	}
	 
	return recvCount;
}
示例#7
0
//------------------------------------------------------------------------------
//
// Close() - Close socket and free up any memory allocated for the socket
//
//------------------------------------------------------------------------------
bool CPassiveSocket::Close(void)
{
    if (IsSocketValid())
    {
        if (shutdown(m_socket, CSimpleSocket::Both) != CSimpleSocket::SocketError)
        {
            if (CLOSE(m_socket) != CSimpleSocket::SocketError)
            {
                m_socket = CSimpleSocket::SocketError;
                SetSocketError(CSimpleSocket::SocketInvalidSocket);
                return true;
            }
        }
    }

    TranslateSocketError();

    return false;
}
//------------------------------------------------------------------------------
//
// Close() - Close socket and free up any memory allocated for the socket
//
//------------------------------------------------------------------------------
bool TDSocket::Close(void)
{
	bool bRetVal = false;

	//--------------------------------------------------------------------------
	// if socket handle is currently valid, close and then invalidate
	//--------------------------------------------------------------------------
	if (IsSocketValid())
	{
		if (CLOSE(m_socket) != TDSocket::SocketError)
		{
			m_socket = INVALID_SOCKET;
			bRetVal = true;
		}
	}

	m_bIsConnect = false;
	TranslateSocketError();

	return bRetVal;
}
//------------------------------------------------------------------------------
//
// Initialize() - Initialize socket class
//
//------------------------------------------------------------------------------
bool TDSocket::Initialize()
{
	errno = TDSocket::SocketSuccess;

#ifdef WIN32
	//-------------------------------------------------------------------------
	// Data structure containing general Windows Sockets Info                
	//-------------------------------------------------------------------------
	//memset(&m_hWSAData, 0, sizeof(m_hWSAData));
	WSAStartup(MAKEWORD(2, 0), &m_hWSAData);

#endif

	//-------------------------------------------------------------------------
	// Create the basic Socket Handle										 
	//-------------------------------------------------------------------------
	m_socket = socket(m_nSocketDomain, m_nSocketType, 0);

	TranslateSocketError();

	return (IsSocketValid());
}
//------------------------------------------------------------------------------
//
// Receive() - Attempts to receive a block of data on an established		
//			   connection.	Data is received in an internal buffer managed	
//			   by the class.  This buffer is only valid until the next call	
//			   to Receive(), a call to Close(), or until the object goes out
//			   of scope.													
//																			
//------------------------------------------------------------------------------
i32 TDSocket::Receive(i32 nMaxBytes)
{
	m_nBytesReceived = 0;

	//--------------------------------------------------------------------------
	// If the socket is invalid then return false.
	//--------------------------------------------------------------------------
	if (IsSocketValid() == false)
	{
		return m_nBytesReceived;
	}

	//--------------------------------------------------------------------------
	// Free existing buffer and allocate a new buffer the size of
	// nMaxBytes.
	//--------------------------------------------------------------------------
	if (nMaxBytes != m_nBufferSize)
	{
		m_nBufferSize = nMaxBytes;
		m_pBuffer.clear();
		m_pBuffer.resize(m_nBufferSize);
	}

	SetSocketError(SocketSuccess);

	switch (m_nSocketType)
	{
		//----------------------------------------------------------------------
		// If zero bytes are received, then return.  If SocketERROR is 
		// received, free buffer and return CSocket::SocketError (-1) to caller.	
		//----------------------------------------------------------------------
	case TDSocket::SocketTypeTcp:
	{
		do
		{
			m_nBytesReceived = RECV(m_socket, &m_pBuffer[m_nBytesReceived],
				nMaxBytes - m_nBytesReceived, m_nFlags);
			TranslateSocketError();
		} while (IsSocketValid() && (GetSocketError() == TDSocket::SocketInterrupted));

		break;
	}
	case TDSocket::SocketTypeUdp:
	{
		socklen_t srcSize;

		srcSize = sizeof(struct sockaddr_in);

		if (GetMulticast() == true)
		{
			do
			{
				m_nBytesReceived = RECVFROM(m_socket, &m_pBuffer[0], nMaxBytes, 0,
					&m_stMulticastGroup, &srcSize);
				TranslateSocketError();
			} while (IsSocketValid() && GetSocketError() == TDSocket::SocketInterrupted);
		}
		else
		{
			do
			{
				m_nBytesReceived = RECVFROM(m_socket, &m_pBuffer[0], nMaxBytes, 0,
					&m_stClientSockaddr, &srcSize);
				TranslateSocketError();
			} while (GetSocketError() == TDSocket::SocketInterrupted);
		}

		break;
	}
	default:
		break;
	}

	TranslateSocketError();
	return m_nBytesReceived;
}
//------------------------------------------------------------------------------
//
// Send() - Send data on a valid socket
//
//------------------------------------------------------------------------------
i32 TDSocket::Send(const char *pBuf, size_t bytesToSend)
{
	SetSocketError(SocketSuccess);
	m_nBytesSent = 0;

	switch (m_nSocketType)
	{
	case TDSocket::SocketTypeTcp:
	{
		if (IsSocketValid())
		{
			if ((bytesToSend > 0) && (pBuf != NULL))
			{
				//---------------------------------------------------------
				// Check error condition and attempt to resend if call
				// was interrupted by a signal.
				//---------------------------------------------------------
				do
				{
					m_nBytesSent = SEND(m_socket, pBuf, bytesToSend, 0);
					TranslateSocketError();
				} while (GetSocketError() == TDSocket::SocketInterrupted);

			}
		}
		break;
	}
	case TDSocket::SocketTypeUdp:
	{
		if (IsSocketValid())
		{
			if ((bytesToSend > 0) && (pBuf != NULL))
			{
				//---------------------------------------------------------
				// Check error condition and attempt to resend if call
				// was interrupted by a signal.
				//---------------------------------------------------------
				//                    if (GetMulticast())
				//                    {
				//                        do
				//                        {
				//                            m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stMulticastGroup, 
				//                                                  sizeof(m_stMulticastGroup));
				//                            TranslateSocketError();
				//                        } while (GetSocketError() == TDSocket::SocketInterrupted);
				//                    }
				//                    else
				{
					do
					{
						m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stServerSockaddr, sizeof(m_stServerSockaddr));
						TranslateSocketError();
					} while (GetSocketError() == TDSocket::SocketInterrupted);
				}

			}
		}
		break;
	}
	default:
		break;
	}

	return m_nBytesSent;
}
示例#12
0
//------------------------------------------------------------------------------
//
// Receive() - Attempts to receive a block of data on an established		
//			   connection.	Data is received in an internal buffer managed	
//			   by the class.  This buffer is only valid until the next call	
//			   to Receive(), a call to Close(), or until the object goes out
//			   of scope.													
//																			
//------------------------------------------------------------------------------
int32 CSimpleSocket::Receive(int32 nMaxBytes)
{
    m_nBytesReceived = 0;

    //--------------------------------------------------------------------------
    // If the socket is invalid then return false.
    //--------------------------------------------------------------------------
    if (IsSocketValid() == false)
    {
        return m_nBytesReceived;
    }

    //--------------------------------------------------------------------------
    // Free existing buffer and allocate a new buffer the size of
    // nMaxBytes.
    //--------------------------------------------------------------------------
    if ((m_pBuffer != NULL) && (nMaxBytes != m_nBufferSize))
    {
        delete [] m_pBuffer;
        m_pBuffer = NULL;
    }

    //--------------------------------------------------------------------------
    // Allocate a new internal buffer to receive data.
    //--------------------------------------------------------------------------
	if (m_pBuffer == NULL)
	{
		m_nBufferSize = nMaxBytes;
		m_pBuffer = new uint8[nMaxBytes]; 
	}

    SetSocketError(SocketSuccess);

    switch (m_nSocketType)
    {
        m_timer.Initialize();
        m_timer.SetStartTime();

        //----------------------------------------------------------------------
        // If zero bytes are received, then return.  If SocketERROR is 
        // received, free buffer and return CSocket::SocketError (-1) to caller.	
        //----------------------------------------------------------------------
        case CSimpleSocket::SocketTypeTcp:
        {
            do 
            {
                m_nBytesReceived = RECV(m_socket, (m_pBuffer + m_nBytesReceived), 
                                        nMaxBytes, m_nFlags);
                TranslateSocketError();
            } while ((GetSocketError() == CSimpleSocket::SocketInterrupted));

            break;
        }
        case CSimpleSocket::SocketTypeUdp:
        {
            uint32 srcSize;
                
            srcSize = sizeof(struct sockaddr_in);

            if (GetMulticast() == true)
            {
                do 
                {
                    m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0, 
                                                &m_stMulticastGroup, &srcSize);
                    TranslateSocketError();
                } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
            }
            else
            {
                do 
                {
                    m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0, 
                                                &m_stClientSockaddr, &srcSize);
                    TranslateSocketError();
                } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
            }

            break;
        }
        default:
            break;
    }
    
    m_timer.SetEndTime();
    TranslateSocketError();

    //--------------------------------------------------------------------------
    // If we encounter an error translate the error code and return.  One 
    // possible error code could be EAGAIN (EWOULDBLOCK) if the socket is
    // non-blocking.  This does not mean there is an error, but no data is
    // yet available on the socket.
    //--------------------------------------------------------------------------
    if (m_nBytesReceived == CSimpleSocket::SocketError)
    {
        if (m_pBuffer != NULL)
        {
            delete [] m_pBuffer;
            m_pBuffer = NULL;
        }
    }

    return m_nBytesReceived;
}