/*************************************************************************** * * socket.c - routine for socket communcation * *************************************************************************** */ #define EXTERN /*-------------------------- * Standard include files *-------------------------- */ #include #include #include #include #include #include #include #include #include #include #include #if SUNOS | LINUX #include #endif #if LYNX #include #endif /*----------------------------- * Non-standard include files *----------------------------- */ #include "addlib.h" #include "errorlib/error_msg.h" /*----------------------------------------------------------------------- ** sock_write_data() - writes bufsize number of bytes at location ** buf at file description sock. ** return: -1 error writing to sock. ** >0 number of long int written. **----------------------------------------------------------------------- */ #if ANSI long sock_write_data( int fd, char *buf, long bufsize, int blocking ) #else long sock_write_data( fd, buf, bufsize, blocking ) int fd; /* Socket's file descriptor */ char * buf; /* Pointer to buffer area */ long bufsize; /* number of byte to send */ int blocking; /* specify blocking (TRUE) or non-blocking (FALSE) */ #endif { int len, sleep_cnt, status_flags; long total; #if( FUN_NAME ) printf("sock_write_data()\n"); #endif if( !blocking ) /* making it non-blocking */ { status_flags = fcntl( fd, F_GETFL, 0); if( fcntl(fd, F_SETFL, FNDELAY) < 0) return ERR_SOCKET_ERR; } sleep_cnt = SOCKET_TIMEOUT_CNT; /* number of 0.1 sec sleep for non-blocking sockets */ total = 0; while( bufsize > 0 ) { len = ( bufsize > PACKET_SIZE ? PACKET_SIZE : bufsize ); len = write(fd, (char *) buf, len); if( len < 0 ) { if( errno == EWOULDBLOCK ) { if( !(sleep_cnt--)) return ERR_SOCKET_TIMEOUT; usleep(100000); /* sleep for 0.1 seconds */ } else { return ERR_SOCKET_ERR; } } else { bufsize -= len; /* update counter & pointer */ buf += len; total += len; sleep_cnt = SOCKET_TIMEOUT_CNT; /* reset sleep counter when you write data */ } } if( !blocking ) /* restore flages */ { fcntl( fd, F_SETFL, status_flags); } return total; }