Mercurial > hg > trac > TreeVNC > TreeVNC
comparison src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 80:2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 30 Apr 2014 13:43:10 +0900 |
parents | cdf7bbe45885 |
children | ca39d762805e |
comparison
equal
deleted
inserted
replaced
79:015321785ddf | 80:2951dd85e9fc |
---|---|
26 this.proxyName = name; | 26 this.proxyName = name; |
27 } | 27 } |
28 | 28 |
29 public void openport() { | 29 public void openport() { |
30 try { | 30 try { |
31 // if(echoSocket==null) | |
32 echoSocket = new Socket(proxyName, echoPort); | 31 echoSocket = new Socket(proxyName, echoPort); |
33 echoSocket.setReuseAddress(true); | 32 echoSocket.setReuseAddress(true); |
34 os = new DataOutputStream(echoSocket.getOutputStream()); | 33 os = new DataOutputStream(echoSocket.getOutputStream()); |
35 is = new BufferedReader(new InputStreamReader( | 34 is = new BufferedReader(new InputStreamReader( |
36 echoSocket.getInputStream())); | 35 echoSocket.getInputStream())); |
37 } catch (UnknownHostException e) { | 36 } catch (UnknownHostException e) { |
38 System.err.println("Don't know about host: localhost"); | 37 System.err.println("Don't know about host: "+proxyName); |
39 } catch (IOException e) { | 38 } catch (IOException e) { |
40 System.out.println(proxyName + " Connection Faild"); | 39 System.out.println(proxyName + " Connection Faild"); |
41 System.exit(0); | 40 System.exit(0); |
42 } | 41 } |
43 | 42 |
49 * @return | 48 * @return |
50 */ | 49 */ |
51 public EchoClient requestHostName(String args) { | 50 public EchoClient requestHostName(String args) { |
52 if (echoSocket != null && os != null && is != null) { | 51 if (echoSocket != null && os != null && is != null) { |
53 try { | 52 try { |
54 | 53 InetAddress addr = echoSocket.getLocalAddress(); |
55 InetAddress addr = InetAddress.getLocalHost(); | 54 myAddress = addr.getHostAddress(); |
56 myAddress = new String(addr.getHostAddress()); | |
57 // add = getIpV6(); | |
58 | |
59 os.writeBytes(myAddress + "\n"); | 55 os.writeBytes(myAddress + "\n"); |
60 os.writeBytes(args + "\n"); | 56 os.writeBytes(args + "\n"); |
61 getProxyData(is); | 57 getProxyData(is, echoSocket); |
62 | 58 |
63 streamClose(); | 59 streamClose(); |
64 } catch (UnknownHostException e) { | 60 } catch (UnknownHostException e) { |
65 System.err.println("Trying to connect to unknown host: " + e); | 61 System.err.println("Trying to connect to unknown host: " + e); |
66 } catch (IOException e) { | 62 } catch (IOException e) { |
100 public boolean notfoundParent() { | 96 public boolean notfoundParent() { |
101 if (echoSocket != null && os != null && is != null) { | 97 if (echoSocket != null && os != null && is != null) { |
102 runflag = true; | 98 runflag = true; |
103 try { | 99 try { |
104 sendDataProxy("2", parentNum, null); | 100 sendDataProxy("2", parentNum, null); |
105 getProxyData(is); | 101 getProxyData(is, echoSocket); |
106 reConnectionMain(echoSocket); | 102 reConnectionMain(echoSocket); |
107 streamClose(); | 103 streamClose(); |
108 } catch (UnknownHostException e) { | 104 } catch (UnknownHostException e) { |
109 System.err.println("Trying to connect to unknown host: " + e); | 105 System.err.println("Trying to connect to unknown host: " + e); |
110 } catch (IOException e) { | 106 } catch (IOException e) { |
114 return true; | 110 return true; |
115 } | 111 } |
116 | 112 |
117 public EchoClient Interruption(Socket _clientSocket) { | 113 public EchoClient Interruption(Socket _clientSocket) { |
118 clientSocket = _clientSocket; | 114 clientSocket = _clientSocket; |
119 BufferedReader lostis = null; | |
120 | |
121 try { | 115 try { |
122 lostis = new BufferedReader(new InputStreamReader( | 116 BufferedReader lostis = new BufferedReader(new InputStreamReader( |
123 clientSocket.getInputStream())); | 117 clientSocket.getInputStream())); |
124 getProxyData(lostis); | 118 getProxyData(lostis, clientSocket); |
125 clientSocket.close(); | 119 clientSocket.close(); |
126 } catch (IOException e) { | 120 } catch (IOException e) { |
127 System.out.println(e); | 121 System.out.println(e); |
128 } | 122 } |
129 return this; | 123 return this; |
130 } | 124 } |
131 | 125 |
132 void getProxyData(BufferedReader is) throws IOException { | 126 void getProxyData(BufferedReader is, Socket soc) throws IOException { |
133 if ((parentAddress = is.readLine()) != null) { | 127 if ((parentAddress = is.readLine()) != null) { |
134 System.out.println("Server: " + parentAddress); | 128 System.out.println("Server received: " + parentAddress); |
135 } | 129 } |
136 if ((parentNum = is.readLine()) != null) { | 130 if ((parentNum = is.readLine()) != null) { |
137 System.out.println("parent: " + parentNum); | 131 System.out.println("parent: " + parentNum); |
138 } | 132 } |
139 if ((treeNum = is.readLine()) != null) { | 133 if ((treeNum = is.readLine()) != null) { |
140 System.out.println("treenum: " + treeNum); | 134 System.out.println("treenum: " + treeNum); |
141 } | 135 } |
142 if ((leaderFlag = is.readLine()) != null) { | 136 if ((leaderFlag = is.readLine()) != null) { |
143 System.out.println("leaderflag: " + leaderFlag); | 137 System.out.println("leaderflag: " + leaderFlag); |
144 } | 138 } |
145 } | 139 InetAddress parent = soc.getInetAddress(); |
146 | 140 parentAddress = parent.getHostName(); |
147 /* | 141 System.out.println("Actual Server: " + parentAddress); |
148 String getProxyData2(BufferedReader is) throws IOException { | 142 } |
149 String checkRepetition = null; | 143 |
150 if (!("1".equals(leaderFlag))) { | |
151 ServerSocket waitReplyServer = new ServerSocket(9999); | |
152 Socket socketForWait = waitReplyServer.accept(); | |
153 BufferedReader isNotLeader = new BufferedReader( | |
154 new InputStreamReader(socketForWait.getInputStream())); | |
155 System.out | |
156 .println("-------------------renode----------------------------"); | |
157 if ((parentAddress = isNotLeader.readLine()) != null) { | |
158 System.out.println("Server: " + parentAddress); | |
159 } | |
160 if ((parentNum = isNotLeader.readLine()) != null) { | |
161 System.out.println("parent:test " + parentNum); | |
162 } | |
163 if ((checkRepetition = isNotLeader.readLine()) != null) { | |
164 System.out.println("checkRepetition: " + checkRepetition); | |
165 } | |
166 try { | |
167 Thread.sleep(1000); | |
168 } catch (InterruptedException e) { | |
169 e.printStackTrace(); | |
170 } | |
171 } else { | |
172 System.out | |
173 .println("-------------------re----------------------------"); | |
174 if ((parentAddress = is.readLine()) != null) { | |
175 System.out.println("Server: " + parentAddress); | |
176 } | |
177 if ((parentNum = is.readLine()) != null) { | |
178 System.out.println("parent:test " + parentNum); | |
179 } | |
180 if ((checkRepetition = is.readLine()) != null) { | |
181 System.out.println("checkRepetition: " + checkRepetition); | |
182 } | |
183 } | |
184 return checkRepetition; | |
185 } | |
186 */ | |
187 void reConnectionMain(Socket echoSocket) { | 144 void reConnectionMain(Socket echoSocket) { |
188 while (true) { | 145 while (true) { |
189 try { | 146 try { |
190 client.closeApp(); | 147 client.closeApp(); |
191 // set Socket for connection in VncViewer. | 148 // set Socket for connection in VncViewer. |
235 } | 192 } |
236 } | 193 } |
237 | 194 |
238 public void getParentName() { | 195 public void getParentName() { |
239 if (clientSocket == null) { | 196 if (clientSocket == null) { |
240 // echo = new EchoClient(pHost, this); | |
241 openport(); | 197 openport(); |
242 requestHostName("1"); // 1 is normal connection type. | 198 requestHostName("1"); // 1 is normal connection type. |
243 } else { | 199 } else { |
244 Interruption(clientSocket); | 200 Interruption(clientSocket); |
245 } | 201 } |