Talk C Chestnut Bar (160th time: C language Instance-socket communication model one)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the example of socket communication based on af_inet Domain datagram, this time we say the example is the socket communication model . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we mentioned in the previous Zhanghuizhong the number of communications, just said can set the number of communications, and did not do a detailed introduction, today we look at how to set the number of communications, as well as with its extension: communication model.

Iterative communication model

Crossing, the control of the number of communications is mainly reflected in the server side, the following is the core code:
Flow Socket Communication

    while(index2)    {        accept(server_fd,NULL,NULL);        //readsocket        read(client_fd,buf,BUF_SIZE);    //closesocket        close(client_fd);    }

Datagram Socket Communication

    while2)    {        res = recvfrom(server_fd,buf,BUF_SIZE,0,(struct sockaddr *)&client_address,&addr_len);    }

From the code above, we can see that the number of times in the while loop is (index<2) limited to communication, if we write a dead loop: that while(1); is infinite communication. The number of communications that we have said, we extend it, that is, the topic we are going to introduce today: communication model.

As you can see, the operation of the data in the communication process is carried out in the while loop, only the data in the current communication process is completed before entering the next communication process. From the server's point of view, the server can only communicate with one client at a time. From the client's point of view, only one client can communicate with the server, and the other client needs to wait for the current client to complete communication before communicating with the server side. That is to say, multiple clients are in turn communicating with the server side, and we give this communication a name called: iterative communication model.

Concurrent Communication model

Next we introduce another type of socket communication model. First, let's modify the core code as follows:
Flow Socket Communication

    //线程执行函数    *thread_func*param)    {           //readsocket        read(client_fd,buf,BUF_SIZE);        //closesocket        close(client_fd);    }    while(1)    {        accept(server_fd,NULL,NULL);        *)param);    }

Datagram Socket Communication

    //线程执行函数    void *thread_func(void *param)    {        res = recvfrom(server_fd,buf,BUF_SIZE,0,(struct sockaddr *)&client_address,&addr_len);    }    while(1)    {          res = pthread_create(&thread_value,NULL,thread_func,(void *)param);    }

From the code above, we can see that we have introduced threads and set the number of communications to infinite times, and of course, it is possible to write as many as two communications in an iterative model. Let's focus on the changes in the communication process.

The operation of the data in the communication process is in the thread function, in the while loop is mainly to accept the connection request from the client, and create a new thread, after the creation of a new thread, the next loop, the operation of the data during the communication process by the thread called its thread function to complete. At this point, if a client sends a connection request, the server creates a new thread.

From a server perspective, the server can communicate with multiple clients. From the client's point of view, multiple clients can communicate with the server at the same time, and they do not need to wait for other client communication to complete communication to communicate with the server side. In other words, at the same time, multiple clients can communicate with the server side simultaneously. We've given this communication a name: the concurrent communication model .

Crossing, we do not write detailed code, we can in front of us "four dishes" in the code to make changes, it is difficult to believe that you can complete.

You crossing, for example of a socket communication model Let's talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (160th time: C language Instance-socket communication model one)

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.