Game Development Reference
In-Depth Information
// create socket handle
if ((m_sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
GCC_ERROR(
NetListenSocket Error: Init failed to create socket handle
);
}
// set socket options to reuse server socket addresses even if they are
// busy - this is important if your server restarts and you don
'
t want
// to wait for your sockets to time out.
if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&value, sizeof(value))== SOCKET_ERROR)
{
closesocket(m_sock);
m_sock = INVALID_SOCKET;
GCC_ERROR(
NetListenSocket Error: Init failed to set socket options
);
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = ADDR_ANY;
sa.sin_port = htons(portnum);
// bind to port
if (bind(m_sock, (struct sockaddr *)&sa, sizeof(sa)) == SOCKET_ERROR)
{
closesocket(m_sock);
m_sock = INVALID_SOCKET;
GCC_ERROR(
NetListenSocket Error: Init failed to bind
);
}
// set nonblocking - accept() blocks under some odd circumstances otherwise
SetBlocking(false);
// start listening
if (listen(m_sock, 256) == SOCKET_ERROR)
{
closesocket(m_sock);
m_sock = INVALID_SOCKET;
GCC_ERROR(
NetListenSocket Error: Init failed to listen
);
}
port = portnum;
}
'
If the listen socket gets any input, it means there
s a client ready to attach. The method
that handles the attachment and creates a new socket handle is AcceptConnection() .
Search WWH ::




Custom Search