USR-TCP232-302 User Manual
h.usriot.com
Jinan USR IOT Technology Limited
51 / 53
14.
char recvbuf[RECV_BUF_SIZE] = {0};
15.
char sendbuf[SEND_BUF_SIZE] = {0};
16.
int recvlen = 0;
17.
int retlen = 0;
18.
int sendlen = 0;
19.
int leftlen = 0;
20.
char *ptr = NULL;
21.
struct sockaddr_in ser_addr;
22.
memset(&ser_addr, 0, sizeof(ser_addr));
23.
ser_addr.sin_family = AF_INET;
24.
inet_aton("127.0.0.1", (struct in_addr *)&ser_addr.sin_addr);
25.
ser_addr.sin_port = htons(SERVER_PORT);
26.
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
27.
if(sock_fd < 0)
28.
{
29.
syslog(LOG_ERR, "%s:%d, create socket failed", __FILE__, __LINE__);
30.
exit(1);
31.
}
32.
if(connect(sock_fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) < 0)
33.
{
34.
syslog(LOG_ERR, "%s:%d, connect socket failed", __FILE__, __LINE__);
35.
exit(1);
36.
}
37.
//receive data
38.
recvlen = 0;
39.
retlen = 0;
40.
ptr = recvbuf;
41.
leftlen = RECV_BUF_SIZE -1;
42.
//do
43.
{
44.
retlen = recv(sock_fd, ptr, leftlen, 0) ;
45.
if(retlen < 0)
46.
{
47.
if(errno == EINTR)
48.
retlen = 0;
49.
else
50.
exit(1);
51.
}
52.
r= retlen;
53.
leftlen -= retlen;
54.
ptr += retlen;
55.
}
56.
//while(recvlen && leftlen);
57.
printf("receive data is : %s", recvbuf);
58.
sprintf(sendbuf, "hello server/n");