// Perform receiving size check and receiving processing simultaneously
rbuf_idx = 0; // Receive data storage head index initialization
recv_size = 0; // Initialize the number of receive data
while(1) {
length = recv(socketno, (char*) (&r_buf[rbuf_idx]), (BUF_SIZE - rbuf_idx), 0);
// Response data receiving
if(length == 0) { // Is connection cut off?
Sockerror(ERROR_RECEIVE); // Error handling
return (SOCK_NG);
}
if(length == SOCKET_ERROR) {
nErrorStatus = WSAGetLastError();
if(nErrorStatus != WSAEWOULDBLOCK) {
Sockerror(ERROR_RECEIVE); // Error handling
return (SOCK_NG);
} else {
continue; // Repeat until messages are received
}
} else {
rbuf_idx += length; // Update the receive data storage
// position
recv_size += length; // Update the number of receive data
if(recv_size >= RECV_ANS_1) // Have all response messages been
// received?
break; // Stop repeating as messages have
// been received
}
}
r_buf[rbuf_idx] = '\0' ; // Set NULL at the end of receive data
printf("\n receive data\n%s\n",r_buf);
// strcpy((char *)(s_buf), "01FF000A4420000000000500"); // D0 to D4 batch read request
// (1E frame)
strcpy((char *)(s_buf), "500000FF03FF000018000A04010000D*0000000005");
// D0 to D4 batch read request
// (3E frame)
length = strlen((char *)(s_buf));
if(send(socketno, (char *)(s_buf), length, 0) == SOCKET_ERROR) {
// Data sending
Sockerror(ERROR_SEND); // Error handling
return (SOCK_NG);
}
printf("\n send data\n%s\n",s_buf);