[comp.unix.questions] non-blocking sockets

ycy@walt.cc.utexas.edu (Joseph Yip) (04/12/90)

I am writing some socket programs. Everything works fine except when I
want my socket to be NON-BLOCKING.

I use,

	/* to read a connected socket */
	read(socket_id,buf,BUFS);
	
Does anyone know how to read from a socket using non-blocking mode? 
recv() system call has a flag that you can PEEK at the socket. 
Do I have to go through fcntl() call to make it non-blocking?

Another question is that if I am transferring 512*512 image file using
socket,

	/* send 512*512 image file */
	write(socket_id,buf,512*512);

	......
	------------------------------------

	/* at the receiving end */
	read(socket_id,buf,512*512);

Will the receiving end receive all 512*512 bytes before returning?
What is the optimal size to send using socket?

Thank you

Joseph Yip

brnstnd@stealth.acf.nyu.edu (04/15/90)

In article <28080@ut-emx.UUCP> ycy@walt.cc.utexas.edu (Joseph Yip) writes:
> I am writing some socket programs. Everything works fine except when I
> want my socket to be NON-BLOCKING.

If the file descriptor may be shared between processes, none of the
techniques for non-blocking I/O mentioned in other messages will work,
because they use fcntls that are per-fd rather than per-process. Under
BSD 4.3 you can use siginterrupt() and signals to customize the amount
of blocking to your taste, whether you're using sockets or not. This is
illustrated by multitee, appearing soon in a source group near you.

If you want asynchronous I/O---i.e., nonblocking I/O plus threads---you
can fork off separate processes, use select/signal/SIGALRM creatively,
or do whatever else suits your taste.

---Dan