Why add a "block" to the back of the title, because the system creates another event pattern in order to increase concurrency, reduce wait (block), and then introduce a blocking model.
Blocking the server to do can be roughly divided into the following steps:
1. Create a service-side listening connection
2. Generate user connections
3. Receive user requests
4. Send back to User
The code-knocking process is as follows:
To set the listener address and port:
addr_server.sin_family =
af_inet;
Addr_server.sin_port = htons (port);
ADDR_SERVER.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
To create a connection and bind:
Sock_server = socket (af_inet, sock_stream, 0);
Flag = Bind (Sock_server, (struct sockaddr*) &addr_server, sizeof (struct sockaddr)
);
if (flag < 0)
{
printf ("Your bind is not ok\n");
Close (sock_server);
return 0;
}
Start listening:
Flag = Listen (Sock_server, m);
if (flag < 0)
{
printf ("Your listen is not ok\n");
Close (sock_server);
return 0;
}
To receive and generate a user connection:
Sock_client = Accept (Sock_server, (struct
sockaddr*) &addr_client, &size);
if (sock_client <=0)
{
printf ("Your accept is no ok\n");
Close (sock_server);
return 0;
}
Receive User data:
Flag = recv (sock_client, buffer, Recv_buf_len, 0);
if (flag <= 0)
{
printf ("Your recv is no ok\n");
Close (sock_client);
Continue;
}
Validate Data legality:
if (flag!=)
{
printf ("Your recv does follow the protocal\n");
Close (sock_client);
Continue;
}
if (buffer[31] | | buffer[63])
{
printf ("Your recv does follow the protocal\n");
Close (sock_client);
Continue;
}
Send current time to client:
Current = time (0);
Send (Sock_client, (const char*) ¤t, sizeof (time_t), 0);
To close a client connection:
printf ("Your connection is ok\n");
printf ("Now close your connection\n");
Close (sock_client);
This is a simple service-side process, blocking mode under the influence of Io, concurrency is only about 2 K, the previous client program's concurrency is 10000, so the server is not up to, and the server's concurrency is not affected by the number of client processes, indeed very small.
The complete code is here:
Download: http://download.csdn.net/detail/xiaofei_hah0000/5206548
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/zs/