• Post Reply Bookmark Topic Watch Topic
  • New Topic

not able to enter selection on servlet  RSS feed

 
Valarie Brandt
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI I hope I can get some help on this :
I have a class called protocol in which i specify the appearance and what each selection does and I have this class called :
public class Client
{
private boolean status = true;
private PrintWriter out = null;
private BufferedReader in = null;
private Socket assignSocket = null;
private String host;


public static void main(String[] args) throws IOException
{
Client cc = new Client();
cc.runClient(args);
}

private void runClient(String[] args) throws IOException
{
if (args.length == 0)
{
host = "localhost";
}
else
{
host = args[0];
}

try
{
assignSocket = new Socket("localhost", 4444);
out = new PrintWriter(assignSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader
(assignSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Doesn't know about host: " + host);
e.printStackTrace();
return;

}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: " +
host);
e.printStackTrace();
return;
}

BufferedReader stdIn = new BufferedReader(new InputStreamReader
(System.in));
String fromServer;
String fromUser = null;

while (true)
{
fromServer = in.readLine();
System.out.println(fromServer);
while (true)
{
fromServer = in.readLine();
if (fromServer == null)
{
System.out.println("fromServer was null");
break;
}

System.out.println(fromServer);

if (fromServer.equals("Bye"))
{
status = false;
break;
}
}
if (status == false)
{
break;
}

if (fromUser != null)
{
fromUser = stdIn.readLine();
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
assignSocket.close();
}
}
I think part of my problem is I can't break out of the loop but... i am not sure what i am doing wrong or omitting.
any suggestions would be great.

valarie
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I had a quick look at your code and I'm gonna assume the problem is in your while (true) loops .. seems a likely place to look!

As an aside would you consider putting the line


in instead of the



It'll make the code a bit less prone to problems.

Next on your inner loop you search for "bye"


and that is the only way out of the loop. You might want to have a look at that.
Also do you really need the nested while loop. Have a look at this part of the code and post back if you're still having problems,
Barry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!