void CBaseSocket::_AcceptNewSocket() { SOCKET fd = 0; sockaddr_in peer_addr; socklen_t addr_len = sizeof(sockaddr_in); char ip_str[64]; while ( (fd = accept(m_socket, (sockaddr*)&peer_addr, &addr_len)) != INVALID_SOCKET ) { CBaseSocket* pSocket = new CBaseSocket(); uint32_t ip = ntohl(peer_addr.sin_addr.s_addr); uint16_t port = ntohs(peer_addr.sin_port); snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", ip >> 24, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF); // log("AcceptNewSocket, socket=%d from %s:%d\n", fd, ip_str, port); pSocket->SetSocket(fd); pSocket->SetCallback(m_callback); pSocket->SetCallbackData(m_callback_data); pSocket->SetState(SOCKET_STATE_CONNECTED); pSocket->SetRemoteIP(ip_str); pSocket->SetRemotePort(port); _SetNoDelay(fd); _SetNonblock(fd); AddBaseSocket(pSocket); CEventDispatch::Instance()->AddEvent(fd, SOCKET_READ | SOCKET_EXCEP); m_callback(m_callback_data, NETLIB_MSG_CONNECT, (net_handle_t)fd, NULL); } }
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; }
void CBaseSocket::_AcceptNewSocket() { SOCKET fd = 0; sockaddr_in peer_addr; socklen_t addr_len = sizeof(sockaddr_in); char ip_str[64]; while ( (fd = accept(m_socket, (sockaddr*)&peer_addr, &addr_len)) != INVALID_SOCKET ) { CBaseSocket* pSocket = new CBaseSocket(m_pBaseServer); uint32_t ip = ntohl(peer_addr.sin_addr.s_addr); uint16_t port = ntohs(peer_addr.sin_port); snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", ip >> 24, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF); printf("AcceptNewSocket, socket=%d from %s:%d\n", fd, ip_str, port); pSocket->SetSocket(fd); pSocket->SetState(SOCKET_STATE_CONNECTED); pSocket->SetRemoteIP(ip_str); pSocket->SetRemotePort(port); pSocket->SetNotify(m_Notify); _SetNoDelay(fd); _SetNonblock(fd); m_pBaseServer->AddBaseSocket(pSocket); m_pBaseServer->AddEvent(fd, SOCKET_READ | SOCKET_EXCEP); m_Notify->OnConnect((net_handle_t)fd); } }
//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; }
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; }