🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.java.programmer
7 messages
7 total messages Started by Geoffrey L. Goetz Wed, 04 Feb 1998 00:00
IP Socket programming with BufferedReader
#439
Author: Geoffrey L. Goetz
Date: Wed, 04 Feb 1998 00:00
44 lines
2528 bytes
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(); } } }
Re: IP Socket programming with BufferedReader
#440
Author: 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
Re: IP Socket programming with BufferedReader
#441
Author: shoban
Date: Wed, 04 Feb 1998 00:00
48 lines
2613 bytes
There should be a method for specifying when the data orginating from the
server stops. I the case you are communicating with Web server (e.g., GET
www.yahoo.com HTTP/1.0), the server terminates connection with an empty line
or specifies the number of bytes that it will be sending sending before
termination.

If there is no way of finding out when the data stream ends, read all the
data that comes in. When the server closes the socket, you will get an
exception.

Instead of using a 'for' loop try using a do-while construct:

try
{
  do
  {
    readLine();
  }while(true);
}
catch(exception)
{
}

This way, you can get all the data.

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).
Re: IP Socket programming with BufferedReader
#442
Author: 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
>
Re: IP Socket programming with BufferedReader
#443
Author: kSmith
Date: Wed, 04 Feb 1998 00:00
58 lines
2865 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
>
Re: IP Socket programming with BufferedReader
#437
Author: Greg Dickson
Date: Sat, 07 Feb 1998 00:00
42 lines
2332 bytes
Im trying to this sort of thing also try this its got heaps of ideas
http://www.first.gmd.de/persons/leo/java/Telnet
Im after a solution of sending \r and \n as is (not as carriage
returns) to a telnet server but thats another problem that no one
wants to answer
Hope this helps you though
Greg Dickson
witchy@netserv.net.au
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
Re: IP Socket programming with BufferedReader
#438
Author: Greg Dickson
Date: Sat, 07 Feb 1998 00:00
42 lines
2332 bytes
Im trying to this sort of thing also try this its got heaps of ideas
http://www.first.gmd.de/persons/leo/java/Telnet
Im after a solution of sending \r and \n as is (not as carriage
returns) to a telnet server but thats another problem that no one
wants to answer
Hope this helps you though
Greg Dickson
witchy@netserv.net.au
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads