Multi-User LAN communication based on TCP/IP (4)

Source: Internet
Author: User

Iii. Implementation

3.2 Communication Server

● Multithreading

Each time a user successfully connects to the server, the server creates two threads for it: the receiving thread (RecvData) and the sending thread (SendData), and the receiving thread is executable after creation, the sending thread is blocked, waiting for the server to wake up. Both threads execute an infinite loop process. Only when an exception occurs in communication or the client closes the connection can the thread be terminated by itself, and the two threads must be generated at the same time, end at the same time. Obviously, each connection generates two threads, making data forwarding simple, but at the same time increasing server tasks. Therefore, the number of client connections is limited, depending on the server's software and hardware capabilities.

At the same time, because multithreading requires operations on the struct info_data, threads must be synchronized. Here, I have defined the mutex CMutex m_mutex and used its methods Lock () and Unlock () to complete synchronization.

Let's first look at the receiving thread (RecvData): (incomplete code)

UINT RecvData (void * cs)
{
SOCKET clientSocket = (SOCKET) cs;
While (1)
{
Numrcv = recv (clientSocket, buffer, MAXBUFLEN, NO_FLAGS_SET );
Buffer [numrcv] =;
If (strcmp (buffer, "Close! ")! = 0)File: // NoIs the "Close" data received
{
............
For (I = 0; I {
If (po! = NULL)
{
S1 = s_info.GetNext (po );
If (s1.pet. Compare (petname) = 0)File: // RatioIs the nickname the same?
{
M_mutex.Lock ();File: // mutualLock
Info_data.data = pos;
Info_data.thread = s1.thread;
M_mutex.Unlock ();File: //Lock
}
S1.thread-> ResumeThread ();File: // restoreResend the corresponding thread
Break;
}
}
}
Else
{
............
If (clientSocket = s1.s _ client)
{
M_mutex.Lock ();File: // mutualLock
Info_data.data = buffer;
M_mutex.Unlock ();File: //Lock
S1.thread-> ResumeThread ();File: // restoreResend the corresponding thread
S_info.RemoveAt (po1 );File: // DeleteExcept this user information
Break;
}
.........
Goto aa;
}
}
Aa: closesocketlink (LPVOID) clientSocket );File: // closeClosed connection
AfxEndThread (0, true );File: // closeBundle thread
Return 1;
}

Next let's take a look at the sending thread (SendData): (incomplete code)

UINT SendData (void * cs)
{
SOCKET clientSocket = (SOCKET) cs;
While (1)
{
If (info_data.data! = "Close! ")
{
M_mutex.Lock ();File: // mutualLock
Numsnd = send (clientSocket, info_data.data,
Info_data.data.GetLength (), NO_FLAGS_SET );File: // sendSend data
Now = info_data.thread;
M_mutex.Unlock ();File: //Lock
Now-> SuspendThread ();File: // fromHanging
}
Else
{Goto bb ;}

}
Bb: closesocketlink (LPVOID) clientSocket );File: // closeClosed connection
AfxEndThread (0, true );File: // closeBundle thread
Return 1;
}


 

3.3 Client

Obviously, the user end does not need to consider multithreading, and the network connection technology is also relatively mature. Therefore, there is no difficulty in communication. However, the user end is intended for actual users, so both interfaces and functions must be friendly. Like most software updates, the improvement of interface friendliness and functional perfection are often at the top of the list. It can be seen that from the perspective of overall design and technical implementation, the workload on the user side is very large, and the design is much more complicated than that on the server side. I have summarized the following:

● Compatible with the server communication format;

● Simple and easy to use, with beautiful interfaces and shortcut keys;

● Accurately receive and transmit data;

● All data recording and extraction functions;

● Multiple message receiving and prompting methods, such as the flickering Tray Icon (the sender's profile picture) and sound prompt;

Based on the above content, I designed three independent dialog boxes to complete registration, login, and communication functions. The login and registration dialog boxes are connected to the server's 56789I/O port, the Communication dialog box is connected to the 56790I/O port of the server, which isolates registration, login, and communication. This reduces the server load and ensures communication security.

As this part is not the main content, for detailed code, see the program.

Iv. Conclusion

Through the above description, we can know that the system is divided into server and client, and the server is divided into registration/login server and communication server, the forwarding function of the communication server enables multi-user communication in the LAN. This article uses multi-thread technology and shared data structure technology to implement the forwarding function of communication servers, so that network applications based on TCP/IP are generally developed. The system has run through the LAN (one server and 20 clients) in our lab.

Click to download source code

References:

[1] Eugene Olafsen, Kenn Scribner, K. David White, etc. MFC Visual C ++ 6.0 Programming Technology insider. Beijing: Machinery Industry Press 2000.2
[2] Charles Wright. Visual C ++ programmers. Beijing: China Water Conservancy and hydropower press 2001.10

Author information: Yuan (Department of machinery, East China Shipbuilding Institute, Zhenjiang 212003, Jiangsu)

Related Article

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.