-2

i'm making a java telnet server. The client is the windows telnet. I'm stuck at the sending data,string with other key rather than "ENTER" key. E.g: Hello, i'm user 1. after "user 1" when the full stop is typed it should send that code:

byte[] buff = new byte[4096];
String str = new String(buff, "UTF-8");
do {
    if ( buff[0] == 46 ) {  //46-[.]
        System.out.println("46-[.]  " + buff[0]);
        //incoming.getInputStream().read(buff);
    }
    incoming.getInputStream().read(buff);
} while ((buff[0] != 27) && (!done));
Christoph
  • 1,993
  • 1
  • 25
  • 33
  • Are you asking how to make the windows telnet client send data before return is pressed? – matt Jan 08 '16 at 10:09
  • its not related how ur server is, it depends on what your client uses to trigger the send data – nafas Jan 08 '16 at 10:10
  • that windows client write something. after that instead of ENTER to send that to use other key. – Vinter Alex Jan 08 '16 at 10:13
  • It's unclear what you're asking. post the code in your question, and it is possible to send strings with telnet with `sen` command. – WalterM Jan 08 '16 at 10:15
  • byte[] buff = new byte[4096]; String str = new String(buff, "UTF-8"); do { if ( buff[0] == 46 )// 46-[.] { System.out.println("46-[.] " + buff[0]); //incoming.getInputStream().read(buff); } incoming.getInputStream().read(buff); } while ( (buff[0] != 27) && (!done) ); ---here i used ESC key to stop connection--- – Vinter Alex Jan 08 '16 at 10:15
  • If it is really a telnet problem like you stated at your edit message, the problem may be more suited for superuser.com as they handle software problems, while we handle programming questions – Ferrybig Jan 08 '16 at 10:16
  • Possible duplicate of [Send data over telnet without pressing enter](http://stackoverflow.com/questions/4532344/send-data-over-telnet-without-pressing-enter) – Ferrybig Jan 08 '16 at 10:18
  • @Ferrybig telnet in windows sends every time you press a key. – WalterM Jan 08 '16 at 10:21
  • like in that code: when ESCAPE key[27] is pressed the connection is out. something like that for what i write in telnet. and instead of ENTER i press something else and send it. – Vinter Alex Jan 08 '16 at 10:23
  • @VinterAlex your question still doesn't make sense. Are you getting the key presses as the user types them? – matt Jan 08 '16 at 10:25
  • sorry for confusion. so: i write "hello world'' and instead of pressing ENTER to send that i wanna press "." after hello world. "." behavior like "enter" key – Vinter Alex Jan 08 '16 at 10:53
  • Java's console input is pretty basic maybe instead you could have a look at for a [Java Curses](https://www.google.com.au/search?q=curses&oq=curses&aqs=chrome..69i57j69i60l3.1717j0j7&sourceid=chrome&es_sm=119&ie=UTF-8#safe=off&q=java+curses) library which would give you more control – MadProgrammer Jan 08 '16 at 11:03
  • @VinterAlex from the question Ferrybig linked, you can tell the telnet client to send data after each character. Then it is up to you to determine if a command has been sent. As in your example, check for a "." – matt Jan 08 '16 at 11:21
  • problem solved! thanx for [Java Courses](http://sourceforge.net/projects/javacurses/) @MadProgrammer. – Vinter Alex Jan 08 '16 at 11:41
  • this is what i wanted: 'do { socket.getInputStream().read(buff); str=str+new String(buff); if ( buff[0] == 46 )// 46-[.] { System.out.println("46-[.] "+str ); socket.getOutputStream().write(str.getBytes()); } } while ( (buff[0] != 27) && (!done) );//27-[esc]-exit connection' – Vinter Alex Jan 08 '16 at 11:42

1 Answers1

0

I believe the problem is your code here. This code would never work.

byte[] buff = new byte[4096];
String str = new String(buff, "UTF-8"); //the buffer is initialized to 0 so we get no String
do {
    if ( buff[0] == 46 ) {  //46-[.]
        System.out.println("46-[.]  " + buff[0]);
        //incoming.getInputStream().read(buff);
    }
    incoming.getInputStream().read(buff);   //reading into an array
} while ((buff[0] != 27) && (!done));       //and only checking first index

Try this and see if it works.

public static void main(String[] args) throws IOException {
    //listen on tcp port 5000
    ServerSocket ss = new ServerSocket(5000);
    Socket s = ss.accept();

    //create an input/output stream
    InputStream in = new BufferedInputStream(s.getInputStream());
    PrintWriter out = new PrintWriter(s.getOutputStream(), true);

    byte[] buffer = new byte[0x2000];
    for (int bufferlen = 0, val; (val = in.read()) != -1;) {
        if (val == '.') { //if token is a '.' no magic numbers such as 46
            String recv = new String(buffer, 0, bufferlen);
            System.out.printf("Received: \"%s\"%n", recv);
            bufferlen = 0; //reset this to 0
        } else if (val == 27) {
            s.close();
            break;
        } else { //character is not a . so add it to our buffer
            buffer[bufferlen++] = (byte)val;
        }
    }
    System.out.println("Finished");
}

when you run it do telnet localhost 5000 from the same computer. Windows telnet will send every time you press a key, so this will work with windows telnet not linux. You have to remember that TCP is stream based, unlike UDP which is packet based. I have tested this with Windows Command Prompt, so I don't know which one you are using if it doesn't work.

WalterM
  • 2,686
  • 1
  • 19
  • 26