1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import java.net.*;
import java.io.*;
class LMSTelnet {
public static void main( String[] args ) {
int port = Integer.parseInt(args[1]);
ClientTelnet cltlnt= new ClientTelnet(args[0],port);
}
}
class ClientTelnet extends Thread{
protected Socket s;
protected BufferedReader in;
protected BufferedReader inKey;
protected PrintStream out;
public ClientTelnet(String host,int port){ //constructeur lance méthode "écoute serv" et "écoute clavier"
try{
s=new Socket(host,port);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(IOException e){
System.out.println("Prob Socket");
System.exit(1);
}
start();
KeyListener();
}
public void KeyListener(){ //lit le cliavier et l'envoi au serv
String ligne;
try{
inKey = new BufferedReader( new InputStreamReader( System.in ) );
out = new PrintStream( s.getOutputStream() );
while( !(ligne = inKey.readLine()).equals("xxxxxx") )
{
ligne = HexToString( ligne );
out.println( ligne );
}
System.exit(1);
} catch( IOException e ) {
System.out.println( "probleme d'entree/sortie" );
}
}
public static String HexToString(String hexa) {
// On vérifie la longeur de la chaine :
if (hexa.length() % 2 != 0) {
throw new RuntimeException("Taille incorrecte");
}
// Création du buffer de lecture :
StringBuffer buf = new StringBuffer();
// On parcours la chaine par bloc de 2 caractères :
for (int pos = 0; pos<hexa.length(); pos+=2) {
// On récupère la chaine courante :
String substring = hexa.substring(pos, pos+2);
// Que l'on convertit en int puis en char :
char c = (char) Integer.parseInt(substring, 16);
// Et on ajoute le char au buffer :
buf.append(c);
pos++;
}
return buf.toString();
}
public void run(){ //lit le coté serv et l'affiche
String ligne;
try{
while(true){
sleep(1000);
ligne=in.readLine();
if(ligne==null)break;
System.out.println(ligne);
}
}catch(Exception e){
System.out.println("connexion perdue");
}
}
} |