Home > Archive > Unix Programming > May 2004 > Socket Read() does not return values
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Socket Read() does not return values
|
|
| zb011 message 2004-05-12, 9:08 pm |
| Hi all,
I currently have an outbound socket connecting from a solaris box to a
windows box. The data transfer requires an Acknowledgement to be
returned before the next segment of data is sent.
At certain times, read() does not return me any value ==> staying in a
"HANG" state.
I have thus 2 questions that hopefully someone can help with.
1) Why does read not return anything..staying in a "HANG" state.
(read() is a blocking function)
2) How can i work around this?
==> have tried select() to check on the socket.
"0" is returned when there is nothing to be read. this can be used to
aviod read() being in a hang state.
However, select() requires a sleep time to be employed. This is
however undiserable in my situation.
Any ideas anyone?
Thanks!!
============code extract=================================
=
int readn(int fd, char *ptr)
{
int ntot, nread, found, sr; //sr added 11th May
char *start_posn, *end_posn;
start_posn = ptr;
found = 0;
ntot = 0;
struct timeval to;
fd_set rread;
while((ntot < MAX_MSG_LEN) && (found == 0))
{
nread = read(fd, ptr, 1); <===HANG STATE encountered
if (nread < 0)
{
if (found)
return -43;
else
return -42;
}
else if (nread == 0)
{
break;
}
| |
| Nils O. Selåsdal 2004-05-12, 9:08 pm |
| On Wed, 12 May 2004 01:08:20 -0700, zb011 message wrote:
> ============code extract=================================
= int readn(int
> fd, char *ptr)
> {
> int ntot, nread, found, sr; //sr added 11th May char *start_posn,
> *end_posn;
> start_posn = ptr;
> found = 0;
> ntot = 0;
> struct timeval to;
> fd_set rread;
> while((ntot < MAX_MSG_LEN) && (found == 0)) {
> nread = read(fd, ptr, 1); <===HANG STATE encountered if (nread < 0)
> {
> if (found)
> return -43;
> else
> return -42;
> }
> else if (nread == 0)
> {
> break;
> }
if nread < 0 , it means some error happened, and you should take action
accordingly., I assume you didn't include everything above, as 'found'
is always 0, and you overwrite 'ptr' on more than 1 loop.
| |
| Søren Hansen 2004-05-12, 9:08 pm |
| On Wed, 12 May 2004 01:08:20 -0700, zb011 message wrote:
> 1) Why does read not return anything..staying in a "HANG" state.
> (read() is a blocking function)
If there's nothing to be read, read() will block until there are data, EOF
or an interrupt occurs.
> 2) How can i work around this?
> ==> have tried select() to check on the socket. "0" is returned when
> there is nothing to be read. this can be used to aviod read() being in a
> hang state.
> However, select() requires a sleep time to be employed. This is however
> undiserable in my situation.
Just set the timeout to 0 seconds and 0 milliseconds. Do NOT set the
timeout argument to NULL, since that will make select() block, which in
your case would be quite pointless.
--
Salu2, Søren.
| |
| Fletcher Glenn 2004-05-12, 9:08 pm |
| Søren Hansen wrote:
> On Wed, 12 May 2004 01:08:20 -0700, zb011 message wrote:
>
>
>
>
> If there's nothing to be read, read() will block until there are data, EOF
> or an interrupt occurs.
>
>
>
>
> Just set the timeout to 0 seconds and 0 milliseconds. Do NOT set the
> timeout argument to NULL, since that will make select() block, which in
> your case would be quite pointless.
>
It's also possible to set the socket into non-blocking mode. Using the
fcntl command and the O_NONBLOCK flag does the trick.
By the way, when read returns a value of zero, it is an indication of
an EOF condition. When it happens on a socket, it means that the other
end of the connection has closed.
--
Fletcher Glenn
| |
| David Schwartz 2004-05-12, 9:08 pm |
|
"Fletcher Glenn" <fletcher@removethisfoglight.com> wrote in message
news:40A2477C.1080504@removethisfoglight.com...
> It's also possible to set the socket into non-blocking mode. Using the
> fcntl command and the O_NONBLOCK flag does the trick.
Not just possible, it's the only solution. The 'select' function won't
help him, the socket will still be blocking.
DS
| |
| Søren Hansen 2004-05-13, 6:31 am |
| On Wed, 12 May 2004 10:53:54 -0700, David Schwartz wrote:
> Not just possible, it's the only solution. The 'select' function won't
> help him, the socket will still be blocking.
Why?
|
|
|
|
|