<unistd.h>write(fd,buf,len) [write(2)] question...

<unistd.h>write(fd,buf,len) [write(2)] question...

Post by al.. » Fri, 03 Aug 2001 05:15:48



Hi all,

I'm not all that good with sockets, and I've got to support this legacy
code. The program compiles and runs fine under RedHat 5.2, but when
compiled under RedHat 6.2, it pops errors during transmissions over the
network.

I believe I've isolated the problem to write(2) in one of the sources.

--------->snippet

function()
{
    ...
    .
    .
    {
        ...
        .
        .
        nbytes = write((*association)->networkSpecific.TCP.socket,
                       pdu->presentationDataValue.data,
                       pdu->presentationDataValue.length - 2);

        if ((unsigned long) nbytes != pdu->presentationDataValue.length - 2)
            return COND_PushCondition(DUL_TCPIOERROR)
                               DUL_Message(DUL_TCPIOERROR), strerror(errno),
                                      "writeDataPDU");
    }
    return DUL_NORMAL;

Quote:}

<---------snippet

The problem is that write() returns a value not equal to the supplied
length, and pops an errno of EINPROGRESS. Now, how do I wait until it has
actually completed, and know if it did it successfully? I'm supposing I
should do something with select(2).

Thanks.

 
 
 

<unistd.h>write(fd,buf,len) [write(2)] question...

Post by Casper H.S. Dik - Network Security Engine » Fri, 03 Aug 2001 06:08:24


[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]


>The problem is that write() returns a value not equal to the supplied
>length, and pops an errno of EINPROGRESS. Now, how do I wait until it has
>actually completed, and know if it did it successfully? I'm supposing I
>should do something with select(2).

Yopu shouldn't check errno unless write() returns < 0.

make the write a loop until it has written all bytes.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

 
 
 

<unistd.h>write(fd,buf,len) [write(2)] question...

Post by Nithyanandha » Fri, 03 Aug 2001 09:41:21



> I believe I've isolated the problem to write(2) in one of the sources.

> function()
> {
>         nbytes = write((*association)->networkSpecific.TCP.socket,
>                        pdu->presentationDataValue.data,
>                        pdu->presentationDataValue.length - 2);

>         if ((unsigned long) nbytes != pdu->presentationDataValue.length - 2)
>             return COND_PushCondition(DUL_TCPIOERROR)
>                                DUL_Message(DUL_TCPIOERROR), strerror(errno),
>                                       "writeDataPDU");
>     }
>     return DUL_NORMAL;
> }

> <---------snippet

> The problem is that write() returns a value not equal to the supplied
> length, and pops an errno of EINPROGRESS.

How do you know that write() sets the errno to EINPROGRESS. Did you check the
errno value after your connect() and then after write().

Quote:> Now, how do I wait until it has
> actually completed, and know if it did it successfully? I'm supposing I
> should do something with select(2).

write(fd, buf, len) doesn't mean that the data will be written in one shot. You
have to keep write() in a loop until the complete data is written.

--

Nithyanand.
Siemens, Bangalore, India.
(Opinions expressed are my own and do not reflect the opinions of my employer,
Siemens)

 
 
 

<unistd.h>write(fd,buf,len) [write(2)] question...

Post by al.. » Sun, 05 Aug 2001 05:53:23


Thanks for all the help

 
 
 

1. newbie: write(fd, input, len) and stat struct

i tested reading out of one file a length of 1024 bytes(including
NULL) and then simply writing to another file.  Success.  2 questions
please.

1. Here is my output file.
total 4
-rwxr-xr-x    1 root     root         1024 Dec 24 15:29 file.a
As we can see it's plainly 1024 bytes.  Shouldn't it be 1025 bytes?

2. The stat struct i created and read on the file says, "Error writing
to file.: File name too long" when in fact the file was written.  The
file is simply the character 'a' 1023 times and the NULL.  it was
created by an awk printf that looped 1023 times and closed by the unix
system.  Why is stat giving me this obviously BS answer?

code follows:

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

main(argc, argv)
        int argc;
        char *argv[]; {
                char *filename="file.a";
                char pathname[]="/var/root/werk/file.a";
                char infile[]="/var/root/file.b";
                int len=1024;
                char input[len];
                struct stat fileinfo;
                int fd1, fd2;
                fd1=(creat(pathname, 0777));
                fd2=(open(infile, O_RDONLY));
                read(fd2, input, len);
                close(fd2);
                write(fd1, input, len);
                close(fd1);
                /* fd=open(pathname, O_RDWR, 0777); */
                if (stat(input, &fileinfo) != 0) {
                        perror("Error writing to file. Adieu.");
                        exit(-1);}
                printf("The file %s is %d bytes long.\n", input,
fileinfo.st_size);
                exit(0);

        }

2. where to find tip dialer?

3. write (socket, buf, 0) and read (socket, buf, 0)

4. POP vs IMAP

5. Simple Question - <unistd.h>

6. Dos Jazz drive?

7. in <linux/fs.h> ,the read and write ??

8. displaying arrays in dbxtool

9. Can I write curses-like application without using <curses.h>?

10. <unistd.h> error

11. #include <unistd.h> macro recursion too deep

12. 'gethostname' not found in <unistd.h>

13. Solaris recv(0, buf, len, MSG_PEEK) question