[Reprint] netmsg LAN chat program

Source: Internet
Author: User
Author: Qian Yili
Beijing Institute of Technology Software 2002

Download source code

I wanted to chat like QQ for a long time.ProgramIt has never been time-consuming. In addition, it is difficult to implement multi-point network communication, so this idea is put aside. In the last two months, the school has been busy reading TCP/IP and multithreading books, coupled with the previous basics of C ++ and VC, completed the program in 17 days. It includes the server-side program netmsgserver and the client-side program netmsgclient. Through programming, it is also found that the original socket (such as send and Recv) commands are faster than the cscoket encapsulated by VC. How can we say that the program is not small, with more than 3000 linesCodeThere must be a lot of errors and spam code. After all, the level is limited. thank you if you don't throw any eggs.

I. Description
This program adopts multi-thread programming of TCP/IP socket and implements timeout Control for some important synchronization operations to reduce the waiting time, such as connecting to the server and waiting for the recipient to receive files. Similar to QQ, the basic function enables multi-point chat and point-to-point file transfer. The advantage of this programming is that the transmitted data will not be lost, and the user can immediately respond when going online or offline. The only pity is that the interface is too cumbersome. I have no idea about Interface Programming and can only be used in the LAN for the time being, because no proxy server function is added.

II. General Communication Process

1. netmsgserver Server
It uses two threads, one of which processes the connection request (accept) of each client, and the other receives the command after successful connection request of the client and returns information, this includes user registration, login, deprecation, generation of online user lists, query and modification of information for an online user.

2. netmsgclient
Each netmsgclient is both an information server and an information client, so it can implement multi-point communication.
2.1
Obtain the user number through registration. Send two Commands: "regt \ t \ n", and then send a Data Structure of user data. After successful registration, the server returns an ID number to the user; otherwise, an error code is returned.

 
Send (m_dcsocketclient, (char *) & userinf, sizeof (userinf), 0); // send user information

Where:

 
User_inf userinf;
// Structure of user information
Typedef struct
{
Long ID;
Char nickname [20];
Char sex [10];
Int age;
Char Address [50];
Char password [20];
} User_inf, * lpuser_inf;

2.2
Enable the first thread to log on to the server, and the port is 4000. The following 1000 indicates my ID number. The "User 1000" and "pass password" commands are sent to log on to the server.

2.3
If the login is successful, send the "List \ t \ n" command to the server to obtain the list of online users and display them in the list box. The online list is transmitted as a structure array.

 
Recv (m_dcsocketclient, (char *) (m_onlineuser), sizeof (m_onlineuser), 0 );

Where:

Onlineuser_inf m_onlineuser [max_online_num]; // number of online users
Typedef struct
{
Long ID;
Char nickname [20];
Char IP [16];
Socket S;
} Onlineuser_inf, * lponlineuser_inf;

2.4
Start the second thread to create a message server and accept connection requests from each client. The port is 4001.

 
While (true)
{
Int socklen = sizeof (inetaddr );
If (saccept = accept (slisten, (sockaddr *) & inetaddr, & socklen) = inva pai_socket)
{
Afxmessagebox ("error: accept failed in threadmsg ");
Return 1;
}

Afxbeginthread (threadrecvmsgserver, (lpvoid) saccept );
}

2.5
If the connection is received, the third thread is enabled to accept the information of the other party.

 
Recv (acceptsocket, buff, sizeof (buff), 0)

If I am a user of 1000 and receive the "chat 1001 \ t \ n" command, it means that the user of 1001 wants to chat with me, if I have too many chat users, I will reject 1001 of the chat requests.

 
Sprintf (buff, "% d \ t \ n", reject_chat_req); send (acceptsocket, buff, sizeof (buff), 0 );

If the request is accepted, a confirmation message is sent.

Sprintf (buff, "% d \ t \ n", accept_chat_req); send (acceptsocket, buff, sizeof (buff), 0 );

Next we can communicate with each other.

2.6
If both parties require file transfer, 4th threads are enabled and the port is 4002, which is only a point-to-point communication. If the 1000 idea is to send a file to 1001, the file sender establishes a file server, then, the "file ANC. the AVI \ t \ n command connects 1001 to the file server of 1000 on the message communication line of the other party. After 1001 is connected to the file server of 1000, the system sends a confirmation command to determine whether to receive the file. If it receives the file, it can start to transmit the file.

2.7
Assume that I am a user of 1000 and 1001 has been connected to my machine and chatted with me. If I want to chat with 1002, enable 5th threads to send "chat 1000 \ t \ n" to the 1002 Information Server (Port 4001, each netmsg client has an information server). If you want to chat with 1003, enable the information server that sends commands "chat 6th \ t \ n" from 1000 threads to 1003 to request connection, so that each client can implement multi-point communication.

QQ: 54476167

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.