LAN chat system _ 1. Requirement Analysis and Model Selection

Source: Internet
Author: User
I. Requirement Analysis

This simple LAN chat system consists of two parts: server and client. The client is sent to the user for chat, and the server user manages all user information. Client functions: 1. chat with other online users. update user status: when a user goes online or offline, the user status is updated in time. update user list: when other users are online and get deleted by the server, update the user list in time. 4. send offline messages: users can send offline messages to other offline users. receive offline messages: when a user goes online, the user can receive offline messages from other users. 6. persistent chat record: Save User chat record to file next time start chat automatically load server function: 1. view the current user list: You can view the user details, including the IP password online and offline time. maintain the current user list: delete offline users and force an online user to go offline. verify User Logon: Perform necessary verification (password verification, repeated logon verification, etc.) when the user logs on. save offline messages: Save offline messages sent by online users for offline users. forward offline messages: When an offline User goes online, the offline messages stored for the offline user are forwarded. 6. notify the client to update the member list: notify the client to update the list when a new user is registered or deleted. notify the client to update the member status: when a user is offline, notify all clients to update the user status. persistence of necessary data: when the server exits, save the offline messages currently saved and the user list. interface preview Server

Client Interface logon Interface
User Interface offline message chat interface online chat
Other Interface 3 for offline chat. Select the wsaasyncselect model here for the communication model, and use the csocket class to complete communication between the client and the server, as well as between the client and the client. The implementation principle of csocket is not described here. The basic features of csocket are as follows: 1. according to the function (listening socket, communication socket), derive csocket to get the desired Class 2. overwrite the virtual functions (such as onaccept onreceive onsend onclose) corresponding to messages of interest to the csocket derived class. 3. in the corresponding override function, it is necessary to specify the csocketfile class here. The serialization operation makes csocket-based communication very simple. This instance also communicates with each other through csocketfile. Each csocket class or its derived class should have a csocketfile associated with it to complete serialization communication. You can associate a csocketfile object with a csocket class by passing a pointer to the csocket class when constructing the csocketfile object. For example, after the csocket class is constructed, you can call
M_psocketfile = new csocketfile (this); after the csocketfile object is associated and Its pointer is saved, you can use the following method to send data carchive * parchiveout = new carchive (m_psocketfile, carchive :: store );


// Send a message
Chatpacket. serialize (* parchiveout );
Parchiveout-> flush ();

Delete parchiveout;
Parchiveout = NULL;

Chatpacket is the communication package. This package must be a class capable of serialization. Data is written to the file stream pointed to by m_psocketfile through serialization, and the file stream is associated with the csocket class, so that data is written to the corresponding sending buffer. Similarly, the receiving file is simplified to: carchive * parchivein = new carchive (m_psocketfile, carchive: load );

// Write data to the file stream chatpacket. serialize (* parchivein );
Delete parchiveout;
Parchiveout = NULL; For onaccept, when accepting a connection request, its parameter requires a csocket Class Object: cclientsocket * pclient = new cclientsocket ();


// Call the listening socket to accept the connection request and associate it with the client object
Sockaddr_in clientaddr;
Int Len = sizeof (sockaddr_in );
M_plistensocket-> Accept (* pclient, (sockaddr *) & clientaddr, & Len );
When sending and receiving data, note the following:

These are only necessary knowledge for the preliminary use of the csocket mechanism. If you want to use it in a multi-threaded or more complex environment, you will face various difficulties, these require us to have a deeper understanding of the internal implementation of csocket, and combine the Win32 socket programming knowledge to deepen our understanding. Well, of course, I certainly won't. -Source code click here: http://download.csdn.net/detail/wudaijun/4911762

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.