Exemplo n.º 1
0
/**
* Listens and accepts a client.Returns the accepted connection.
* @return CSocket*.
* @throw CSocketException
* @since 1.0.0
*/
CSocket* CServerSocket::Accept() { // throw (CSocketException) {
	if(m_sockAddr != NULL) 
		m_sockAddrIn = m_sockAddr->GetSockAddrIn();
	if(!m_bBound) {	
		m_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
		int nret = bind(m_socket, (LPSOCKADDR)&m_sockAddrIn, sizeof(struct sockaddr));
		if (nret == SOCKET_ERROR) {
			nret = WSAGetLastError();
			throw CSocketException(nret, "Failed to bind: Accept()");
		}
		m_bBound = true;
	}	
	int nret = listen(m_socket, m_nQueue);
	if (nret == SOCKET_ERROR) {
		nret = WSAGetLastError();
		throw CSocketException(nret, "Failed to listen: Accept()");
	}
	SOCKET theClient;
	SOCKADDR_IN clientAddr;
	int ssz = sizeof(struct sockaddr);
	theClient = accept(m_socket,(LPSOCKADDR)&clientAddr,&ssz);
	//theClient = accept(m_socket,NULL,NULL);
	if (theClient == INVALID_SOCKET) {
		int nret = WSAGetLastError();
		throw CSocketException(nret, "Invalid client socket: Accept()");		
	}
	CSocket *sockClient = new CSocket();
	sockClient->SetSocket(theClient);
	sockClient->SetClientAddr(clientAddr);		
	return sockClient;
}
Exemplo n.º 2
0
CSocket* CServerSocket::BindAndListen() throw (CSocketException) {
	if(m_sockAddr != NULL) 
		m_sockAddrIn = m_sockAddr->GetSockAddrIn();

	if(!m_bBound) {	
        if (m_socket == INVALID_SOCKET) {
		    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        }
        // The Bind method binds the socket to the server.
		if (bind(m_socket, (LPSOCKADDR)&m_sockAddrIn, sizeof(struct sockaddr)) == SOCKET_ERROR) {
			throw CSocketException(WSAGetLastError(), "Failed to bind: BindAndListen()");
		}
		m_bBound = true;
	}	

    // The listen function places a socket in a state in which it is listening for an incoming connection.
	if (listen(m_socket, m_nQueue) == SOCKET_ERROR) {   
		throw CSocketException(WSAGetLastError(), "Failed to listen: BindAndListen()");
	}

	CSocket *sock = new CSocket();
	sock->SetSocket(m_socket);
	sock->SetClientAddr(m_sockAddrIn);		
	return sock;
}
Exemplo n.º 3
0
CSocket* CServerSocket::Bind() throw (CSocketException) {
	if(m_sockAddr != NULL) 
		m_sockAddrIn = m_sockAddr->GetSockAddrIn();

	if(!m_bBound) {	
        if (m_socket == INVALID_SOCKET) {
		    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        }
            // The Bind method binds the socket to the server.
		if (bind(m_socket, (LPSOCKADDR)&m_sockAddrIn, sizeof(struct sockaddr)) == SOCKET_ERROR) {
			throw CSocketException(WSAGetLastError(), "Failed to bind: Bind()");
		}
		m_bBound = true;
	}	 

	CSocket *sock = new CSocket();
	sock->SetSocket(m_socket);
	sock->SetClientAddr(m_sockAddrIn);		
	return sock;
}
Exemplo n.º 4
0
/**
* Listens and accepts a client. Returns the accepted connection.
* @return CSocket*.
* @throw CSocketException
* @since 1.0.0
*/
CSocket* CServerSocket::Accept() throw (CSocketException) {
	if(m_sockAddr != NULL) 
		m_sockAddrIn = m_sockAddr->GetSockAddrIn();

	if(!m_bBound) {	
        if (m_socket == INVALID_SOCKET) {
		    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        }
        // The Bind method binds the socket to the server.
		int nret = bind(m_socket, (LPSOCKADDR)&m_sockAddrIn, sizeof(struct sockaddr));
		if (nret == SOCKET_ERROR) {
			throw CSocketException(WSAGetLastError(), "Failed to bind: Accept()");
		}
		m_bBound = true;
	}	

    // The listen function places a socket in a state in which it is listening for an incoming connection.
	if (listen(m_socket, m_nQueue) == SOCKET_ERROR) {
		throw CSocketException(WSAGetLastError(), "Failed to listen: Accept()");
	}

	SOCKET theClient = INVALID_SOCKET;
	SOCKADDR_IN clientAddr;
	int ssz = sizeof(struct sockaddr);

    // The accept function permits an incoming connection attempt on a socket.
	theClient = accept(m_socket,(LPSOCKADDR)&clientAddr, &ssz);
	if (theClient == INVALID_SOCKET) {
		throw CSocketException(WSAGetLastError(), "Invalid client socket: Accept()");		
	}

	CSocket *sockClient = new CSocket();
	sockClient->SetSocket(theClient);
	sockClient->SetClientAddr(clientAddr);		
	return sockClient;
}