File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes IO problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "IO problem" Watch "IO problem" New topic

IO problem

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439
Why is this code not working?

I have written the program below but somehow after read the input stream the server doesnot process the outputstream... it gets sort of stuck....
I tried Buffered Reader and InputStream for reading input from program. While using 'is' (InputStream) gets me to line that contains
System.out.println("I am here") , using BufferedReader is not taking me beyond the while loop

import java.util.*;
public class WhoisServer {
static int port=43;
static Socket s=new Socket("localhost",43);;
public static void main(String args[])
try {
ServerSocket soc=new ServerSocket(port);
InputStream is;
Reader rd;
BufferedReader br=null;
OutputStream os;
OutputStreamWriter out=null;
BufferedWriter bw;
String inquiry="";
String output1="Hello";
String output2="There";
String output3="I";
String output4="am";
String output5="Just Testing";
String output6="Bye";

is= s.getInputStream(); int letter=0;
br=new BufferedReader(new InputStreamReader(is));
inquiry=inquiry+(char)letter; }
while(inquiry!=null){ inquiry=br.readLine();
out= new OutputStreamWriter(os);
out.write("Welcome to host");
System.out.println("I am here");
} catch (Exception ioe) {
String mess=""+ioe;
System.out.println("Server Already Running at : " +port );
System.out.println("Server Whois : " + ioe);
You may see the sample application using this server here
Whois Server at this very IO stream forum
Thank you in advance
Maki Jav
[ November 08, 2003: Message edited by: Maki Jav ]

Help gets you when you need it!
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671

This seems to be designed to keep looping until readLine() would return null. What would cause that to happen? The readLine() method returns null only if no more input is available, e.g. because the end of a file has been reached, or because the source you're reading from cannot send anything anymore. In this case, I assume you've got some other process (maybe on another machine) which has connected to your server socket. What is that other process doing? Is it just sitting there with an open socket? Is it writing, reading, what? My guess is that your server is waiting for the client to say something, and the client is not saying anything. Perhaps the client needs to hang up? If the client has an OutputStream connected to a socket, then if you close() theo OutputStream that will tell the socket that no more bytes will be sent, which will allow the InputStream on the server to return -1 to indicate there's nothing more to be read. You BufferedReader will translate this into a null return value, and your program will continue from there.
[ November 08, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439
Thanks Jim for the reply.
The application I used to model my whoisserver is actually used to get information from whois servers of various whois services at the web.
I guess that the servers running this service just read a few lines and stop reading....They start writing to socket... The code in application gives us a clue ...

PrintStream ps=new PrintStream(os);

Reader rd=new InputStreamReader(is);
BufferedReader br=new BufferedReader(rd);
String tmp=br.readLine();


What you say? The question is how many lines? now if you want to see the full code it is here
Thanks once again,
Maki Jav
[ November 09, 2003: Message edited by: Maki Jav ]
I agree. Here's the link:
subject: IO problem
jQuery in Action, 3rd edition