Build a backend server: client

Source: Internet
Author: User
Tags current time printf sprintf

Part of the protocol request specified in the previous article:

Request

Username/password, the contract username and password each accounted for 32 bytes (the same as the lowest 0)

The client's logic can be designed to:

1. Package the Username,password into the buffer

2. Connect the service side

3. Send Buffer

4. Current time of receiving binary system

5. Show time

The code is as follows:

Service-Side Address Setup section:

addr_server.sin_family = af_inet;  
Addr_server.sin_port = htons (port);  
ADDR_SERVER.SIN_ADDR.S_ADDR = inet_addr (IP);

To create a connection:

Sock_client = 

socket (af_inet, sock_stream, 0);

To connect the service-side code:

Flag = Connect ( 

sock_client, (struct sockaddr*) &addr_server, sizeof (Addr_server));

To set the buffer fill Username/password code:

sprintf (buffer, "%s", "username");  
sprintf (buffer +, "%s", "password");  
      
BUFFER[31]=BUFFER[63] = 0;

Next is send:

Flag = Send (sock_client, buffer, 

0);  
if (flag = =)  
{     
      printf ("Send ok\n");   
}

Receive part of code:

Flag = recv (sock_client, buffer, 0);  
if (flag!= sizeof (time_t))  
{  
       printf ("recv does not follow protocal\n");  
       Close (sock_client);  
       Continue;  
}

Converts the received binary data to a time

memcpy (curtime, buffer, sizeof (time_t) 

);  
struct TM *ptm = localtime (curtime);

Show Time:

printf ("System time:%

04d-%02d-%02d-%02d:%02d:%02d\n", Ptm->tm_year + 1900, Ptm->tm_mon + 1, ptm->tm_ Mday,  
        ptm->tm_hour, Ptm->tm_min, ptm->tm_sec);

Close connection:

printf ("Ok,now We close connection\n");  
Close (sock_client);

In the actual development, in order to pursue concurrent efficiency and enhance the effect of pressure, the client needs to have a loop, in addition to the simultaneous operation of multiple processes.

In the actual test, I found that the client's single process concurrent volume of about 10,000, so open a few more programs, already can meet the demand.

Detailed Code Download address:

Download Address: http://download.csdn.net/detail/xiaofei_hah0000/5205711

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.