Bonjour,

Pour m'entrainer à coder en java j'aimerai réaliser un outil permettant de prendre le contrôle à distance d'une autre machine sur Windows et sur un même réseau. Comme le fait VNC ou logmein par exemple.

Pour se faire, avant chaque type d’envois de donnée (action clavier, souris, capture d'ecran, ...) j’envoie un message texte indiquant quelle va etre le prochain type de donnée afin de bien la réceptionner.

Actuellement, sur mon poste Client j'envoie juste le message "Capture" suivie d'un screenshot toute les 4 secondes, et du Coté serveur j'affiche juste le message reçu ansi que le screenshot.
Le problème est que le 1er message reçu s'affiche bien mais que pour tout les suivant la lettre y viens se glisser au début de mon message, je reçoit alors "yCapture" pour tout les message a partir de la 2ème réception. Si je décide de changer le message envoyé, la même erreur se reproduit, pour tout les message a partir du 2eme la lettre y vient se glisser en début de réception

Voici mon code :

coté client
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public class Emission implements Runnable {
 
	private PrintWriter out;
	//private String message = null;
	private Scanner sc = null;
	Socket socket = null;
 
	public Emission(Socket socketServer) {
		this.socket = socketServer;
 
	}
 
 
	public void run() {
 
		  sc = new Scanner(System.in);
 
		  while(true){
				try {
					Thread.sleep(2000);
					envoyerMsg(socket, "Capture");
					Thread.sleep(2000);
					envoyerCapture(socket);
 
 
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
 
			  }
	}
 
	static void envoyerMsg(Socket socketClient, String message) throws IOException
	{
		PrintWriter out = new PrintWriter(socketClient.getOutputStream());
        out.println(message);
        out.flush();
        System.out.println("Envoyé "+message);
 
	}
 
 static void envoyerCapture(Socket socket)  throws IOException {
 
		Robot robot = null;
		//Get default screen device
        GraphicsEnvironment gEnv=GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gDev=gEnv.getDefaultScreenDevice();
 
        //Get screen dimensions
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
 
        //Prepare Robot object
        try {
			robot = new Robot(gDev);
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 
 
		Dimension dimensionEcran = Toolkit.getDefaultToolkit().getScreenSize();
 
		ObjectOutputStream oos = null; //Used to write an object to the stream
		oos = new ObjectOutputStream(socket.getOutputStream());
		//oos.writeObject(new Rectangle(dimensionEcran));
 
		boolean test = true;
		while(test){
			test = false;
            //Capture screen
			BufferedImage captureEcran = null;
			captureEcran = robot.createScreenCapture(new Rectangle(dimensionEcran));
			 //capture d'ecran         dimension de la capture
 
 
            /* I have to wrap BufferedImage with ImageIcon because BufferedImage class
             * does not implement Serializable interface
             */
            ImageIcon imageIcon = new ImageIcon(captureEcran);
 
            //Send captured screen to the server
            try {
                System.out.println("before sending image");
                oos.writeObject(imageIcon);
                oos.reset(); //Clear ObjectOutputStream cache
                System.out.println("New screenshot sent");
            } catch (IOException ex) {
               ex.printStackTrace();
            }
 
            //wait for 100ms to reduce network traffic
            try{
                Thread.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
	}
Coté serveur

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public class Reception implements Runnable {
 
	static JFrame maFenetre = new JFrame();
	static JPanel J_connexion = new JPanel();
	private BufferedReader inMessage;
	private String message = null;
	private static Socket socket = null;
 
	public Reception(Socket socketcClient){
 
		this.socket = socketcClient;
		try {
			inMessage = new BufferedReader (new InputStreamReader (socketcClient.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
	public void run() {
 
		drawGUI();
 
		while(true){
	        try {	
 
			recevoirMsg(socket);
 
		    } catch (IOException e) {
 
				e.printStackTrace();
			}
		}
	}
 
 
	public static void drawGUI(){
 
		maFenetre.setSize(550, 400); // Ses dimensions
		maFenetre.setTitle("Connection distante"); // Son titre 
		maFenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		maFenetre.setLocationRelativeTo(null); //La Centre
		maFenetre.setVisible(true); // La rend visible
		maFenetre.setResizable(true); // Non redimensionnable
 
    }
 
	static void recevoirMsg(Socket socket) throws IOException{
		 BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
		 String message_distant = in.readLine();
		 System.out.println("Reçu : "+message_distant);
 
		switch (message_distant) {
		case "Capture":
			recevoirCapture(socket);
			break;
 
		default:
			recevoirCapture(socket);
			break;
		}
	 }
 
 
	static void recevoirCapture(Socket socket)  throws IOException {
 
		ObjectInputStream cObjectInputStream = null;
	    JPanel cPanel = null;
	    boolean continueLoop = true;
 
	            try {
 
	                //Read screenshots of the client then draw them
	                while(continueLoop){
	                	J_connexion = new JPanel();
	                	continueLoop = false;
	                    //Recieve client screenshot and resize it to the current panel size
 
	                	cObjectInputStream = new ObjectInputStream(socket.getInputStream());
	                    ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject();
	                    System.out.println("New image recieved");
	                    //Image image = imageIcon.getImage();
 
	                    JLabel contenuPanel = new JLabel(imageIcon);
	            		J_connexion.add(contenuPanel);
 
	            		maFenetre.add(J_connexion);
	            		maFenetre.revalidate();
 
	                    /*image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST);
	                    //Draw the recieved screenshot
	                    Graphics graphics = cPanel.getGraphics();
	                    graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);*/
	                }
	            } catch (IOException ex) {
	                ex.printStackTrace();
	          } catch(ClassNotFoundException ex){
	              ex.printStackTrace();
	          }
 
 
	}
Le plus surprenant est que si du coté client et serveur je supprime la réception et l'envoie du "screenshot" je reçoit le bon message "Capture" et non "yCapture" alors que les 2 fonctions sont indépendante