🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: IP Socket programming with BufferedReader

#442
From: k smith
Date: Wed, 04 Feb 1998 00:00
57 lines
2845 bytes
Geoff,

I've found the following piece of code quite effective for reading a stream
from a socket into a string

  public static String readStringFromSocket( Socket socket ) {
    int bufferSize = 4096;
    StringBuffer resultBuffer = new StringBuffer( 4096 );
    int resultSize = 0;
    char c[] = new char[bufferSize];
    try {
      InputStreamReader in = new
InputStreamReader( socket.getInputStream() );
      while (resultSize >= 0) {
        resultSize = in.read( c, 0, bufferSize );
        resultBuffer.append( c, 0, resultSize );
      }
    }
    catch( Exception e ) {
    }
    return resultBuffer.toString();
  }

You can tweek the buffer sizes for efficiency but this should be much faster
than reading in text via readln().  This still seems to raise an exception
though -- does anyone have any ideas on how to check to see if a socket is
still connected?  That could be added to the while condition.

kevin

Geoffrey L. Goetz wrote in message
<34D808EB.51ECE550@columbus.NOSPAM.rr.com>...
>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
>
Technical Details
Message-ID: <6ba6lv$7it15@forums.borland.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: "k smith" <ks@columbus.rr.com>
Subject: Re: IP Socket programming with BufferedReader
Date: 1998/02/04
Message-ID: <6ba6lv$7it15@forums.borland.com>
X-Deja-AN: 322036075
References: <34D808EB.51ECE550@columbus.NOSPAM.rr.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
Organization: None
Newsgroups: comp.lang.java.programmer,borland.public.jbuilder.java.language