Java + Connexion Telnet + PB d'exécution
Bonjour à tous,
Espérant que je poste dans l'endroit adéquat .
Je travaille sur un code de création et lancement d'une session telnet via un serveur de streming VLC.
J'ai pas trouvé de solution pour mon problème que j'essaye de résoudre pendant presque 3 semaines :cry:.
Donc j'ai fini par poster mon code, espérant que quelqu'un pourra me le fixer .
Mon code est simple, un bout de code pour le lancement d'une commande vlc qui crée une session telnet avec un ensemble de paramètre (le syntaxe est précis dans le code). le bout de code suivant permet de se connecter à cette session déjà créé et ajouter un flux dedans.
Le problème c'est que lorsque je lance mon code la création est faite mais la phase de connexion rend toujours des message d'erreur, mais lorsque j'ai divisé mon code dans deux main (un contient le bout de code de la création , l'autre contient le bout de code de la connexion et d'ajout de flux) et je lance le premier et puis le deuxième , ça marche !!!!!
Donc j'ai finit par conclure qu'il s'agit d'un problème de temps ,le programme essaye de se connecter avant que les instructions de la création alloue déjà le port fixé dans la commande de création.Et ce qui est non logique !!!!
j'ai essayé de mettre chacun dans un thread dont le premier met le premier en attente et puis il le lance met ça n'a pas marché .
Je vous serez reconnaissante , aidez moi à résoudre le problème , j'ai tout essayer , j'ai bien fouillé sur le net mais j'ai pas réussi à rien fixé, pourquoi ça se passe .
voilà mon code main
Code:
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.io.Console;
import java.io.IOException;
import java.net.SocketException;
import java.util.Random;
import java.util.Scanner;
import java.lang.Process;
import java.lang.Object;
import java.util.Timer;
/**
*
* @author bassouma
*/
public class FirstMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SocketException, IOException, InterruptedException {
try{
Scanner sc =new Scanner(System.in);
//____________Initialisation___________________________________________________
String ClientIP = "0.0.0.0";
int TelnetPort;
String Password="azerty";
int RTSPHost;
String FilmFileName="The_Film";
String FilePath="d:\\films\\Defiance\\Defiance.avi";
/* System.out.print("Entrer ClientIP:\n");
ClientIP=sc.nextLine();
System.out.print("Entrer Password:\n");
Password=sc.nextLine();
System.out.print("Entrer FilmFileName:\n");
FilmFileName=sc.nextLine();
System.out.print("Entrer FilePath:\n");
FilePath=sc.nextLine();
*/
//____________RTSPHostGenerate__________________________________________________
System.out.print("Entrer RTSPHost:\n");
RTSPHost=sc.nextInt();
/* int RTSPHost_int = 5500;
Random RTSPHost_random = new Random();
RTSPHost = (int) (RTSPHost_random.nextInt(500)+RTSPHost_int);
System.out.print(RTSPHost+"\n");*/
//____________TelentPortGenerate________________________________________________
System.out.print("Entrer TelnetPort:\n");
TelnetPort=sc.nextInt();
/* int TelnetPort_int=4200;
Random TelnetPort_random = new Random();
TelnetPort = (int) (TelnetPort_random.nextInt(500)+TelnetPort_int);
System.out.println(TelnetPort+"\n");*/
//___________StartNewInstance___________________________________________________
String commande = "C:\\Program Files\\VideoLAN\\VLC\\vlc";
String arguments = "--ttl 12 -vvv --color -I telnet --telnet-password "+Password+" --telnet-port "+TelnetPort+" --rtsp-host "+ClientIP+":"+RTSPHost;
System.out.print(arguments);
//________Commande definition _1rst méthode
String[] cmd={
commande,
"--ttl",
"12",
"-vvv",
"--color",
"-I",
"telnet",
"--telnet-password",
Password,
"--telnet-port",
TelnetPort+"",
" --rtsp-host",
ClientIP,
":",
RTSPHost+""
};
//________Commande definition _2nd méthode
String cmd1=commande+" "+arguments;
Process p=null;
Runtime r = Runtime.getRuntime();
p = r.exec(cmd1);
p.getOutputStream().close();
p.getInputStream().close();
p.destroy();
//____________Connect___________________________________________________________
try{
TelnetConnection tc=new TelnetConnection("localhost",TelnetPort);
String s = tc.Login(/*"",*/Password,100);
System.out.println("\n this 's S : \n"+s);
String prompt = s.trim();
System.out.println(prompt.length());
System.out.println(s.trim()+"\n");
prompt = s.substring(prompt.length()-1,prompt.length());
if (prompt.endsWith(">"))
System.out.println("\n"+"Successss"+"\n");
if (!prompt.equals("$") && !prompt.equals(">"))
System.out.println("Connection failed"+"\n");
//______________AddVOD__________________________________________________________
String query = "new " + FilmFileName + " vod enabled";
tc.WriteLine(query);
query = "setup " + FilmFileName + " input '" + FilePath + "'";
tc.WriteLine(query);
System.out.println( "rtsp://localhost:"+RTSPHost+"/" + FilmFileName);
query="show";
tc.WriteLine(query);
String y=tc.Read();
System.out.println(y);
}
catch (Exception e){
System.out.println(e.getMessage());
System.out.println("voilà le stack");
e.printStackTrace();
}
}
catch(Exception e){System.out.println("Catch my Main "+e.getMessage());}
}
} |
voilà la classe de la connexion telnet
Code:
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package VOD_Package;
import java.net.Socket;
//import org.apache.commons.net.telnet.TelnetClient;
//import java.util.Enumeration;
/**
*
* @author bassouma
*/
public class TelnetConnection
{
Socket tcpSocket;
int TimeOutMs = 100;
//______________________________________________________________________________
public TelnetConnection(String Hostname, int Port)
{
try{
tcpSocket = new Socket(Hostname, Port);
}
catch (Exception e) { System.out.println(e);}
}
//______________________________________________________________________________
public String Login(String Username, String Password, int LoginTimeOutMs)
{
int oldTimeOutMs = TimeOutMs;
TimeOutMs = LoginTimeOutMs;
String s = Read();
if (!s.trim().endsWith(":"))
System.out.println("Failed to connect : no login prompt");
WriteLine(Username);
s += Read();
if (!s.trim().endsWith(":"))
System.out.println("Failed to connect : no password prompt");
WriteLine(Password);
s += Read();
TimeOutMs = oldTimeOutMs;
return s;
}
//______________________________________________________________________________
public void WriteLine(String cmd)
{
Write(cmd + "\n");
}
//______________________________________________________________________________
public void Write(String cmd)
{
try{
if (!tcpSocket.isConnected()) return ;
//byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.replace("\0xFF", "\0xFF\0xFF"));
byte[] buf = cmd.replace("\0xFF", "\0xFF\0xFF").getBytes("ASCII");
tcpSocket.getOutputStream().write(buf, 0, buf.length);
}
catch (Exception e){System.out.println(e);}
}
//______________________________________________________________________________
public String Read()
{
try{
if (!tcpSocket.isConnected()) return null;
StringBuilder sb = new StringBuilder();
do
{
ParseTelnet(sb);
Thread.sleep(TimeOutMs);
} while (tcpSocket.getInputStream().available()> 0);
return sb.toString();
}
catch(Exception e){
return (e.toString());
}
}
//______________________________________________________________________________
public Boolean isConnected()
{
return tcpSocket.isConnected();
}
//______________________________________________________________________________
void ParseTelnet(StringBuilder sb)
{
try{
while (tcpSocket.getInputStream().available() > 0)
{
int input = tcpSocket.getInputStream().read();
switch (input)
{
case -1:
break;
case Verbs.IAC:
// interpret as command
int inputverb = tcpSocket.getInputStream().read();
if (inputverb == -1) break;
switch (inputverb)
{
case (int)Verbs.IAC:
//literal IAC = 255 escaped, so append char 255 to String
sb.append(inputverb);
break;
case (int)Verbs.DO:
case (int)Verbs.DONT:
case (int)Verbs.WILL:
case (int)Verbs.WONT:
// reply to all commands with "WONT", unless it is SGA (suppres go ahead)
int inputoption = tcpSocket.getInputStream().read();
if (inputoption == -1) break;
tcpSocket.getOutputStream().write((byte)Verbs.IAC);
if (inputoption == (int)Options.SGA)
tcpSocket.getOutputStream().write(inputverb == (int)Verbs.DO ? (byte)Verbs.WILL : (byte)Verbs.DO);
else
tcpSocket.getOutputStream().write(inputverb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
tcpSocket.getOutputStream().write((byte)inputoption);
break;
default:
break;
}
break;
default:
sb.append((char)input);
break;
}
}
}
catch (Exception e){ System.out.println(e) ;}
}
} |
la classe telnet connexion utilise deux classe (énumération) qu'on peut les définir en deux classes séparés
Code:
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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
/**
*
* @author bassouma
*/
public class Verbs {
public final static int WILL=251;
public final static int WONT=252;
public final static int DO=253;
public final static int DONT=254;
public final static int IAC=255;
}
//______________________________________________
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
/**
*
* @author bassouma
*/
public class Options {
public final static int SGA=3;
} |
J'ai finit tout mes outils sincèrement, j'ai plus d'idée ou solution:calim2:, merci d'avance pour m'aidez