Problems with recv() function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems with recv() function

Jump to solution
1,986 Views
edgarsevilla
Contributor II

Well, I'm working with TWRK60N512, MQX 3.7. And a netbook with Linux RT.

My embedded system is running as Server (Sockets TCP). It just runs a task:

s32BytesRecv = recv(u32SocketFd, au8BufferRx, MAX_LEN_BUFFER, 0);

if(s32BytesRecv != RTCS_ERROR)

{

    u16MsgCnt++;

}

My netbook is running as client, It sends 1000 messages of 4 bytes length each 50 ms.

The problems is that when the MAX_LEN_BUFFER constant has the value of 4, no message is lost. But if MAX_LEN_BUFFER has a value greater than 4, then the system lose almost 50 % of messages.

Any idea of how to resolve this?

Regards

Edgar


Tags (4)
0 Kudos
1 Solution
975 Views
Martin_
NXP Employee
NXP Employee

I'm afraid this is incorrect usage of the recv() function. recv() function doen't necessarily return after a message. If we look into the RTCS User's Guide, we will see the recv() function returns when one of the following occurs:

-a push flag in the data is received

-buffer is completely filled with incoming data

-receive timeout expires

-immediately after it polls TCP for any data in the internal message buffer (also depends on socket options OPT_RECEIVE_NOWAIT and OPT_RECEIVE_PUSH).


recv() return value != RTCS_ERROR should not be counted as a complete TCP message. For example, I repeat your application scenario, with buffer size 100. On the K60N512 side I receive 4000 bytes (1000 times send() of 4 bytes on PC client side) with 82 recv() returns on MQX TCP Server side.

View solution in original post

0 Kudos
2 Replies
976 Views
Martin_
NXP Employee
NXP Employee

I'm afraid this is incorrect usage of the recv() function. recv() function doen't necessarily return after a message. If we look into the RTCS User's Guide, we will see the recv() function returns when one of the following occurs:

-a push flag in the data is received

-buffer is completely filled with incoming data

-receive timeout expires

-immediately after it polls TCP for any data in the internal message buffer (also depends on socket options OPT_RECEIVE_NOWAIT and OPT_RECEIVE_PUSH).


recv() return value != RTCS_ERROR should not be counted as a complete TCP message. For example, I repeat your application scenario, with buffer size 100. On the K60N512 side I receive 4000 bytes (1000 times send() of 4 bytes on PC client side) with 82 recv() returns on MQX TCP Server side.

0 Kudos
975 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi Edgar,

You could try setting the sockets options OPT_RECEIVE_NOWAIT. If the receive-nowait socket option is TRUE, RTCS immediately copies internally buffered data (up to buflen bytes) into the buffer (at buffer), and recv() returns.

The other option that could help is OPT_RECEIVE_PUSH. If the receive-push socket option is TRUE, a received TCP push flag causes recv() to return with whatever data has been received. If the receive-push socket option is FALSE, RTCS ignores incoming TCP push flags, and recv() returns when enough data has been received to fill the buffer.

Both options are linked with the buffer size you give to the socket. I hope this helps to solve your problem!

Best Regards,
Garabo

0 Kudos