Mike
Mike

Reputation: 411

How to turn off TCP PSH flag in the POSIX function send()?

I am sending commands to a stepper motor system using Ethernet. The commands get the motor to move, or respond with drive status, or configure the drive, etc... The stepper motor system sometimes hangs, or fails to execute the command, and the manufacturer having looked over everything I supplied has told me to turn off the PSH flag in the TCP layer.

A screenshot of Wireshark showing the use of the PSH flag by my code:

enter image description here

I am using C++11 which is running in Ubuntu (16.04).

My send function looks like so:

int sendStatusCode = send(socket , *&pointerToMessageAsCharArray , sizeOfMessage , 0);

I looked over the writeup for the function, and I understand that the last argument (which I have at 0) is a flag. What I do not understand is the value that I need to plug in to turn off the PSH flag without changing anything else (if that is even possible). Can you help?

Edit The size of the messages is quite small. for example: const char m_ME[5] = {m_HEADER_ONE, m_HEADER_TWO, m_M_HEX, m_E_HEX, m_FOOTER_HEX};

which when sent looks like so: enter image description here

The largest message I am sending is 8 chars which works out to 8 bytes.

EDIT I implemented this answer (How would one disable Nagle's algorithm in Linux?) and it looks to me that TCP_NODELAY does not turn off the PSH flag. The code I used to check:

int noDelayState = 1; // 0 means prefer bandwidth optimization over low latency

int tcpPushOptionOff = setsockopt(m_socketRight
    , IPPROTO_TCP
    , TCP_NODELAY
    ,(char *) &noDelayState
    , sizeof(int));

if (tcpPushOptionOff < 0) { /* do smth */ }

Upvotes: 10

Views: 2238

Answers (2)

Bill
Bill

Reputation: 1

When you send() bytes with your application, TCP completes a segment at the last byte of the send() and sets the PSH flag. This notifies the receiving stack that it should complete the application's recv() early even if it doesn't fill the buffer.

However... receivers who expect a short recv() to mean that was a send() boundary at the sender will flake out unexpectedly when, in practice, that doesn't always turn out to be true.

Upvotes: 0

kert
kert

Reputation: 2281

With standard linux TCP stack you don't have direct control over this. Whether or not the PSH flag gets set is controlled by send buffer and copies in the stack. With something like embedded LWIP stack you have a bit more control.

However, try setting setsockopt(SO_SNDBUF) to be a very small value and see if this makes a difference. Smaller than your packet size, i.e. a few bytes Buffer size setting doesn't work, as minimum size larger than a frame is enforced by OS.

Upvotes: 4

Related Questions