04-09-2007
read/write socket error
I have client and server connected.
client write and read from csock.
server write and read from ssock
suppose the server does :
....
close(ssock); //send FIN to client
othertask();
....
READ ERROR
if after the server close() the client does:
...
read(csock,...);
...
read(csock,...);
...
close(csock,...);
...
read(csock,...);
...
first read return 0 (read FIN)
second read also return 0.
third read return -1 with errno set to EBADF.
QUESTION 1:
Indeed if after the server close() the client does:
....
write(csock, "hello", 6); //server send RST
...
read(csock,...);
...
write(csock, ...);
first write return 6 without signaling nothing.
read return:
0 if the client read before the RST arrives.
-1 with errno set to ECONNRESET otherwise.
second write comports the SIGPIPE signal.
if it is ignored write return -1 and errno=EPIPE, otherwise it
comports the client termination.
is ok?
IS THE SITUATION THE SAME WHEN THE SERVER CRASH BEFORE THE CLIENT'S
FIRST WRITE?
QUESTION 2:
when read fails with errno=ENOTCONN?
QUESTION 3:
In general if I don't set the timeout option, is possible that a
read() fails with errno=ETIMEDOUT?
WRITE ERROR
when write fails and errno =ECONNRESET??
10 More Discussions You Might Find Interesting
1. Programming
I am presently refactoring a windows deamon with an eye towards porting it to linux someday.
Presently the application uses a single background thread and asynchronous socket I/O to implement FTP and HTTP clients in a single switch statement (2000 lines and 100 cases just for the switch... (3 Replies)
Discussion started by: siegfried
3 Replies
2. IP Networking
Hi Friends,
I have written a program which will listener for more than 1000 requests per second from a single socket descriptor and then it will process those requestes. Its taking X amount of time. Now i want to reduce that time. Will I can write multiple threads to receive the... (2 Replies)
Discussion started by: pa.chidhambaram
2 Replies
3. Shell Programming and Scripting
I need to find all the files that have group Read or Write permission or files that have user write permission.
This is what I have so far:
find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}'
It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies
4. IP Networking
Hi,
We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies
5. Programming
Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct...
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include... (3 Replies)
Discussion started by: cyler
3 Replies
6. Programming
Dear Experts,
i am compiling my code in suse 4.1 which is compiling fine,
but at runtime it is showing me for socket programming error no 88
as i searched in errno.h it is telling me socket operation on non socket,
what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies
7. Programming
Can anyone tell what happens if each end writes at the same time on the same socket ?
- if one of them issues a read() after write() has completed, will it record into the buffer what the other sent ?
ex.
e1 writes to e2 - - - while - - - e2 writes to e1 (at the same time)
e1 read () - what... (1 Reply)
Discussion started by: gendaox
1 Replies
8. Programming
How to know whether socket is ready for write.
select(maxfds, (fd_set *)NULL, &writefds, NULL, &timeout);
By default socket is set for write without checking whether it would block or not? If so how do I know my FD is ready for writing. (3 Replies)
Discussion started by: satish@123
3 Replies
9. Shell Programming and Scripting
Hello all,
I have a requirement to read and write to a tcp socket from an HP-UX shell script. I see a /dev/tcp character device on my servers:
crw-rw-rw- 1 root root 72 0x00004f Mar 28 18:37 /dev/tcp
So I believe this is what I should use. The problem is that all the... (2 Replies)
Discussion started by: lupin..the..3rd
2 Replies
10. Programming
hello
I have a C program and I want to read data from a socket ,and especially I want a client to send a .jpg to the server.The problem I face is that no matter what the size of the photo is,my read command seems to read an amount of data and then does nothing.
while(1){
... (4 Replies)
Discussion started by: vlm
4 Replies
READ(2) Linux Programmer's Manual READ(2)
NAME
read - read from a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
DESCRIPTION
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified.
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not
an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually avail-
able right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read()
was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the
file position (if any) changes.
ERRORS
EAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block.
EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001
allows either error to be returned for this case, and does not require these constants to have the same value, so a portable appli-
cation should check for both possibilities.
EBADF fd is not a valid file descriptor or is not open for reading.
EFAULT buf is outside your accessible address space.
EINTR The call was interrupted by a signal before any data was read; see signal(7).
EINVAL fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address
specified in buf, the value specified in count, or the current file offset is not suitably aligned.
EINVAL fd was created via a call to timerfd_create(2) and the wrong size buffer was given to read(); see timerfd_create(2) for further
information.
EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling tty,
and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O
error while reading from a disk or tape.
EISDIR fd refers to a directory.
Other errors may occur, depending on the object connected to fd. POSIX allows a read() that is interrupted after reading some data to
return -1 (with errno set to EINTR) or to return the number of bytes already read.
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001.
NOTES
On NFS file systems, reading small amounts of data will only update the timestamp the first time, subsequent calls may not do so. This is
caused by client side attribute caching, because most if not all NFS clients leave st_atime (last file access time) updates to the server
and client side reads satisfied from the client's cache will not cause st_atime updates on the server as there are no server side reads.
UNIX semantics can be obtained by disabling client side attribute caching, but in most situations this will substantially increase server
load and decrease performance.
Many file systems and disks were considered to be fast enough that the implementation of O_NONBLOCK was deemed unnecessary. So, O_NONBLOCK
may not be available on files and/or disks.
SEE ALSO
close(2), fcntl(2), ioctl(2), lseek(2), open(2), pread(2), readdir(2), readlink(2), readv(2), select(2), write(2), fread(3)
COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2009-02-23 READ(2)