Acho q este post devia ir para o forum “Java Muito Avançado” mas como nao existe vai mesmo para aqui. É o seguinte… criei um ServetSocket e um ClientSocket… até aqui tudo bem… varias pessoas se podem ligar atraves de telnet ao ServerSocket, e tudo que escrevam aparece na consola do ServerSocket. Perfeito!
Só um problema… é quando alguem usa a tecla de BACKSPACE… por exemplo:
Se alguem teclar nas teclas: Q U I T
na consola aparece: quit
Mas se em vez disso alguem teclar: Q U I [BACKSPACE] I T
deveria aparecer “quit” na mesma… mas não… aparece: quiit
ou seja… ele tá a ler a tecla de BACKSPACE como se fosse um caracter normal, e n devia ser assim :
é por isso que depois no codigo quando faço:
buffer.equalsIgnoseCase("quit")
ele retorna false, caso a pessoa tenha usado algum BACKSPACE enquanto escrevia “quit”
Espero que tenham percebido, alguem sabe como resolver este problema?
e eu ignorar o backspace… o resultado seria: “quiit”
o mais estranho é q embora na consola do windows apareca o backspace como caracter, se eu mandar de volta para a consola da telnet, lá vai aparecer correctamente…
Encontrei nos forum da sun o que me parece ser a melhor resposta para o meu problema, fica aqui a resposta para caso alguem daqui venha a ter o mesmo problema mais tarde.
[quote]Generally, this is a telnet problem.
When you telnet into a UNIX or a Linux machine, the system places you in what’s called a terminal. The terminal is essentially a set of settings that describe various features such as how many horizontal lines can be displayed, how many vertical columns, what size font, what type font, and so on. This is a gross oversimplification, but please bear with me.
As you would expect, various vendors offer their own terminals. One annoying quirk is that some companies treat the backspace key as “go back one character and overwrite it” and others prefer to use the delete key for that behavior. But in any case, let’s assume you press the wrong key. What happens is that the terminal echos back to you the value of the key you pressed (which is not backspace).
Putty and various third party programs automatically deal with situations like this, and this is what you need to modify your program to do. (And I see you’ve done it already.)
To answer your question, no, there’s no better solution than “cleaning up” the keys sent or the output received.
If you’re using raw telnet, you can type the following at your prompt:
% stty erase
which essentially remaps the delete a character command to whichever backspace/delete key you want. I suppose, in a pinch, you could just have your program automatically send that command whenever you connect to a non-conforming terminal, but it’s better in a reliability sense to do the filtering yourself. [/quote]
Na verdade você tem que implementar algo como uma nova console, que entende backspaces, tabs, acentos e etc.
Pelo que entendi você deve estar executando System.out.print(caracterRecebidoViaSocket), e sua console não está interpretando corretamente, entaum, você tem que ensinar a ela o que fazer quando receber BACKSPACE, e etc.
A vida não é tão fácil coleguinha !
Dá uma olhadinha neste código, funciona em windows !
package console;
public class Teste {
public static void main(String args[]) {
Reader reader = new InputStreamReader(System.in);
int c;
try {
while((c = reader.read()) != (int) 13)
System.out.print((char)c);
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
}
Exactamente! No putty telnet client por exemplo, este problema dos backspaces já não acontece porque, pelo que me disseram, o putty faz emulação para 8 bytes. Mas pormenores tecnicos à parte, eu com isto apenas queria saber se a API de java já tinha alguma solução para este problema. Visto que não, já tratei de fazer eu uma