🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: IP Socket programming with BufferedReader

#440
From: Kent Smotherman
Date: Wed, 04 Feb 1998 00:00
65 lines
3492 bytes
Well this can be a bit tricky since, as you said, you don't want to rely on
the exception. I have had problems in the past with a server socket closing
and no exception being thrown on the waiting client. I always code a simple
heartbeat message that is sent from the client to the server and the server
returns. If the heartbeat doesn't make it back in X amount of time, the
client closes the socket connection. With something like this in place you
can replace your for loop with a while loop....tell me if you need more
details....

Geoffrey L. Goetz wrote:

> I am having a problem with implementing the readLine() method of the
> BufferedReader.  I have an IP socket server written in Delphi C/S 3.0
> running on NT and want to send/receive a string of text to/from this ip
> server with a java client application (not an applet).  This java
> application will eventually be a servlet.  But first I need to get some
> of the basics of Java based IP programming under control.
>
> The problem is that the readLine() quits once a carriage return and/or a
> line feed is 'hit'.  So I decided to create a for loop that will execute
> 100 times until an exception is raised which will force an exit.  (BAD
> CODE)  Don't like counting on the exception and I never know if 100
> executions of readLine() will be enough.  The server application
> (Delphi/NT) will close the connection once the data is sent back if this
> helps...
>
> Any other (GOOD CODE) ways of implementing this?  For the example I am
> using port 80 on yahoo's web server and sending a request for the
> default page of the server, but my needs are not URL based.  I need to
> send and receive multiple strings to and from an open ip socket on a
> server (non standard ports).
>
> Thanks!
> -Geoff
>
> import java.net.*;
> import java.io.*;
> public class ClientSocket {
>   public static void main(String args[]) {
>     try {
>       InetAddress addr = InetAddress.getByName("www.yahoo.com");
>       Socket socket = new Socket(addr, 80);
>       BufferedReader in = new BufferedReader(
>         new InputStreamReader(socket.getInputStream()));
>       PrintWriter out = new PrintWriter(new BufferedWriter(
>         new OutputStreamWriter(socket.getOutputStream())),true);
>       out.println("GET / HTTP/1.0\f\n");
>       try {
>         for(int i = 0; i < 100; i++)
>           System.out.println(in.readLine()); }
>       catch(Exception e) { } }
>     catch(Exception e){
>       e.printStackTrace(); } } }



--
For sample Java and USRobotics Pilot source code (for gcc) visit:

http://www.tconl.com/~kents

For PortalNet and MagicNet info and downloads visit:

http://www.tconl.com/~kents/portalnet
Technical Details
Message-ID: <34D8678B.827AEA32@tconl.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <34D808EB.51ECE550@columbus.NOSPAM.rr.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a146,ba5f46377b9766a3
X-Google-Attributes: gid10a146,public
X-Google-Thread: f74b3,ba5f46377b9766a3
X-Google-Attributes: gidf74b3,public
From: Kent Smotherman <kents@tconl.com>
Subject: Re: IP Socket programming with BufferedReader
Date: 1998/02/04
Message-ID: <34D8678B.827AEA32@tconl.com>
X-Deja-AN: 321977381
Content-Transfer-Encoding: 7bit
References: <34D808EB.51ECE550@columbus.NOSPAM.rr.com>
MIME-Version: 1.0
Reply-To: kents@tconl.com
To: "Geoffrey L. Goetz" <ggeoffre@columbus.NOSPAM.rr.com>
Content-Type: text/plain; charset=us-ascii
X-Trace: 4 Feb 1998 07:00:35 +600, 204.26.80.70
Newsgroups: comp.lang.java.programmer,borland.public.jbuilder.java.language