writev() on a SOCK_STREAM socket

K

kid joe

Hi,

Do I need to retry writev() on a blocking Unix-domain SOCK_STREAM socket,
or will it always write out the exact number of bytes I asked for?

For example:

if (write(fd, (const char *) presp, resp_size) != resp_size)
{
warn("Writing %lu bytes failed\n", resp_size);
return;
}

this seems to work, but I want to know if it's "really OK" in terms of
ANSI Standards etc.
 
F

Flash Gordon

pete wrote, On 20/06/08 00:01:
The ANSI C standards have nothing to do with
write(), writev(), Unix, and sockets.

As this appears to be about Uni like systems comp.unix.programmer would
be a good place to ask. I suspect the answer is "no" but I could easily
be wrong.
 
J

Jens Thoms Toerring

kid joe said:
Do I need to retry writev() on a blocking Unix-domain SOCK_STREAM socket,
or will it always write out the exact number of bytes I asked for?
For example:
if (write(fd, (const char *) presp, resp_size) != resp_size)
{
warn("Writing %lu bytes failed\n", resp_size);
return;
}
this seems to work, but I want to know if it's "really OK" in terms of
ANSI Standards etc.

The C standard won't tell you anything about this problem since
there's not even a write() function (or an open() function to
start with;-) defined there. But even if you ask in e.g. the
comp.unix.programmmer group you will have to tell how exactly
you did open the file (descriptor). If you e.g. did open it in
non-blocking mode then for sure write() may return before even
a single byte was written. Results may also depend on signals
being blocked etc. - the POSIX standard is what you need to refer
to in this case.
Regards, Jens
 
R

rahul

Hi,

Do I need to retry writev() on a blocking Unix-domain SOCK_STREAM socket,
or will it always write out the exact number of bytes I asked for?

For example:

if (write(fd, (const char *) presp, resp_size) != resp_size)
{
warn("Writing %lu bytes failed\n", resp_size);
return;
}

this seems to work, but I want to know if it's "really OK" in terms of
ANSI Standards etc.

This topic is not about ANSI but POSIX standards.
<off-topic>
You asked about writev and you are using write. Nevertheless, the same
errors are defined for both. write can fail in various cases: EPIPE(if
pipe is broken), EINT(if interrupted by a signal) and EAGAIN ( if fd
opened as O_NONBLOCK and the call would block). There are a few more
error scenarios which the man pages document. Its always a good idea
to consult the man pages(or any equivalent documentation).
</off-topic>
 
A

Antoninus Twink

rahul said:
write can fail in various cases: [snip good answer]
There are a few more error scenarios which the man pages document.
Its always a good idea to consult the man pages(or any equivalent
documentation).

Most here prefer that you only refer the enquirer to a suitable
newsgroup, rather than attempt to answer off-topic questions.

s/Most/A vocal minority/
The reason is that the people to criticize your answer just don't
exist here, so faulty answers won't get exposed.

This is materially false: the intersection between posters to clc and
posters to cup is large, and might well be larger still if it wasn't for
the topicality zealots stifling POSIX discussions here.

In any case, the answer is a precis of formal documentation (the write
manpage), so it's very unlikely to be "faulty".
And we all make mistakes.

Yes - some more than others.
 
V

vippstar

rahul wrote:

... snip ...


Most here prefer that you only refer the enquirer to a suitable
newsgroup, rather than attempt to answer off-topic questions. The
reason is that the people to criticize your answer just don't exist
here, so faulty answers won't get exposed. And we all make
mistakes.
Agreed. I posted a follow-up to comp.unix.programmer only, advicing
rahul that, along with correcting his reply (and answering OP).
I hope the discussion continues there...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top