Article View: comp.lang.java.programmer
Article #439IP Socket programming with BufferedReader
From: Geoffrey L. Goetz
Date: Wed, 04 Feb 1998 00:00
Date: Wed, 04 Feb 1998 00:00
44 lines
2528 bytes
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(); } } }
Technical Details
Message-ID:
<34D808EB.51ECE550@columbus.NOSPAM.rr.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10a146,ba5f46377b9766a3,start X-Google-Attributes: gid10a146,public X-Google-Thread: f74b3,ba5f46377b9766a3,start X-Google-Attributes: gidf74b3,public From: "Geoffrey L. Goetz" <ggeoffre@columbus.NOSPAM.rr.com> Subject: IP Socket programming with BufferedReader Date: 1998/02/04 Message-ID: <34D808EB.51ECE550@columbus.NOSPAM.rr.com> X-Deja-AN: 321911265 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Time Warner Communications Newsgroups: comp.lang.java.programmer,borland.public.jbuilder.java.language