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/