Server Port 1, requires a specified port number (long type m_lport), and we also need a cptrlist object M_ClientSocks to manage the socket of each customer. 2. Detect a CLISTENSocket class from the Socket class to listen to the customer's connection request. (Performed by derived class object M_Li- STENSOCKET) 3, based on M_Lport to create a listener interface m_listensocket-> create (m_lport); then you can start listening to m_listensocket-> listen (); 5, in the process of listening We will receive a request in the customer, and we implement this by adding the virtual-function accept (int NerrorCode) of the CLISTENSocket class. 6. In the program, a Socket class is also required to read and write the customer's socket-operation. Here, the data that receives the user is done by the virtual function onRecEive (int NerrorCode) of the class.
void CReadWriteSocket :: OnReceive (int nErrorCode) {Receive (& m_Data, sizeof (m_Data)); :: PostMessage (:: AfxGetApp () -> m_pMainWnd-> m_hWnd, CLT_TALKING, (WPARAM) & m_Data, 0); // here we Use postMessage to transfer a message, notify the application to handle information from the user // so we also define this message and the processing function of this message; // User Online, proceel to process if (m_data.m_bonline) {function } Else {:: PostMessage (:: AfxGetApp () -> m_pmainwnd-> m_hWnd, CLT_Close, (WPARAM) & this, 0);}} 7, we define the messages we want to use in the header file of the CReadwritesocket class; #define CLT_CLOSE WM_USE #define CLT_TALKING WM_USER 28, is a message transmitted CReadWriteSocket class object is added in the main message corresponding: ON_MESSAGE (CLT_CLOSE, OnClientClose) ON_MESSAGE (CLT_TALKING, OnClientTalking) 9, we want to add the above-mentioned function LRESULT CMa In *** dlg :: OnClientClose (WPARAM WPARAM, LPARAM LPARAM) {creadwritesocket * psock; psock = (creadwritesocket *) mParam; m_listensocket-> clossoSocket; RETURN 1; // This function is mainly used to handle user launch When the function is functional, it is mainly to turn off the socket // can also handle the user's data storage, and the like. } LResult cmain *** dlg :: OnClientTalking (WPARAM WPARAM, LPARAM LPARAM) {// Processes messages from users;} 10, CSocket classes are derived from the CasynvSocket class, can't think of the CasyncSocket class is from csocket Class - derived; 11, basic functions:
Create a socket
Csocket :: Create
Uint ncsocketport = 0;
Int nsockettype = Sock_Stream,
LPCTSRT LPSZSOCKETDRESS = NULL)