read/write socket error


 
Thread Tools Search this Thread
Top Forums Programming read/write socket error
# 1  
Old 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??
# 2  
Old 04-12-2007
Sockets and errors

Here are some examples of when these errors occur:
write(..) on a socket that has been closed at the other end will cause a SIGPIPE.

read on a remotely closed socket will return 0 (EOF) or ECONNRESET in some cases, see the last part of this reply.

read(..) or write(..) on a socket that was never connected will return ENOTCONN.

read(..) or write(..) on a locally closed socket will return EBADF

I'm referring to [comp.unix.programmer] Unix-socket-faq for network programming when answering the question regarding read returning 0 or ECONNRESET:

"If the peer calls close() or exits, without having messed with
SO_LINGER, then our calls to read() should return 0. It is less clear
what happens to write() calls in this case; I would expect EPIPE, not
on the next call, but the one after.

If the peer reboots, or sets l_onoff = 1, l_linger = 0 and then
closes, then we should get ECONNRESET (eventually) from read(), or
EPIPE from write()."

/Kristofer
# 3  
Old 04-17-2007
Quote:
close(csock,...);
...
read(csock,...);
...
close() closes the file descriptor hence your EBADF for the following read.

As a suggestion, once you use close, actually or mentally put -1 into the file descriptor variable. This also acts as a massive hint that the file descriptor is no longer valid.

Did you really mean "shutdown()" ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

read from socket

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

2. Shell Programming and Scripting

Read and write to tcp socket

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

3. Programming

write on Non Blocking Socket

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

4. Programming

Write-Write on a socket

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

5. Programming

Error with socket operation on non-socket

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

6. Programming

socket function to read a webpage (socket.h)

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

7. IP Networking

read/write,write/write lock with smbclient fails

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

8. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

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

9. IP Networking

Can we write a multiple thread to receive from a single socket file descriptor

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

10. Programming

How to Write Linux Friendly Async Socket I/O

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
Login or Register to Ask a Question