CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    Single 'keystroke' access and char display, in a JAVA console app?

    I had written a simple text based TCP/IP client app to get a feel for the JAVA sockets implementation. Well it "kind" of worked, but it had a fatal flaw, specifically that it seems useless if there is no EOL character on either the incoming or outgoing data. I had created an InputStream and an OutputStream and attached them to the Socket. To gather keyboard input, I used the System.in stream, as in the below.

    I'm sure there are many issues and errors in my hasty set of exceptions, but right now my main interest is to figure out how I can modify this to (a) display incoming characters as they come in (without waiting for an appended EOL), and sending keystrokes as they are typed, again without waiting for the ENTER key. (BTW, I DO understand that doing so would normally be extremely wasteful of network bandwidth, but the specific external test device I'm trying to interface with (a) doesn't always terminate a sent text message with an EOL, and (b) requires single key commands, and will reject them if they come in a block ending with an EOL),

    I think for the keystrokes I need to find an example of setting up a class to receive a "keystroke" event. But I don't know... maybe all the InputStream and OutputStream classes I'm using are causing me this same grief?


    Code:
       public static void iotest2() throws IOException  { 	
        	Socket s = null;
        	InputStream in = null;
        	OutputStream out = null;
        	
            byte [] myBuf = new byte[4096];
            int size;
            
        	try 
        	  {
        	     System.out.println("Connecting, please wait...");
          	     s = new Socket("192.168.0.19", 12005);
                 System.out.println("Connected: " + s);
         	     s.setTcpNoDelay(true); 
     
          	     in = s.getInputStream();
          	     out = s.getOutputStream(); 
          	     
     
    end:
        	   	 while (s.isConnected())
        	   		{
        	   		 while((size = in.available()) > 0)
        	   		  {
        	   		   size = in.read(myBuf, 0, size); 
        	   		   if (size <= 0) break; // or continue 
        	   		   String inMsg = new String(myBuf).substring(0,size);
        	   		   System.out.println(inMsg);
          	   		  }   	   		  
        	   		    
           	   		 while ( (size = System.in.available()) > 0) 
        	   		  {
        	   		   size = System.in.read(myBuf, 0, size);  // probably just 1 key
        	   		   if (size <= 0) break;
         	   		    out.write(myBuf, 0, size);
        	   		    out.flush();
         	   		  }
        	   	   }
        		}
          	catch (UnknownHostException ex)
        	    {
                System.out.println("Unknown Host: " + ex.getMessage());
                ex.printStackTrace();
        	    }
            catch (IOException ex)
                {
                System.out.println("I/O Exception: " + ex.getMessage());
                ex.printStackTrace();
                }
           	finally
        	    {
        		if (s != null) s.close();
        		if (in != null) in.close();
        		if (out != null) out.close();
        	    }
        
        	System.out.println("Program Exit");
        }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Single 'keystroke' access and char display, in a JAVA console app?

    The console doesn't provide a way of getting single key strokes, input is only sent to the system.in stream once the enter key is pressed. If you want single key strokes you can create a simple terminal style swing based GUI and add a KeyListener to get notified of key events.

    The incoming characters not displaying until an EOL is most likely down to the fact they aren't sent individually. Where is the sending code?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    Re: Single 'keystroke' access and char display, in a JAVA console app?

    Quote Originally Posted by keang View Post
    The console doesn't provide a way of getting single key strokes, input is only sent to the system.in stream once the enter key is pressed. If you want single key strokes you can create a simple terminal style swing based GUI and add a KeyListener to get notified of key events.

    the incoming characters not displaying until an EOL is most likely down to the fact they aren't sent individually. Where is the sending code?
    Hey there again keang! Well if by the "sending" code you mean the device I'm connected to, that is all compiled "C" code running in an external 386ex based SBC board. But at this point I'm pretty sure the blocks received from that side are indeed displaying correctly in my JAVA app, whether sent as a bloc or not, and whether EOL terminated or not. I think I was misinterpreting the behavior I was seeing.

    The outgoing key hits, however, are another matter. It is distressing if there truly is NO WAY to access or interpret single keystrokes, as events or otherwise, in a console program. If that is true, this will be the first language I've ever worked with that didn't allow direct keystroke access, in both GUI based apps and console apps. Sadly, if this is true, there is no way to implement even a simple terminal program in console mode. Perhaps there is a backdoor? maybe through some of the native interface calls I've heard about?

    Well anyway, I'm glad its a true limitation and not my error. But you know how us coders hate to believe anything "can't" be done ;-)

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Single 'keystroke' access and char display, in a JAVA console app?

    It is distressing if there truly is NO WAY to access or interpret single keystrokes, as events or otherwise, in a console program.
    You're not the first person to feel this way about this problem.
    If that is true, this will be the first language I've ever worked with that didn't allow direct keystroke access, in both GUI based apps and console apps.
    I suspect the problem is the console is provided by the OS and it is designed for line input/output, hence you can only do this by creating your own java GUI console.
    Sadly, if this is true, there is no way to implement even a simple terminal program in console mode. Perhaps there is a backdoor? maybe through some of the native interface calls I've heard about?
    It is true and there is no backdoor and even if you you went to all the trouble of using JNI I'm not even sure if the command line console provided by the OS would have this feature. And anyway it's much simpler to just create your own GUI console.
    But you know how us coders hate to believe anything "can't" be done
    Who said it can't be done, you just have to use a GUI console, have I said that already!!
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    Re: Single 'keystroke' access and char display, in a JAVA console app?

    Quote Originally Posted by keang View Post
    You're not the first person to feel this way about this problem.
    I suspect the problem is the console is provided by the OS and it is designed for line input/output, hence you can only do this by creating your own java GUI console.
    It is true and there is no backdoor and even if you you went to all the trouble of using JNI I'm not even sure if the command line console provided by the OS would have this feature. And anyway it's much simpler to just create your own GUI console.
    Who said it can't be done, you just have to use a GUI console, have I said that already!!

    Thanks for the confirmation then.

    OK, well if a GUI console is easy to implement, that would probably be both a good starting point and a good learning experience for me. Especially as I'm anxious to see how different it is from building a GUI based app in the "Windows" world. Do you know of an existing example GUI based console project for to start with? Seems every time I dig around for tutorials on the Oracle/Sun site they pages are not found (I think they are rebuilding the site or something).

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Single 'keystroke' access and char display, in a JAVA console app?

    A quick google came up with this http://www.subbu.org/javaapps/console/JavaConsole.html. I've no idea if it does what you want but it should get you started.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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

Featured