writestring return value question

Discussion to talk about software related topics only.
Post Reply
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

writestring return value question

Post by seulater »

I am using " int writestring( int fd, const char *str ); "

the docs say the following:
/**********************************************************************/
/* write data to a file descriptor */
/* returns the number of charactors written on success. */
/* returns a number <0 on failure. */
/* */
/* Note: It is possible for this call to return a number less */
/* than the requested number of bytes. */
/**********************************************************************/

the question that i have is does the above mean that it will return <0 on failure to write to the MAC/PHY or does it mean that it will return <0 when the remote address does not ack back ?

I have 2 MOD boards, call them A & B. B has a SSL server connection running, A is the client. when A boots up it creates a SSL connection to B. every 30 seconds A sends a "alive" packet to B, which when received B responds back. If B does not respond back i want to close the SSL connection on A.


I am looking at the return value i am getting from writestring, and it allways has the value i sent out, regardless if B in on the LAN or if i unplug B's LAN cable.
User avatar
yevgenit
Posts: 84
Joined: Fri Apr 25, 2008 12:47 am
Contact:

Re: writestring return value question

Post by yevgenit »

Behavior of writestring() or any other function, which writes into the TCP socket, is correct. The problem is common misunderstanding , what means "TCP guaranties delivery of data".

The book "Effective TCP/IP Programming by Jon C. Snader, Copyright 2000 by Addison-Wesley.'' (http://home.netcom.com/~jsnader/etcpabout.html) has detail explanation of the topic.

(The following paragraph is my interpretation of the related book fragment.)
In case of no ICMP message about the remote host is unavailable, as in case of unplug the network connector, TCP/IP stack cannot know immediately about the network fail. By the timeout, the transmit TCP will resend the non-acknowledged segments. The process continues, until the transmit TCP will decide, that the delivery is impossible, and the connection will be closed with some system error message. In BSD system, the criteria is 12 failed attempts (approximately 9 minutes).

If you need more fast indication of the network health, the periodic keep-alive messages from the remote host can be monitored by your application.
seulater wrote: ...
I am looking at the return value i am getting from writestring, and it allways has the value i sent out, regardless if B in on the LAN or if i unplug B's LAN cable.
Yevgeni Tunik
Embedded/RealTime software engineer
https://www.linkedin.com/in/yevgenitunik/
________________________
thomastaranowski
Posts: 82
Joined: Sun May 11, 2008 2:17 pm
Location: Los Angeles, CA
Contact:

Re: writestring return value question

Post by thomastaranowski »

This is interesting. It looks like it returns >0 if the socket is open, and the underlying send() call succeeded, which usually always happens if the socket isn't full. Reasons for failure would be:

Socket closed - probably returns 0.
Socket error - Some errno less than 0.

To tell if the remote end received it you need an application level ack message that is sent back in response to your command. A typical method to do this is to send a sequence number in your command, then have the other side echo your command header with the command's sequence number.
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: writestring return value question

Post by seulater »

To tell if the remote end received it you need an application level ack message that is sent back in response to your command. A typical method to do this is to send a sequence number in your command, then have the other side echo your command header with the command's sequence number.
i am doing a simular thing already, i just assumed that when i used the writestring the value returned would be of a successful write and ack.
User avatar
yevgenit
Posts: 84
Joined: Fri Apr 25, 2008 12:47 am
Contact:

Re: writestring return value question

Post by yevgenit »

seulater,

Return value of writestring() or any other function, which writes into the TCP socket, indicates successful transfer of data from the application to TCP of the source host.

If this portion of the data can be transferred to TCP of the destination host (despite temporary network problems like dropped segments or failed default routing), TCP will do it. Else, TCP will return the related system error after a relatively long time.

Even received acknowledge from TCP of the destination host - doesn't mean, that the related portion of the data is arrived into the destination application. For example, the destination application can be non-responding.

As thomastaranowski wrote, if you wish to guarantee the delivery up to the destination application, you need to ask acknowledge from the destination application.
seulater wrote:
To tell if the remote end received it you need an application level ack message that is sent back in response to your command. A typical method to do this is to send a sequence number in your command, then have the other side echo your command header with the command's sequence number.
i am doing a simular thing already, i just assumed that when i used the writestring the value returned would be of a successful write and ack.
Yevgeni Tunik
Embedded/RealTime software engineer
https://www.linkedin.com/in/yevgenitunik/
________________________
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: writestring return value question

Post by seulater »

Got it. I would think that there would be another option for it as well. (there probably is, i just have not read enough to find it)
i would like to be able to use writestring, and when the data is sent and the ack has come back the value returned is true for a good send and false for bad.
Post Reply