Server Model --- socket !!!

Source: Internet
Author: User

/******************** Server Model **************** **/

1. Cyclic server: the cyclic server can only respond to one client request at a time;

2. Concurrent Server: the concurrent server can request multiple clients at the same time.

**************/

1. UDP server: The implementation of UDP cyclic server is very simple: the UDP server reads a client request from the socket each time, processes the request, and then returns the result to the client. the following algorithm can be used for implementation.

Socket (...);

BIND (...);

While (1) {recvfrom (...); process (...); sendto (...);}

Because UDP is non-connection-oriented, no client can occupy the server. As long as the processing process is not an endless loop, the server will always be able to meet the requirements of each client.

 

2. TCP server: The implementation of the TCP cyclic server is not difficult: the TCP Server accepts a client connection and processes it. After all the requests of the client are completed, the connection is closed.

The algorithm is as follows:

Socket (...);
BIND (...);
Listen (...);
While (1)
{
Accept (...);
While (1)
{
Read (...);
Process (...);
Write (...);
}
Close (...);
}

The TCP cyclic server can only process requests from one client at a time. the server can continue the subsequent request only after all the requests of this customer are met. in this way, if a client occupies the server, other clients cannot work. therefore, the TCP server rarely uses the cyclic server model.

 

/**************** Concurrent Server **************/

1. UDP server (rarely used in practice) People apply the concept of concurrency to UDP and get the concurrency UDP server model. the concurrent UDP server model is actually simple. like the concurrent TCP server model, the algorithm used to create a sub-process is the same as the concurrent TCP model. unless the server takes a long time to process client requests, this model is rarely used.

2. TCP Server

Advantage: the TCP cyclic Server Client exclusively occupies the server;

Disadvantages: create a sub-process that consumes a lot of resources. ++> solution: multiplexing I/O model (see figure 3)

 

To make up for the defects of the cyclic TCP server, people have come up with a concurrent server model. the idea of a concurrent server is that the request of each client is not directly processed by the server, but is created by the server to process the request.

The algorithm is as follows:

 

Socket (...);

BIND (...);

Listen (...);

While (1)

{

Accept (...);

If (Fork (..) = 0)

{

While (1)

{

Read (...);

Process (...);

Write (...);

}

Close (...);

Exit (...);

}

Close (...);

}

3. multiplexing I/O: (this is generally used)

Advantage: Solves the Resource Restriction problem. The UDP loop model is actually used on TCP;

Disadvantage: because the server processes client requests in sequence, some customers may wait for a long time. to solve the system resource consumption caused by the creation of sub-processes, we have come up with a multiplexing I/O model.

First introduce a function select

Int select (INT NFDs, fd_set * readfds, fd_set * writefds,

Fd_set * Records t FDS, struct timeval * timeout) void fd_set (int fd, fd_set * fdset)

Void fd_clr (int fd, fd_set * fdset)

Void fd_zero (fd_set * fdset)

Int fd_isset (int fd, fd_set * fdset)

Generally, when we read and write data to a file, the process may block reading and writing until certain conditions are met.

For example, when we read data from a socket, there may be no data readable in the buffer (the communication peer has not sent the data). At this time, our read call will wait (blocking) until data is readable. if we do not want to block it, one of our options is to use the Select system call. as long as we have set the select parameters, when the file can be read and written, select back "notice" We say it can be read and written. set of all file descriptors to be read in readfds writefds

File descriptor to be notified to us by other Alibaba TFDs servers

Timeout timeout settings.

NFDs adds 1 to the largest file descriptor we monitor.

When we call select, the process will be blocked until one of the following situations occurs. 1) There are files that can be read. 2) There are files that can be written. 3) the time specified for timeout is reached.

To set file descriptors, we need to use several macros. fd_set to add FD to fdset.

Fd_clr clears FD from fdset

Fd_zero clears all file descriptors from fdset.

Fd_isset determines whether FD is in the fdset set;

After Select is used, our server program becomes.

An example of using select

 

Int use_select (int * readfd, int N)

{

Fd_set my_readfd;

Int maxfd;

Int I;

Maxfd = readfd [0];

For (I = 1; I

If (readfd [I]> maxfd) maxfd = readfd [I];

While (1)

{

/* Add all file descriptors */

Fd_zero (& my_readfd );

For (I = 0; I

Fd_set (readfd [I], * my_readfd );

/* Process blocking */

Select (maxfd + 1, & my_readfd, null, null );

/* Something can be read */

For (I = 0; I

If (fd_isset (readfd [I], & my_readfd ))

{

/* It turns out that I can read it */

We_read (readfd [I]);

}

}

}

Server Model --- socket !!!

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.