aspose file tools*
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
Author

IO problem

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
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.io.*;
import java.util.*;
import java.net.*;
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";

while(s!=null){
s=soc.accept();
os=s.getOutputStream();
System.out.println(s);
is= s.getInputStream(); int letter=0;
br=new BufferedReader(new InputStreamReader(is));
/*while(letter!='\r'){ letter=is.read();
inquiry=inquiry+(char)letter; }
*/
while(inquiry!=null){ inquiry=br.readLine();
if(inquiry!=null)
System.out.println(inquiry);
}
out= new OutputStreamWriter(os);
out.write("Welcome to host");
System.out.println("I am here");
out.write(output1);
out.flush();
}
//System.out.println(url);
s.close();
br.close();
out.close();
} catch (Exception ioe) {
String mess=""+ioe;
if(mess.indexOf("JVM_Bind")!=-1)
System.out.println("Server Already Running at : " +port );
else
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
Wanderer
Sheriff

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: 437
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);
ps.print(query+"\r\n");
ps.flush();

Reader rd=new InputStreamReader(is);
BufferedReader br=new BufferedReader(rd);
String tmp=br.readLine();
while(tmp!=null){
result.addElement(tmp);
tmp=br.readLine();
}
br.close();

ps.close();

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 ]
 
 
subject: IO problem