C ++ network programming (2)-client server program c ++ network programming (1)

Source: Internet
Author: User

This is a Windows-based client server written in C ++.ProgramSuitable for beginners.Source code must be shared

The idea is as follows. start the server. After the server is started, a subthread is created to send messages to the client. an infinite loop is used to receive client requests. After the client request is successful, the client connection is saved to a collection. The following describes the class for saving client connections. after the client is successfully connected, the server creates a subthread to receive information from the client. The client also creates a subthread to receive information from the server. in this way, the client and the server can communicate. If either party exits, the thread corresponding to the other Party's data reception will automatically terminate.

After exiting a client, the thread that receives data on the server automatically terminates. For example:

When the server saves the client connection set, the corresponding client connection will be deleted, because this delete operation occurs in the Child thread, that is, there will be multiple threads to operate this set, the operations for this set must be thread-safe. there are many methods to ensure thread security. In my blog "multi-thread programming-five ways to achieve thread synchronization", I introduced methods in section 5 to achieve thread synchronization. Here I use key segments, it is worth noting that there must be only one copy of the set for saving client connections. I used a class to encapsulate this set. Every method in this class is thread-safe, there can only be one instance. Here we use the brute force method to set the relevant method to private, and provide a public method to return a static instance of this object, a unique instance.

Classes for saving client connections are as follows:

 //  Clientlist. h stores client requests and can only have one instance # Ifndef _ clientlist_h _  # Define _ Clientlist_h _ # Include <Vector> # Include  "  Csocket. h  "  # Include <Assert. h> Class  Csocket;  Class  Clientlist {  Public  : Typedef Vector <Csocket *>: Iterator ITER;  Void Add (csocket * Socket );  Int Count () Const  ; Csocket * Operator  [] (Size_t index );  Void Remove (csocket * Socket); ITER find (csocket * Socket );  Void  Clear ();  Static Clientlist * Getinstance (){  Static  Clientlist instance;  Return & Instance ;} ~ Clientlist ();  Private  :  Static  Critical_section g_cs;  Static Vector <csocket *> _ List; clientlist (); clientlist (  Const Clientlist &); Clientlist & Operator = ( Const Clientlist & );};  # Endif       
# Include "  Clientlist. h  "  Typedef Vector <Csocket *> : Iterator ITER; clientlist: clientlist () {initializecriticalsection ( & G_cs ); //  Initialize members of g_cs  } Clientlist :: ~Clientlist () {deletecriticalsection ( & G_cs ); //  Delete key segments  }  Void Clientlist: add (csocket * Socket ){  If (Socket! = Null) {entercriticalsection ( & G_cs ); //  Enter key section  _ List. push_back (socket); leavecriticalsection ( & G_cs ); //  Exit key segment }}  Int Clientlist: Count () Const  {  Return  _ List. Size ();} csocket * Clientlist :: Operator  [] (Size_t index) {assert (Index > = 0 & Index < _ List. Size ());  Return  _ List [Index];}  Void Clientlist: Remove (csocket *Socket) {ITER = Find (socket); entercriticalsection ( & G_cs ); //  Enter key section      If (ITER! = _ List. End () {Delete * ITER; _ list. Erase (ITER);} leavecriticalsection ( & G_cs ); //  Exit key segment  } ITER clientlist: Find (csocket * Socket) {entercriticalsection ( & G_cs ); // Enter key section ITER iter = _ List. Begin ();  While (ITER! = _ List. End ()){  If (* Iter = Socket ){  Return  ITER;} ITER ++ ;} Leavecriticalsection ( & G_cs ); //  Exit key segment      Return  ITER ;} Void  Clientlist: clear () {entercriticalsection ( & G_cs ); //  Enter key section      For ( Int I = _ list. Size ()- 1 ; I> = 0 ; I -- ) {Delete _ list [I];} _ list. Clear (); leavecriticalsection ( & G_cs ); //  Exit key segment  } Critical_section clientlist: g_cs; vector <Csocket *> clientlist: _ list;

C ++ network programming (1)

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.