Large concurrent Server Prerequisite technology: Connect asynchronous

Source: Internet
Author: User
Tags epoll

Before we talk about most of the server and the client side of the technology, the development of servers in fact sometimes involves access to the server, such as Tencent's Pat server needs to know the login member information, you need to visit the member server.

Cross-service access will involve a lot of technology, such as access control, data synchronization and so on, here mainly to learn about the transport layer.

To make it easier to understand, we call the access-side server the client, and the access-side server is called the service side.

The process by which a client initiates a connection:

SOCKET_FD = socket (af_inet,sock_stream,0);  
ret = Connect (SOCKET_FD, (sockaddr*) &addr, sizeof (addr));

The problem is in the second step, connect if it is synchronized, if the service end is busy, the client will always be blocked here, the most likely will be spent a few 10 seconds, joking, not it, the client is also a server alas, this piece is not become a bottleneck.

Let's sort out the idea:

Like you and others girl vindicate, the girl said today not to tell you the answer, tomorrow call to tell you, you are not to eat not to drink in her house downstairs wait, or go back to take a good rest waiting for her phone.

Do you understand, I suggest that we go home to rest, because you have a lot of important things to do.

The same idea, the client needs to find a way to put this errand into a notification queue.

The solution to this problem is to set the SOCKET_FD to asynchronous (nonblocking) before connect:

Fcntl (SOCKET_FD, F_SETFL, Fcntl (S, F_GETFL) | O_nonblock);

If the connection is not connected at the moment, connect return-1, good boy, other girls shy, but do not say no.

That's OK, wait for the phone at home, first to save the girl number.

This is where we put the SOCKET_FD in the notification queue, taking Epoll as an example,

struct epoll_event ee;  
ee.events = Epollout | Epollet;  
EE.DATA.FD = SOCKET_FD;  
Epoll_ctl (EPFD,  epoll_ctl_add, SOCKET_FD,  &ee);

Now do what you have to do.

The phone came, and then do the corresponding treatment, should be happy or sad, expression function prepared, lest the brain vacuum.

The difference is that if the girl tells you she promised you that it's over, and Epoll callback tells you there's an incident, you need to confirm:

GetSockOpt (SOCKET_FD, Sol_socket, So_error, &error, (socklen_t *)

&len);  
if (!error)  
{  
//ok  
}

Alternatively, you can use GetSockName or getpeername to confirm that the connection is successful by confirming OK.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/zs/

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.