Beispiel #1
0
net_handle_t CBaseSocket::Connect(const char* server_ip, uint16_t port, callback_t callback, void* callback_data)
{
    log("CBaseSocket::Connect, server_ip=%s, port=%d\n", server_ip, port);
    
    m_remote_ip = server_ip;
    m_remote_port = port;
    m_callback = callback;
    m_callback_data = callback_data;
    
    m_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (m_socket == INVALID_SOCKET)
    {
        log("socket failed, err_code=%d\n", _GetErrorCode());
        return NETLIB_INVALID_HANDLE;
    }
    
    _SetNonblock(m_socket);
    _SetNoDelay(m_socket);
    
    sockaddr_in serv_addr;
    _SetAddr(server_ip, port, &serv_addr);
    int ret = connect(m_socket, (sockaddr*)&serv_addr, sizeof(serv_addr));
    if ( (ret == SOCKET_ERROR) && (!_IsBlock(_GetErrorCode())) )
    {
        log("connect failed, err_code=%d\n", _GetErrorCode());
        closesocket(m_socket);
        return NETLIB_INVALID_HANDLE;
    }
    
    m_state = SOCKET_STATE_CONNECTING;
    AddBaseSocket(this);
    CEventDispatch::Instance()->AddEvent(m_socket, SOCKET_ALL);
    
    return (net_handle_t)m_socket;
}
net_handle_t CBaseSocket::Connect(const char* server_ip, uint16_t port, IServerNotify* pNotify)
{
	printf("CBaseSocket::Connect, server_ip=%s, port=%d\n", server_ip, port);

	m_remote_ip = server_ip;

	m_remote_port = port;

	m_Notify = pNotify;

	m_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (m_socket == INVALID_SOCKET)
	{
		printf("socket failed, err_code=%d\n", _GetErrorCode());
		return NETLIB_INVALID_HANDLE;
	}

	_SetNonblock(m_socket);
	_SetNoDelay(m_socket);
	sockaddr_in serv_addr;
	_SetAddr(server_ip, port, &serv_addr);
	int ret = connect(m_socket, (sockaddr*)&serv_addr, sizeof(serv_addr));
	if ( (ret == SOCKET_ERROR) && (!_IsBlock(_GetErrorCode())) )
	{	
		printf("connect failed, err_code=%d\n", _GetErrorCode());
		closesocket(m_socket);
		return NETLIB_INVALID_HANDLE;
	}
	m_state = SOCKET_STATE_CONNECTING;
	m_pBaseServer->AddBaseSocket(this);
	m_pBaseServer->AddEvent(m_socket, SOCKET_ALL);
	
	return (net_handle_t)m_socket;
}
Beispiel #3
0
//add by xieqq 2016-05-11  udp socket////////////////////////////
int CBaseSocket::UDP_Bind(const char* server_ip, uint16_t port,  callback_t callback, void* callback_data)
{
	m_local_ip = server_ip;
	m_local_port = port;
	m_callback = callback;
	m_callback_data = callback_data;

	m_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if (m_socket == INVALID_SOCKET)
	{
		printf("socket failed, err_code=%d\n", _GetErrorCode());
		return NETLIB_ERROR;
	}
	// 这通常是重启监听服务器时出现,若不设置此选项,则bind时将出错。
	_SetReuseAddr(m_socket); //  /*设置socket属性,端口可以重用*/

	_SetNonblock(m_socket); // 设置句柄为非阻塞方式

	sockaddr_in serv_addr;

	printf("socket success");

	_SetAddr(server_ip, port, &serv_addr);
	// 绑定端口
    int ret = ::bind(m_socket, (sockaddr*)&serv_addr, sizeof(serv_addr));
	if (ret == SOCKET_ERROR)
	{
		log("bind failed, err_code=%d", _GetErrorCode());
		closesocket(m_socket);
		return NETLIB_ERROR;
	} 

	//ret = listen(m_socket, 64); // udp不用listen
	//if (ret == SOCKET_ERROR)
	//{
	//	log("listen failed, err_code=%d", _GetErrorCode());
	//	closesocket(m_socket);
	//	return NETLIB_ERROR;
	//}

	m_state = SOCKET_STATE_UDP_BIND;

	log("CBaseSocket::UDP_Bind on any %s :%d", server_ip, port);

	AddBaseSocket(this); // g_socket_map.insert	
		
	// UDP不用连接,直接调用连接成功
	m_callback(m_callback_data, NETLIB_MSG_CONNECT, (net_handle_t)m_socket, NULL);

	CEventDispatch::Instance()->AddUDPEvent(m_socket, SOCKET_READ | SOCKET_EXCEP);// SOCKET_ALL);//SOCKET_READ | SOCKET_EXCEP);
	// udp 只是sendto recvfrom 要不要用epoll?  
	//  先用epoll试试  epoll收不到消息?
	//  就不addEvent了

	return NETLIB_OK;
}
int CBaseSocket::Listen(const char* server_ip, uint16_t port, IServerNotify* pNotify)
{
	m_local_ip = server_ip;
	m_local_port = port;
	m_Notify = pNotify;

	m_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (m_socket == INVALID_SOCKET)
	{
		printf("socket failed, err_code=%d\n", _GetErrorCode());
		return NETLIB_ERROR;
	}

	printf("Main Socket[%d]...\n", m_socket);

	_SetReuseAddr(m_socket);
	_SetNonblock(m_socket);

	sockaddr_in serv_addr;
	_SetAddr(server_ip, port, &serv_addr);
    int ret = ::bind(m_socket, (sockaddr*)&serv_addr, sizeof(serv_addr));
	if (ret == SOCKET_ERROR)
	{
		printf("bind failed, err_code=%d\n", _GetErrorCode());
		closesocket(m_socket);
		return NETLIB_ERROR;
	}

	ret = listen(m_socket, 64);
	if (ret == SOCKET_ERROR)
	{
		printf("listen failed, err_code=%d\n", _GetErrorCode());
		closesocket(m_socket);
		return NETLIB_ERROR;
	}

	m_state = SOCKET_STATE_LISTENING;

	printf("CBaseSocket::Listen on %s:%d\n", server_ip, port);

	m_pBaseServer->AddBaseSocket(this);
	m_pBaseServer->AddEvent(m_socket, SOCKET_READ | SOCKET_EXCEP);
	return NETLIB_OK;
}
Beispiel #5
0
int CBaseSocket::Listen(const char* server_ip, uint16_t port, callback_t callback, void* callback_data)
{
    m_local_ip = server_ip;
    m_local_port = port;
    m_callback = callback;
    m_callback_data = callback_data;
    
    m_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (m_socket == INVALID_SOCKET)
    {
        printf("socket failed, err_code=%d\n", _GetErrorCode());
        return NETLIB_ERROR;
    }
    
    _SetReuseAddr(m_socket);
    _SetNonblock(m_socket);
    
    sockaddr_in serv_addr;
    _SetAddr(server_ip, port, &serv_addr);
    int ret = ::bind(m_socket, (sockaddr*)&serv_addr, sizeof(serv_addr));
    if (ret == SOCKET_ERROR)
    {
        log("bind failed, err_code=%d\n", _GetErrorCode());
        closesocket(m_socket);
        return NETLIB_ERROR;
    }
    
    ret = listen(m_socket, 64);
    if (ret == SOCKET_ERROR)
    {
        log("listen failed, err_code=%d\n", _GetErrorCode());
        closesocket(m_socket);
        return NETLIB_ERROR;
    }
    
    m_state = SOCKET_STATE_LISTENING;
    
    //log("CBaseSocket::Listen on %s:%d\n", server_ip, port);
    
    AddBaseSocket(this);
    CEventDispatch::Instance()->AddEvent(m_socket, SOCKET_READ | SOCKET_EXCEP);
    return NETLIB_OK;
}