当前位置: 首页>>代码示例>>C++>>正文


C++ CSocket::SetClientAddr方法代码示例

本文整理汇总了C++中CSocket::SetClientAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ CSocket::SetClientAddr方法的具体用法?C++ CSocket::SetClientAddr怎么用?C++ CSocket::SetClientAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CSocket的用法示例。


在下文中一共展示了CSocket::SetClientAddr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Accept

/**
* 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;
}
开发者ID:ronia,项目名称:flame-adapter,代码行数:37,代码来源:sock.cpp

示例2: BindAndListen

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;
}
开发者ID:AmesianX,项目名称:AVA,代码行数:25,代码来源:ServerSocket.cpp

示例3: Bind

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;
}
开发者ID:AmesianX,项目名称:AVA,代码行数:20,代码来源:ServerSocket.cpp

示例4: Accept

/**
* 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;
}
开发者ID:AmesianX,项目名称:AVA,代码行数:42,代码来源:ServerSocket.cpp


注:本文中的CSocket::SetClientAddr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。