🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.programmer
Article #160

Re: Problems with blocking socket

#160
From: Gordon Beaton
Date: Mon, 11 Feb 2002 12:49
51 lines
2757 bytes
On Mon, 11 Feb 2002 23:07:34 +1100, Rosinger wrote:
> I am experimenting with sockets in Java. I have this little program
> that I've written:

[...]
> 		while ((line = in.readLine()) != null)
> 		{
> 			response += line;
> 		}

> Nothing too complicated, but there is a little problem: the while
> loop keeps blocking and does NOT return null when there's nothing to
> read from the socket.

It is behaving exactly as it should.

> Does anyone know what the difference between the mail server and the
> web server is? Or, how to solve this problem?

BufferedReader.readLine() returns null at EOF. That means that the end
of the stream has been reached, that there will *never* be any more
data available. Not that there is nothing to read "for the moment".

As long as the connection is still active, readLine() will block until
a complete line has been received, at which point it will be returned
to the caller.

When you tested with a web server, it sent you the file and closed the
connection, so EOF was reached and readLine() returned null.

When you tested with a mail server, the remote did not close the
connection because the session is not yet over; it is waiting for
additional commands. You would have run into the same problem if you
had specified HTTP/1.1 when you connected to the web server.

You need to use different criteria for determining when the end of the
data has been reached. For example the pop server might tell you the
length of the mail before transferring it, and there may be a prompt
that appears afterwards.

You should read the protocol definitions so you can deal with this
situation properly:

  pop: http://www.ietf.org/rfc/rfc1939.txt
  http: http://www.ietf.org/rfc/rfc2616.txt

/gordon

-- 
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
Technical Details
Message-ID: <a48eki$379$1@news.du.uab.ericsson.se>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!luth.se!erix.ericsson.se!not
References: <3C67B406.1070407@optushome.com.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a146,610ca5e9d6f87bb8
X-Google-Attributes: gid10a146,public
X-Google-Thread: 10d6db,610ca5e9d6f87bb8
X-Google-Attributes: gid10d6db,public
X-Google-ArrivalTime: 2002-02-11 04:49:29 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!luth.se!erix.ericsson.se!not
From: not@for.email (Gordon Beaton)
Newsgroups: comp.lang.java.programmer,aus.computers.java
Subject: Re: Problems with blocking socket
Date: 11 Feb 2002 12:49:22 GMT
Organization: Ericsson Computer Science Laboratory, Stockholm, Sweden
Lines: 51
Message-ID: <a48eki$379$1@news.du.uab.ericsson.se>
References: <3C67B406.1070407@optushome.com.au>
NNTP-Posting-Host: piraat.du.uab.ericsson.se
NNTP-Posting-User: gordon
X-No-Productlinks: yes
User-Agent: slrn/0.9.6.3 (SunOS)