Results 1 to 1 of 1
- 05-19-2010, 05:38 PM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
Client cannot retrieve all strings from the InputStreamReader through a loop
Hello,
I have a following loop in the client side to read strings from InputStreamReader of a socket
try{
kkSocket = new Socket("yadav-notebook", 1357);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}catch(UnknownHostException e){}
String fromServer=null;
while((fromServer=in.readLine())!=null){
System.out.println(fromServer);
}
I want to display the string typed by a client to all the clients connected to a multi-threaded server. Why do I have to hit enter to read one string at a time? What is wrong in the while loop above?
The server codes are given below.
public class Talker implements Runnable {
Socket linkto; // the socket
PrintWriter out; // the output streams
BufferedReader bin;
int id; // i.d. of the connection
String from_name; // name of host connecting
..
public static void main(String [] args) {
// Parent thread - create a server socket and await a connection
ServerSocket ss = null;
Socket s = null;
connectiontable = new Vector();
try {
ss = new ServerSocket(1357);
while ((s=ss.accept())!= null) {
Talker now;
Thread current = new Thread(now = new Talker(s));
current.setDaemon(true);
connectiontable.addElement(now); // Save talker into vector ..
current.start(); // start the user's thread
}
}
catch (Exception e) {
System.out.println(e);
}
}
Talker (Socket from) {
id = nextid++;
linkto = from;
InetAddress source = linkto.getInetAddress();
from_name = source.getHostName();
try {
out = new PrintWriter(new OutputStreamWriter
(linkto.getOutputStream()));
bin = new BufferedReader(new InputStreamReader
(linkto.getInputStream()));
}
catch (Exception e) {e.printStackTrace();}
}
..
public void run () {
String line = " [User has just logged in] ";
while (true) {
boolean done=false;
// read a line from the user
if (line == null) {
try {
out.print(">: "); //prompt,flush,read
out.flush();
line = bin.readLine();
} catch (Exception e) {
System.out.println(e);
done = true; // force exit if there's a problem
}
}//end if
// echo the line (with a header) to all users
String outline = from_name+" "+id+": "+line;
System.out.println(outline);
int k;
for (k=0;k<connectiontable.size();k++) {
Talker person = (Talker)connectiontable.elementAt(k);
person.out.println(outline);
person.out.flush(); // Vital - ensure it is sent!
}
if (done) {
connectiontable.removeElement(this);
try {
out.close(); // closes needed to terminate connection
bin.close(); // otherwise user's window goes mute
linkto.close();
}catch (Exception e) {}
break;
}//end if
line = null;
}//end while
}//end run
Similar Threads
-
Need help writing program to compare 2 strings using a loop
By kornwheat in forum New To JavaReplies: 15Last Post: 11-06-2009, 10:31 AM -
While loop comparing strings from user
By N3VRMND in forum New To JavaReplies: 5Last Post: 10-30-2009, 08:18 AM -
[SOLVED] Cant compare strings sent by client
By christuart in forum NetworkingReplies: 15Last Post: 09-01-2008, 03:24 AM -
jsp program for client side printer to print these 2 strings on 3/3
By for453 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-07-2007, 04:42 PM -
i want how to make aprint client side printer to print these 2 strings on 3/3 inch pa
By for453 in forum NetworkingReplies: 0Last Post: 08-06-2007, 06:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks