Results 1 to 5 of 5

Thread: [RESOLVED] Complex loop question on a client app

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Resolved [RESOLVED] Complex loop question on a client app

    Basically, I want my client to be continously able to send messages to the server, but *also* at the same time be able to recevie and display any messages from the server. This is the loop I have in its current state on the client

    Code:
    	while (true) {
    	    try {
    		System.out.println("Enter a message to send to the server:");
    		nextline = stdIn.readLine();
    		if (nextline != null) out.println(nextline);
    
    		fromServer = in.readUTF();
    		if (fromServer != null) System.out.println("From server " + fromServer);
    		
    		else {
    		    out.close();
    		    in.close();
    		    csocket.close();
    		    stdIn.close();
    		}
    		
    	    } catch (Exception e) { }
    	}
    It asks me to enter a message, I enter something and it sends it to the server.. perfect. BUT it just doesnt do anything after that, it just halts. I want it to print 'enter a message' again, and also be able to print messages from the server. Hope someone can help.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Complex loop question on a client app

    Most likely that one of the instructions in the "else" section is throwing an exception thus the system exits the loop and the entire method.

    If you are using an IDE you should use trancing tools, otherwise make use of the Exception instance to figure out whats going wrong
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: Complex loop question on a client app

    Well previously I was using this which worked perfectly fine:

    it just continoulsy enable me to enter something, and it sends it to the server..
    Code:
    		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    		String nextline;
    		while ((nextline = stdIn.readLine()) != null) {
    			out.println(nextline);
    		}
    		out.close();
    		csocket.close();
    		stdIn.close();
    But as you see from my first post, I need to change it to be able to print out strings sent from the server too..

    any ideas?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Complex loop question on a client app

    You are not showing enough of your code so we can help.

    You've got 2 choices:
    1- Show more code
    2- Stick to the 2nd part
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: Complex loop question on a client app

    I sorted it;

    Code:
    	
    	while ((inputline = stdIn.readLine()) != null) {
                out.println(inputline);
    	    
                fromServer = in.readLine();
    	    System.out.println("From server: " + fromServer);
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width