本文整理汇总了C++中CSocket::getRemoteIp方法的典型用法代码示例。如果您正苦于以下问题:C++ CSocket::getRemoteIp方法的具体用法?C++ CSocket::getRemoteIp怎么用?C++ CSocket::getRemoteIp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSocket
的用法示例。
在下文中一共展示了CSocket::getRemoteIp方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: acceptSock
void acceptSock(CSocket& pSocket, int pType)
{
CSocket* newSock = pSocket.accept();
if (newSock == 0)
return;
// Server ip bans.
if (pType == SOCK_SERVER)
{
CString ip(newSock->getRemoteIp());
for (std::vector<CString>::const_iterator i = ipBans.begin(); i != ipBans.end(); ++i)
{
if (ip.match(*i))
{
printf("Rejected server: %s matched ip ban %s\n", ip.text(), i->text());
newSock->disconnect();
delete newSock;
return;
}
}
}
//newSock->setOptions( SOCKET_OPTION_NONBLOCKING );
serverlog.out(CString() << "New Connection: " << CString(newSock->getRemoteIp()) << " -> " << ((pType == SOCK_PLAYER) ? "Player" : "Server") << "\n");
if (pType == SOCK_PLAYER || pType == SOCK_PLAYEROLD)
playerList.push_back(new TPlayer(newSock, (pType == SOCK_PLAYEROLD ? true : false)));
else serverList.push_back(new TServer(newSock));
}
示例2: accept
CSocket* CSocket::accept()
{
// Make sure the socket is connected!
if (properties.state == SOCKET_STATE_DISCONNECTED)
return 0;
// Only server type TCP sockets can accept new connections.
if (properties.type != SOCKET_TYPE_SERVER || properties.protocol != SOCKET_PROTOCOL_TCP)
return 0;
sockaddr_storage addr;
int addrlen = sizeof(addr);
SOCKET handle = 0;
// Try to accept a new connection.
handle = ::accept(properties.handle, (struct sockaddr*)&addr, (socklen_t*)&addrlen);
if (handle == INVALID_SOCKET)
{
int error = identifyError();
if (error == EWOULDBLOCK || error == EINPROGRESS) return 0;
SLOG("[CSocket::accept] accept() returned error: %s\n", errorMessage(error));
return 0;
}
// Create the new socket to store the new connection.
CSocket* sock = new CSocket();
sock_properties props;
memset((void*)&props, 0, sizeof(sock_properties));
memset((void*)&properties.address, 0, sizeof(struct sockaddr_storage));
memcpy((void*)&props.address, &addr, sizeof(addr));
props.protocol = properties.protocol;
props.type = SOCKET_TYPE_CLIENT;
props.state = SOCKET_STATE_CONNECTED;
props.handle = handle;
sock->setProperties(props);
sock->setDescription(sock->getRemoteIp());
// Disable the nagle algorithm.
if (props.protocol == SOCKET_PROTOCOL_TCP)
{
int nagle = 1;
setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, (char*)&nagle, sizeof(nagle));
}
// Set as non-blocking.
#if defined(_WIN32) || defined(_WIN64)
u_long flags = 1;
ioctlsocket(handle, FIONBIO, &flags);
#else
int flags = fcntl(properties.handle, F_GETFL, 0);
fcntl(handle, F_SETFL, flags | O_NONBLOCK);
#endif
// Accept the connection by calling getsockopt.
int type, typeSize = sizeof(int);
getsockopt(handle, SOL_SOCKET, SO_TYPE, (char*)&type, (socklen_t*)&typeSize);
return sock;
}