comparison src/myVncClient/MyVncClient.java @ 90:7a1e934144e6

modify RFB Protocol version 3.855.
author e085711
date Tue, 20 Sep 2011 13:42:01 +0900
parents 3ea33e683522
children d89e03d99b7f
comparison
equal deleted inserted replaced
89:3ea33e683522 90:7a1e934144e6
6 import java.net.*; 6 import java.net.*;
7 import java.util.Random; 7 import java.util.Random;
8 8
9 import java.nio.ByteBuffer; 9 import java.nio.ByteBuffer;
10 10
11 public class MyVncClient extends VncViewer implements InterfaceForViewer, java.lang.Runnable, 11 public class MyVncClient extends VncViewer implements InterfaceForViewer,
12 WindowListener { 12 java.lang.Runnable, WindowListener {
13 13
14 /** 14 /**
15 * 15 *
16 */ 16 */
17 private static final long serialVersionUID = 1L; 17 private static final long serialVersionUID = 1L;
49 pHost = "cls080.ie.u-ryukyu.ac.jp"; 49 pHost = "cls080.ie.u-ryukyu.ac.jp";
50 if (mainArgs.length > 1) 50 if (mainArgs.length > 1)
51 port = Integer.parseInt(mainArgs[1]); 51 port = Integer.parseInt(mainArgs[1]);
52 else 52 else
53 port = 5999; 53 port = 5999;
54 54
55 init(); 55 init();
56 start_threads(); 56 start_threads();
57 start(); 57 start();
58 } 58 }
59 59
127 if (first) { 127 if (first) {
128 connectAndAuthenticate(); 128 connectAndAuthenticate();
129 accThread = new Thread(new AcceptThread(rfb, 5999)); 129 accThread = new Thread(new AcceptThread(rfb, 5999));
130 accThread.start(); 130 accThread.start();
131 first = false; 131 first = false;
132 }else { 132 } else {
133 System.out.println("reConnectAndAuthenticate() "); 133 System.out.println("reConnectAndAuthenticate() ");
134 reConnectAndAuthenticate(); 134 reConnectAndAuthenticate();
135 accThread = new Thread(new AcceptThread(rfb, 5999)); 135 accThread = new Thread(new AcceptThread(rfb, 5999));
136 accThread.start(); 136 accThread.start();
137 } 137 }
142 142
143 // rfb.readPngData(); 143 // rfb.readPngData();
144 // vc.drawFirstImage(); 144 // vc.drawFirstImage();
145 145
146 } catch (IOException e) { 146 } catch (IOException e) {
147 try{ 147 try {
148 rfb.sock.close(); 148 rfb.sock.close();
149 149
150 }catch(IOException e2){ 150 } catch (IOException e2) {
151 e2.printStackTrace(); 151 e2.printStackTrace();
152 } 152 }
153 System.out.println("Socket error"); 153 System.out.println("Socket error");
154 // parent no find 154 // parent no find
155 Random rnd = new Random(); 155 Random rnd = new Random();
159 /** 159 /**
160 * this while reconnection 160 * this while reconnection
161 */ 161 */
162 162
163 int counter = 0; 163 int counter = 0;
164 vncFrame.setVisible(false); 164 vncFrame.setVisible(false);
165 vncFrame.dispose(); 165 vncFrame.dispose();
166 166
167 while (true) { 167 while (true) {
168 /** 168 /**
169 * if my last node case reconnectoion stop 169 * if my last node case reconnectoion stop
170 */ 170 */
171 171
172 echoValue = new EchoClient(echoValue, this); 172 echoValue = new EchoClient(echoValue, this);
173 //echoValue = new EchoClient(echoValue); 173 // echoValue = new EchoClient(echoValue);
174 174
175 try { 175 try {
176 Thread.sleep(ran); 176 Thread.sleep(ran);
177 } catch (InterruptedException e1) { 177 } catch (InterruptedException e1) {
178 e1.printStackTrace(); 178 e1.printStackTrace();
179 } 179 }
188 if (echoValue.losthost()) { 188 if (echoValue.losthost()) {
189 break; 189 break;
190 } 190 }
191 counter++; 191 counter++;
192 } 192 }
193 193
194 // System.exit(0); 194 // System.exit(0);
195 } catch (Exception e) { 195 } catch (Exception e) {
196 System.out.println(e); 196 System.out.println(e);
197 System.exit(0); 197 System.exit(0);
198 } 198 }
244 fatalError("Network error: server name unknown: " + host, e); 244 fatalError("Network error: server name unknown: " + host, e);
245 } catch (ConnectException e) { 245 } catch (ConnectException e) {
246 fatalError("Network error: could not connect to server: " + host 246 fatalError("Network error: could not connect to server: " + host
247 + ":" + port, e); 247 + ":" + port, e);
248 } catch (EOFException e) { 248 } catch (EOFException e) {
249 249
250 vncFrame.setVisible(false); 250 vncFrame.setVisible(false);
251 vncFrame.dispose(); 251 vncFrame.dispose();
252 // num4 252 // num4
253 if ( leaderflag != null) { 253 if (leaderflag != null) {
254 while (true) { 254 while (true) {
255 //echoValue = new EchoClient(echoValue, this); 255 // echoValue = new EchoClient(echoValue, this);
256 echoValue = new EchoClient(echoValue); 256 echoValue = new EchoClient(echoValue);
257 echoValue.openport(); 257 echoValue.openport();
258 // runflag = echo.losthost(); 258 // runflag = echo.losthost();
259 if (echoValue.losthost()) { 259 if (echoValue.losthost()) {
260 break; 260 break;
376 376
377 rfb.writeVersionMsg(); 377 rfb.writeVersionMsg();
378 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor 378 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
379 + "." + rfb.clientMinor); 379 + "." + rfb.clientMinor);
380 380
381 381 if (rfb.serverMinor == 855) {
382 if(rfb.serverMinor == 855) { 382 boolean useEchoFlag = rfb.readProxyFlag();
383 byte[] b = new byte[4]; 383 if (useEchoFlag) {
384 b = rfb.readEchoPort(); 384 byte[] b = new byte[4];
385 echoPort = castByteInt(b); 385 b = rfb.readEchoPort();
386 386 echoPort = castByteInt(b);
387 InetAddress addr = InetAddress.getByName(pHost); 387
388 String h = new String(addr.getHostAddress()); 388 InetAddress addr = InetAddress.getByName(pHost);
389 389 String h = new String(addr.getHostAddress());
390 getParentName(); 390
391 if(!(h.equals(host))) { 391 getParentName();
392 rfb.changeParent(host, port); 392 if (!(h.equals(host))) {
393 rfb.readVersionMsg(); 393 rfb.changeParent(host, port);
394 rfb.writeVersionMsg(); 394 rfb.readVersionMsg();
395 } 395 rfb.writeVersionMsg();
396 } 396 boolean flag = rfb.readProxyFlag();
397 397 }
398 398 }
399 }
400
399 int secType = rfb.negotiateSecurity(); 401 int secType = rfb.negotiateSecurity();
400 int authType; 402 int authType;
401 if (secType == RfbProto.SecTypeTight) { 403 if (secType == RfbProto.SecTypeTight) {
402 showConnectionStatus("Enabling TightVNC protocol extensions"); 404 showConnectionStatus("Enabling TightVNC protocol extensions");
403 rfb.setupTunneling(); 405 rfb.setupTunneling();
432 if (inSeparateFrame) { 434 if (inSeparateFrame) {
433 vncFrame.pack(); 435 vncFrame.pack();
434 vncFrame.setVisible(true); 436 vncFrame.setVisible(true);
435 } else { 437 } else {
436 validate(); 438 validate();
437 } 439 }
438 440
439 showConnectionStatus("Connecting to " + host + ", port " + port + "..."); 441 showConnectionStatus("Connecting to " + host + ", port " + port + "...");
440 442
441 rfb.changeParent(host, port); 443 rfb.changeParent(host, port);
442 444
448 450
449 rfb.writeVersionMsg(); 451 rfb.writeVersionMsg();
450 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor 452 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
451 + "." + rfb.clientMinor); 453 + "." + rfb.clientMinor);
452 454
453 455 if (rfb.serverMinor == 855) {
454 if(rfb.serverMinor == 855) { 456 boolean useEchoFlag = rfb.readProxyFlag();
455 byte[] b = new byte[4]; 457 if (useEchoFlag) {
456 b = rfb.readEchoPort(); 458 byte[] b = new byte[4];
457 echoPort = castByteInt(b); 459 b = rfb.readEchoPort();
458 460 echoPort = castByteInt(b);
459 InetAddress addr = InetAddress.getByName(host); 461
460 String h = new String(addr.getHostAddress()); 462 InetAddress addr = InetAddress.getByName(pHost);
461 463 String h = new String(addr.getHostAddress());
462 getParentName(); 464
463 if(!(h.equals(host))) { 465 getParentName();
464 rfb.changeParent(host, port); 466 if (!(h.equals(host))) {
465 rfb.readVersionMsg(); 467 rfb.changeParent(host, port);
466 rfb.writeVersionMsg(); 468 rfb.readVersionMsg();
467 } 469 rfb.writeVersionMsg();
468 } 470 }
469 471 }
470 472 }
471 int secType = rfb.negotiateSecurity(); 473 int secType = rfb.negotiateSecurity();
472 int authType; 474 int authType;
473 if (secType == RfbProto.SecTypeTight) { 475 if (secType == RfbProto.SecTypeTight) {
474 showConnectionStatus("Enabling TightVNC protocol extensions"); 476 showConnectionStatus("Enabling TightVNC protocol extensions");
475 rfb.setupTunneling(); 477 rfb.setupTunneling();
493 } 495 }
494 break; 496 break;
495 default: 497 default:
496 throw new Exception("Unknown authentication scheme " + authType); 498 throw new Exception("Unknown authentication scheme " + authType);
497 } 499 }
498 } 500 }
501
499 // 502 //
500 // Show a message describing the connection status. 503 // Show a message describing the connection status.
501 // To hide the connection status label, use (msg == null). 504 // To hide the connection status label, use (msg == null).
502 // 505 //
503 506
821 * if (host == null) { host = getCodeBase().getHost(); if 824 * if (host == null) { host = getCodeBase().getHost(); if
822 * (host.equals("")) { fatalError("HOST parameter not specified"); } } 825 * (host.equals("")) { fatalError("HOST parameter not specified"); } }
823 * 826 *
824 * port = readIntParameter("PORT", 5550); 827 * port = readIntParameter("PORT", 5550);
825 */ 828 */
826 829
827 // Read "ENCPASSWORD" or "PASSWORD" parameter if specified. 830 // Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
828 readPasswordParameters(); 831 readPasswordParameters();
829 832
830 String str; 833 String str;
831 if (inAnApplet) { 834 if (inAnApplet) {
1025 showMessage(str); 1028 showMessage(str);
1026 } else { 1029 } else {
1027 System.exit(1); 1030 System.exit(1);
1028 } 1031 }
1029 } 1032 }
1030 1033
1031 // 1034 //
1032 // Show message text and optionally "Relogin" and "Close" buttons. 1035 // Show message text and optionally "Relogin" and "Close" buttons.
1033 // 1036 //
1034 1037
1035 void showMessage(String msg) { 1038 void showMessage(String msg) {
1136 public void windowIconified(WindowEvent evt) { 1139 public void windowIconified(WindowEvent evt) {
1137 } 1140 }
1138 1141
1139 public void windowDeiconified(WindowEvent evt) { 1142 public void windowDeiconified(WindowEvent evt) {
1140 } 1143 }
1141 1144
1142 public void getParentName() { 1145 public void getParentName() {
1143 if (echoValue == null) { 1146 if (echoValue == null) {
1144 1147
1145 if (clientSocket == null) { 1148 if (clientSocket == null) {
1146 1149
1175 } 1178 }
1176 1179
1177 public void setEchoValue(EchoClient value) { 1180 public void setEchoValue(EchoClient value) {
1178 this.echoValue = value; 1181 this.echoValue = value;
1179 } 1182 }
1183
1180 int castByteInt(byte[] b) { 1184 int castByteInt(byte[] b) {
1181 ByteBuffer bb = ByteBuffer.wrap(b); 1185 ByteBuffer bb = ByteBuffer.wrap(b);
1182 int value = bb.getInt(); 1186 int value = bb.getInt();
1183 return value; 1187 return value;
1184 } 1188 }
1185 1189
1186 public void setClientSocket(Socket sock) { 1190 public void setClientSocket(Socket sock) {
1187 clientSocket = sock; 1191 clientSocket = sock;
1188 } 1192 }
1189 1193
1190 public void close() { 1194 public void close() {
1191 rfb.close(); 1195 rfb.close();
1192 vncFrame.setVisible(false); 1196 vncFrame.setVisible(false);
1193 vncFrame.dispose(); 1197 vncFrame.dispose();
1194 } 1198 }
1195 1199
1196
1197 } 1200 }