1Network Chat Room Item description1.1function Description
MyQQv1 adopts c/S mode, constructs the network chat room, realizes the function concretely:
A. can display the online user list
B. ability to group chat in chat rooms
C. ability to designate users for private chats
D. a user offline, other users can receive the prompt
1.2the required technology
MFC,socket sockets ,TCP/IP protocol, dynamic array,CString string concatenation and splitting
2Network chat room running flowchart2.1Server Run Flowchart
2.2 Client Run flowchart
3Network chat room details3.1Communication Format
Each communication between the client and the server is sent "start& command & own name & message & The private &end"(for a standard format string) string, then the subject will split and follow the"&"fragment resolution.
3.1.1Message Segmentation
When more than one format string is accepted at a time (when accepting the full list of online users), the entire string is cut into several standard format strings. The list of programs is shown below.
/******************************************************************** function Name: msgcut function: Divide data flow by Communication format processing incoming parameters: CString strtext return value: no ********************************************************************/void cclientsocket:: Msgcut (CString strText) {UINT nfirst, nlast; CString Strtmp;while (Strtext.getlength () > 9) {nfirst = Strtext.find ("START"); Nlast = Strtext.find ("END"); strtmp = Strtext.mid (Nfirst, nlast+3);//intercept the string from start to end each time char * Stran = strtmp.getbuffer (0);//cstring converted to char * Type msgdeal (stran); strText = Strtext.mid (Nlast+3,strtext.getlength ()-nlast-3);}}
Nfirstget "START"The starting position of the substring,Nlastget "END"The starting position of the substring,strtmpto "START"Start to"END"(including the endD) is a standard format string. and cut off the original long string of the newly formedstrtmp,StrTextUseMid (previousEDNlocation+3,Total length-previousEDNlocation-3)function to get the cut-out string untilStrTextmeets the minimum standard length.
3.1.2Press "&the segment resolution
Each standard format string contains a command, a username, a message, a private person 4 A message. They follow the " & " connected, receiving end to press " & "fragment resolution, the program list is as follows.
/*********************************************************************** function name: msgexplain function function: In the case of message disassembly, the message comes in the form k&1& username & words & private person passed in parameter: Char Smsgdeal[5][bufmax],char * smsginit return value: No **************** /void Cclientsocket::msgexplain (char Smsgdeal[6][bufmax], char * smsginit) {char *p;bool BFLG = false;int IRow = -1;int Icol = 0;char c = 0;p=smsginit;while (*p! = ') ' {c = *p;if ( c! = ' & ') {if (BFLG = = FALSE)//If a new word column is encountered {BFLG = True;irow++;icol = 0;} Smsgdeal[irow][icol] = c;icol++;} Else{if (BFLG = = TRUE) {smsgdeal[irow][icol]= ';} BFLG = FALSE;} P++;smsgdeal[irow][icol] = ' + ';}}
The last sentence is no &when parsing to the last word, but still tothe end of the string with thestring terminator, to avoid the last Smsgdeal[6] string error.
3.2Server Class3.2.1overloadedonacceptfunction
After the server has successfully opened with Create (PORT) , it listens with Listen () and occurs when a client makes a connection request onaccept Message response, at which time the onaccept function is overloaded.
/*********************************************************************** function Name: onaccept function: Accept connection incoming parameter: int Nerrorcode return value: no ***********************************************************************/void CServerSocket: : onaccept (int nerrorcode) {//Todo:add your specialized code here and/or call the base classcclientsocket *_pnewclient = New Cclientsocket (); _pnewclient->getserverpointer (this);//!! This sentence is important, bind the server socket to this accept (*_pnewclient); M_clientarr.add (_pnewclient);//accept a connection to add it to the dynamic array csocket::onaccept ( Nerrorcode);}
Create a new cclientsocket socket, bind the client's connection to the socket, and add the socket to the dynamic array. The equivalent of the server is no longer managed since the new connection was accepted, and the subsequent send and accept are done using their newly created cclientsocket sockets.
3.2.2Send to specified user
The server is responsible for the forwarding of all messages, and when there is a private message sent, the classification process. When there is a private message, scan for dynamic numbers, compare the name of the private person with the name of each socket in the dynamic array, and then forward the same.
3.3Client Class3.3.1Initial Connection
The client connection succeeds, sending a callout format string that contains its own name. Shown in the following list of programs.
/*********************************************************************** function Name: onbutlink function Function: Client Connection server incoming parameter: No return value: No ***********************************************************************/void CSetDlg::OnButLink () {//Todo:add Your control notification handler code HERECMYQQDLG *pparent = (cmyqqdlg*) this->getparent (); Pparent->m_client.create ();//pparent->m_client.setdialog (pparent); Set socket member variable UpdateData (TRUE);pP arent->m_client.m_strusername = M_sname;if (Pparent->m_client.connect (m_ Sserverip,port) {MessageBox ("Connection server succeeded"); CString str;pparent->m_client.m_strusername = m_sname;str. Format ("Start&0&%s&0&0&end", M_sname);pP arent->m_client.send (str,str. GetLength ());pP ARENT->M_CCHOSEFLG = 2;pparent->setwindowtext ("Client--" +m_sname); Else{messagebox ("Connection Failed", "warning");} EndDialog (0);}
The server accepts a new user name and assigns it to the newly created cclientsocket m_strusernameso that each socket in the dynamic array has a m_ strUserName with it, prepare for private information.
3.3.2overloadedOnReceivefunction
The client receives the message, generates a onreceive message, rewrites it, and parses the string. The list of procedures is as follows.
/*********************************************************************** function Name: onreceive function: The response function of the client to accept the message passed in parameter: int Nerrorcode return value: no ***********************************************************************/void CClientSocket: : onreceive (int nerrorcode) {//Todo:add your specialized code here and/or call the base Classchar strtext[512] = {0}; Receive (StrText, 512); Msgcut (StrText); Csocket::onreceive (Nerrorcode);}
After the client receives the message, it generates a onreceive message. Overloads the onreceive function to parse the string each time it is obtained.
4Network chat room operating condition
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Network chat room