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/