Author: Qian Yi force Beijing University School of Software Research 2002
Download source code has long wanted to be a chat program similar to QQ. It has not been there. There is no time, and then add more difficult communication, so this idea is on the side. In the last two months, the school has been bored with some TCP / IP, multi-threaded books, plus the foundation of the previous C and VC, and started to do this. It includes server-side program NETMSGSERVER and client programs NETMSGCLIENT. The original Socket (like Send, Recv, etc.) command is also discovered by programming. How to say, the program is not small, there are more than 3,000 lines of code, the wrong and garbage code is definitely a lot, after all, the level is limited, everyone will see not to throw eggs, thank you. First, this program uses TCP / IP Socket multi-threaded programming, and makes timeout control for some important synchronization operations to reduce waiting time, like connecting to the server, waiting for the other party to receive files. Basic functions are similar to QQ, which can make multicast chat and point-to-point file transfer. The advantage of this programming is that the data transmitted will not be lost, and the user can go offline to react now. The only regret is that the interface is too too, I am in connection with interface, and I can only use it in the LAN because there is no increase in the functionality of the proxy server. Second, the approximate communication process 1, the NetMsgServer server side uses two threads, one of the threads handles the connection request for each client, and the other is processed after the client requests the connection to process and return information, including the user Register, log in, offline, generate online users list, query the information of a online user, etc. 2, NETMSGCLIENT client Each NetMsgClient client is both an information server side and an information client, so a multi-point communication can be implemented. 2.1 Get user numbers by registration. Send two commands: "regt / t / n", then send a user data data structure. The registration success server returns a ID number to the user, otherwise returns an error code.
Send (m_dcsocketclient, (char *) & usrinf, sizeof (userinf), 0); // Send user information
among them:
User_INF Userinf;
// user information structure
Typedef struct
{
Long id;
Char nickname [20];
Char sex [10];
Int agec;
CHAR Address [50];
Char Password [20];
User_INF, * LPUSER_INF; 2.2
Turn on the first thread login server, the port is 4000. The following 1000 indicates that my ID number, send "User 1000" and "Pass Password" commands to log in to the server.
2.3
If the login is successful, send the "list / t / n" command to the server side, acquire the list of online users, and appear in the list box. The line list is transmitted in the situation of the structure array.
Recv (m_dcsocketclient, (char *) (m_onlineuser), sizeof (m_onlineuser), 0);
among them:
Onlineuser_inf m_onlineuser [max_Online_num]; // Online User Architecture
Typedef struct
{
Long id;
Char nickname [20];
Char ip [16];
Socket S;
} Onlineuser_inf, * lponlineuser_inf; 2.4 The second thread is turned on to create a message server side, accept the connection request of each client, and the port is 4001.
While (True)
{
INT Socklen = SizeOf (inetaddr);
IF ((SACCEPT = Accept (Slisten, (SockAddr *) & inetaddr, & socklen) == Inva Lid_socket
{
AfxMessageBox ("Error: Accept Failed in ThreadMSG");
Return 1;
}
AfxBeginthread (ThreadRecvmsgServer, (LPVOID) SACCEPT
} 2.5
If you receive a connection, open the third thread to accept the other party.
Recv (Acceptsocket, Buff, Sizeof (BUFF), 0)
If I am a 1000 user, I accept "chat 1001 / t / n" instruction, which is 1001 this user wants to chat with me. If my chat is too much, I refuse 1001 chat request.
Sprintf (buff, "% d / t / n", reject_chat_req); send (Acceptsocket, Buff, Sizeof (BUFF), 0);
If you accept his request, send confirmation information.
Sprintf (buff, "% d / t / n", accept_chat_req); Send (Acceptsocket, Buff, Sizeof (BUFF), 0);
Next, you can communicate. 2.6 If the two parties also ask for the file, turn on the 4th thread, the port is 4002, just point-to-point communication, if the 1000 idea sends the file to 1001, the file sender creates the file server side, then by the message communication line (ie above Acceptsocket) Send the "File Anc.avi / T / N" command to the other party message communication line, let the 1001 connection 1000's File file server. 1001 After connecting to a 1000 File server, send a confirmation command to determine whether the file is received, and if the received can start the file. 2.7 Assumption I am 1000 users, 1001 has been connected to my machine, and chat with me, if I still want to chat with 1002, open the 5th thread to send "Chat 1000 / T / N" to 1002 information The server side (port 4001, each NETMSG client has an information server side), if you still want to talk to 1003 chat, open the sixth thread send command "CHAT 1000 / T / N" to 1003 information server-side request connection, This allows each client to achieve multi-point communication. QQ: 54476167