Hi Guys,
I used my AM3359 ICE V2 board as a TCP server(MODBUS TCP SERVER) to communicate with a client. The server can get the command from client but every time the client side gives an error message "Read TCP Header Failed". Two questions here:
1)Can some one tell me what is the problem here, why the client gives the error message?
2) if the client read tcp header failed, whether it will resend the same command again or not?
Thanks and have a good day!
Software I used:
Sys_BIOS, NDK;
Hardware:
AM3359 ICE V2
Here is PART of my test code:
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if( s == INVALID_SOCKET )
{
printf("failed socket create (%d)\n",fdError());
if( pBuf )
mmBulkFree( pBuf );
if( s != INVALID_SOCKET )
fdClose( s );
}
/* Prepare address for connect */
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_addr.s_addr = address; // 192.168.1.2
//sin1.sin_port = htons(7);
sin1.sin_port = htons(502); // modbus tcp port is 502
if (bind (s, (PSA) &sin1, sizeof (sin1)) < 0) {
printf("failed bind (%d)\n",fdError());
if( pBuf )
mmBulkFree( pBuf );
if( s != INVALID_SOCKET )
fdClose( s );
}
/* listen socket */
if ( listen( s, 5) < 0 )
{
printf("listen failed(%d)\n",fdError());
if( pBuf )
mmBulkFree( pBuf );
if( s != INVALID_SOCKET )
fdClose( s );
}
else
{
printf("listen correct\n");
while(1){
struct sockaddr_in clisinaddr;
int clilen = sizeof(clisinaddr);
SOCKET clisok;
printf("before the accept test is: %d\n", clilen);
clisok = accept(s, (PSA) &clisinaddr, &clilen);
printf(" test value is: %d\n", clilen);
if(clisok == INVALID_SOCKET){
printf("accept error %d\n", fdError());
if( pBuf )
mmBulkFree( pBuf );
if( s != INVALID_SOCKET )
fdClose( s );
}
else {
printf("accept correct\n");
printf("===== received data ====\n");
gotdatabyte = recv( clisok, pBuf, 50, MSG_PEEK );
printf("the received data is: %d byte\n", gotdatabyte);
if( gotdatabyte < 0 )
{
printf("recv failed (%d)\n",fdError());
// goto leave; //break;
}
for (j = 0; j< gotdatabyte; j++){
printf("received data byte %d is: %d\n", j, *(pBuf+j));
}
ProcessReceivedMessage(); // process the command, in this function changed the pBuf, the size will be different from the original received pBuf
// send the response to client
if( send( client_socket, pBuf, 10, 0 ) < 0 )
{
printf("send failed (%d)\n",fdError());
}
mmZeroInit( pBuf, (uint)MODBUS_RX_BUFFER_SIZE);
fdClose( client_socket );