Build a backend server: Service side (blocking)

Source: Internet
Author: User
Tags bind continue current time printf

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/

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.