chapter  20
Multi-player games using sockets
Pages 16

Everytime a new connection needs to be made, the server socket calls the dialog box function ‘ProcessPendingAccept’. This function creates a new client socket and then calls the server function ‘Accept’ using a pointer to this new client socket. If you are new to pointers then think of it as a variable that points to the position in memory where the actual data that you are interested in reside. If the server function ‘Accept’ returns true then this socket is added to the list of sockets being handled by the server socket, if

Fla shM

XG a

m e


void CSockListenerDlgProcessPendingAccept () {

TRACE("CSockListenerDlgProcessPendingAccept\n"); CString csSockAddr; CString csInfo; UINT nSockPort;

CClientSocket *pSocket = new CClientSocket(this);

if (m_ServerSocket.Accept(*pSocket)) {

if(pSocket->GetSockName(csSockAddr, nSockPort)) { csInfo.Format("%s : %u – Connected", ↵

csSockAddr, nSockPort); m_list1.InsertString(–1, csInfo);

} m_connectionList.AddTail(pSocket);

} else

delete pSocket; }

When a client socket is ready to close down, the socket calls the dialog box function ‘OnCloseConnection’; this function prints a message to the dialogs list box and builds an XML message string.