🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Problems with blocking socket

#159
From: Jon Skeet
Date: Mon, 11 Feb 2002 12:45
51 lines
2872 bytes
Rosinger <rosinger@optushome.com.au> wrote:
> 		String response = "";
> 		String line;
> 		while ((line = in.readLine()) != null)
> 		{
> 			response += line;
> 		}

This bit of code is horrible, by the way - use a StringBuffer.
See http://www.pobox.com/~skeet/java/string.html
 		
> 		System.out.println (response);
> 
> 		in.close();
> 		out.close();
> 		socket.close();
> 	}
> }

These close calls should be part of a finally block, so they get 
executed even if an IO exception is thrown.

Neither of these are major problems for small test code, but I thought 
I'd just raise them in case you weren't aware of them.

> 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.

Indeed - and that's correct. It should only return null when the socket 
has been closed (by either end).

> I find it very strange that for the mail server example, the while loop 
> does NOT return null when there's nothing to read from the socket and 
> keeps blocking, while the web server example DOES return null and 
> returns when there's nothing to read from the socket.

Nope, the web server is closing the socket in this case, hence the 
return of null.
 
> Does anyone know what the difference between the mail server and the web 
> server is? Or, how to solve this problem?

In a mail server, the protocol should tell you how much to read - eg by 
finishing with a line of just "." - but this depends on the protocol you 
want to use.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Technical Details
Message-ID: <MPG.16d1cd1ffbee75e098aa59@dnews.peramon.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-x2.support.nl!195.241.194.21.MISMATCH!news.tiscali.nl!bnewspeer01.bru.ops.eu.uu.net!auucp0.ams.ops.eu.uu.net!bnewsifeed00.bru.ops.eu.uu.net!lnewspost00.lnd.ops.eu.uu.net!emea.uu.net!not-for-mail
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:45:56 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-x2.support.nl!195.241.194.21.MISMATCH!news.tiscali.nl!bnewspeer01.bru.ops.eu.uu.net!auucp0.ams.ops.eu.uu.net!bnewsifeed00.bru.ops.eu.uu.net!lnewspost00.lnd.ops.eu.uu.net!emea.uu.net!not-for-mail
From: Jon Skeet <skeet@pobox.com>
Newsgroups: comp.lang.java.programmer,aus.computers.java
Subject: Re: Problems with blocking socket
Date: Mon, 11 Feb 2002 12:45:02 -0000
Message-ID: <MPG.16d1cd1ffbee75e098aa59@dnews.peramon.com>
References: <3C67B406.1070407@optushome.com.au>
Organization: Peramon Technology Ltd.
X-Newsreader: MicroPlanet Gravity v2.30
X-Original-NNTP-Posting-Host: 10.1.3.5
X-Original-Trace: 11 Feb 2002 12:44:52 GMT, 10.1.3.5
Lines: 51
NNTP-Posting-Host: fw.peramon.com
X-Trace: 1013431513 reading.news.pipex.net 234 193.132.195.125
X-Complaints-To: abuse@uk.uu.net