Article View: comp.lang.java.programmer
Article #158Problems with blocking socket
From: Rosinger
Date: Mon, 11 Feb 2002 12:07
Date: Mon, 11 Feb 2002 12:07
68 lines
3109 bytes
3109 bytes
Hi all,
I am experimenting with sockets in Java. I have this little program that
I've written:
------------------------------------------------------------------------
import java.net.*;
import java.io.*;
public class client
{
public static void main (String[] args) throws IOException
{
String username = "foo";
String password = "bar";
Socket socket = new Socket ("mail", 110); // mail is my local POP3 server
BufferedReader in = new BufferedReader (new InputStreamReader
(socket.getInputStream()));
PrintWriter out = new PrintWriter (new OutputStreamWriter
(socket.getOutputStream()));
// send command to server
out.print (username + "\n");
out.flush();
// read response
String response = "";
String line;
while ((line = in.readLine()) != null)
{
response += line;
}
System.out.println (response);
in.close();
out.close();
socket.close();
}
}
------------------------------------------------------------------------
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.
If, for example, I replace the host to "www" (my local web server) and
request to get an HTML file like:
out.print ("GET " + "/index.html" + " HTTP/1.0\n\n")
out.flush();
The while loop gets back the HTML file line by line, and when the entire
file has been read, it does NOT block anymore.
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.
Does anyone know what the difference between the mail server and the web
server is? Or, how to solve this problem?
Thanks in advance,
Cheers,
Joshua
Technical Details
Message-ID:
<3C67B406.1070407@optushome.com.au>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!xmission!hub1.nntpserver.com!news-out.spamkiller.net!propagator-la!news-in-la.newsfeeds.com!news-in.superfeed.net!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!news.xtra.co.nz!news1.optus.net.au!optus!spool01.syd.optusnet.com.au!spool.optusnet.com.au!210.49.20.119.MISMATCH!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10a146,610ca5e9d6f87bb8,start X-Google-Attributes: gid10a146,public X-Google-Thread: 10d6db,610ca5e9d6f87bb8,start X-Google-Attributes: gid10d6db,public X-Google-ArrivalTime: 2002-02-11 04:10:12 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!xmission!hub1.nntpserver.com!news-out.spamkiller.net!propagator-la!news-in-la.newsfeeds.com!news-in.superfeed.net!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!news.xtra.co.nz!news1.optus.net.au!optus!spool01.syd.optusnet.com.au!spool.optusnet.com.au!210.49.20.119.MISMATCH!not-for-mail Message-ID: <3C67B406.1070407@optushome.com.au> Date: Mon, 11 Feb 2002 23:07:34 +1100 From: Rosinger <rosinger@optushome.com.au> User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.4) Gecko/20011019 Netscape6/6.2 X-Accept-Language: en-us MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer,aus.computers.java Subject: Problems with blocking socket Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 69 NNTP-Posting-Host: 203.164.67.187 X-Trace: 1013429163 6128 203.164.67.187