关于网络,我有一个非常奇怪的问题。我创建了一个andorid应用程序,它可以安全地连接到PC上运行的服务器。请注意,尽管PC位于NAT之后,但相应的端口已被转发。 PC也被放入DMZ以避免任何端口转发问题。
问题在于,当我使用WIFI上的代码时,它可以很好地工作,但是,通过3G,SSLSocket的getOutputStream方法几乎总是(29个中的29个)块(在服务器端接受发生)。
你可以帮助我解决问题吗?
谢谢
客户代码:
E.log("establishing connection: trying to create context");
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
E.log("establishing connection: trying to create socket factory");
SSLSocketFactory factory = (SSLSocketFactory) context.getSocketFactory ();
E.log("establishing connection: trying to create socket");
kkSocket = (SSLSocket)
factory.createSocket(host, Integer.parseInt(port));
E.log("establishing connection: trying to create out writer");
out = new PrintWriter(kkSocket.getOutputStream(), true);
E.log("establishing connection: trying to create in reader");
in = new BufferedReader(new InputStreamReader
(kkSocket.getInputStream()));
记录失败:
04-15 00:07:59.066: E/Bubu(17340): starting thread
04-15 00:07:59.076: I/System.out(17340): Mon Apr 15 00:07:59 CEST 2013 - establishing connection: trying to create context
04-15 00:07:59.076: I/System.out(17340): Mon Apr 15 00:07:59 CEST 2013 - establishing connection: trying to create socket factory
04-15 00:07:59.076: I/System.out(17340): Mon Apr 15 00:07:59 CEST 2013 - establishing connection: trying to create socket
04-15 00:07:59.146: D/dalvikvm(17340): GC_CONCURRENT freed 190K, 5% free 7530K/7880K, paused 4ms+2ms, total 23ms
04-15 00:08:01.056: I/System.out(17340): Mon Apr 15 00:08:01 CEST 2013 - establishing connection: trying to create out writer
成功:
04-15 00:13:15.506: E/Bubu(17750): starting thread
04-15 00:13:15.526: I/System.out(17750): Mon Apr 15 00:13:15 CEST 2013 - establishing connection: trying to create context
04-15 00:13:15.536: I/System.out(17750): Mon Apr 15 00:13:15 CEST 2013 - establishing connection: trying to create socket factory
04-15 00:13:15.536: I/System.out(17750): Mon Apr 15 00:13:15 CEST 2013 - establishing connection: trying to create socket
04-15 00:13:15.556: I/System.out(17750): Mon Apr 15 00:13:15 CEST 2013 - establishing connection: trying to create out writer
04-15 00:13:15.746: I/System.out(17750): Mon Apr 15 00:13:15 CEST 2013 - establishing connection: trying to create in reader
04-15 00:13:15.746: I/System.out(17750): Mon Apr 15 00:13:15 CEST 2013 - from server:bubu