java.net.SocketException: Malformed reply from SOCKS server

本文探讨了当IE浏览器设置为SOCKS代理时,NetBeans Java桌面应用程序连接数据库失败的问题。文章分析了故障原因,并提供了两种解决方案:一是通过代码调用ProxySelector.getDefault(),二是设置System.setProperty(java.net.useSystemProxies false)。


当IE设置有SOCKS(套接字)代理时,NetBeans Java Desktop Application也就是GUI窗口程序连接数据库总是会失败。如果关闭代理的设置,程序则能正常运行。

程序表现为:长时间处于连接状态而不返回。监视TCP连接,发现程序长时间连接到代理的IP上。

大约5分钟后,连接被断开,返回如下异常:
2012-05-05 13:16:15,984 ERROR [101001003] (WorkerThread.java:73) - com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.SocketException: Malformed reply from SOCKS server

这个问题肯定与IE代理有关,但System.getProperty()对"http.proxyHost"、"htttps.proxyHost"、"socksProxyHost"得到的结果都是null。

于是跟踪DriverManager.getConnection()代码到 class java.net.SocksSocketImpl 的 protected void connect(SocketAddress endpoint, int timeout) 方法中,看到如下调用:
ProxySelector.getDefault();

根据 ProxySelector.getDefault() 和 java 两个关键字google找到一篇资料:


Java Database Connectivity (JDBC) - JDBC Microsoft SQL Problem

得知问题的原因在于 NetBeans Swing Application Framework 的 org.jdesktop.application.Application.create() 调用了
System.setProperty("java.net.useSystemProxies", "true");
其中提出了两种解决办法:
public void main(String[] args) {
ProxySelector.getDefault();
// code
}
或者
@Override
protected void startup() {
invoke System.setProperty("java.net.useSystemProxies", "false");
// code
}

其它参考
Java Networking and Proxies


===========================================================================================================


Hi, I got RSEG1243 error when conecting from RDz v801 client. The host version is also 801


My another PC works fine. So I guess the host setup should be correct. Any idea for the error? Great thanks for help!

java.net.SocketException: Malformed reply from SOCKS server
java.net.SocketException: Malformed reply from SOCKS server

at java.net.SocksSocketImpl.readSocksReply(Unknown Source)

at java.net.SocksSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at org.eclipse.dstore.core.client.ClientConnection.connectDaemon(Unknown Source)

at com.ibm.etools.zseries.util.DStoreWithSSHConnectorService.launchServer(Unknown Source)

at com.ibm.etools.zseries.util.DStoreWithSSHConnectorService.launchServer(Unknown Source)

at org.eclipse.rse.connectorservice.dstore.DStoreConnectorService.connectWithDaemon(Unknown Source)

at org.eclipse.rse.connectorservice.dstore.DStoreConnectorService.internalConnect(Unknown Source)

at org.eclipse.rse.core.subsystems.AbstractConnectorService$1.run(Unknown Source)

at org.eclipse.rse.core.subsystems.AbstractConnectorService$SafeRunner.run(Unknown Source)

at org.eclipse.rse.core.subsystems.AbstractConnectorService.connect(Unknown Source)

at org.eclipse.rse.core.subsystems.SubSystem.connect(Unknown Source)

at org.eclipse.rse.internal.ui.actions.SystemConnectAllSubSystemsAction$ConnectAllJob.run(Unknown Source)

at org.eclipse.core.internal.jobs.Worker.run(Unknown Source)


A suggestion given by our server side expert...

Can you check to see if the PC w/ the connectivity issue have the same SOCKS server and the same JDK level as the other one that can connect?

Steven

-------------------

Hi, Thanks for the idea, this is exactly the problem!

We found the "SOCKS" box is checked in the Perference --> Network Connection. And this setting is imported from client PC's IE setup automatically.

We change the IE setup to uncheck the SOCKS option and it works now.

Also we found in RDz v7.1 it won't import IE setup automatically, so the problem is not exist in v7.1 client.





### Java网络编程中的`java.net.SocketException: Unexpected end of file from server`解决方案 当遇到`java.net.SocketException: Unexpected end of file from server`异常时,这通常意味着客户端尝试读取来自服务器的数据却意外终止。这种情况可能由多种因素引起,包括但不限于网络不稳定、服务器端程序崩溃或配置不当等[^1]。 #### 一、检查并优化网络环境 确保本地计算机到目标服务器之间的网络连接稳定可靠。可以考虑通过ping命令测试延迟情况以及是否存在丢包现象;另外也可以借助traceroute工具来排查路径上是否有节点出现问题。 #### 二、调整超时设置 适当增加socket操作的timeout时间参数可以帮助缓解因短暂性的网络波动而引发的问题。对于HttpClient而言,可以通过设定合理的connection timeout和so timeout值来进行优化处理: ```java // 设置HttpParams对象的相关属性 params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // 连接建立超时时长设为5秒 params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); // 数据传输等待最大时限设为10秒 ``` #### 三、验证服务端状态与配置 确认所访问的服务处于正常工作状态下,并且能够正确响应请求。如果是在开发环境中,则需特别留意Tomcat或其他应用容器的具体配置项,比如上述提到过的HTTP Connector部分是否合理设置了port、protocol及redirectPort等相关参数[^3]。 #### 四、改进代码逻辑结构 针对可能出现异常的情况做好充分预判,在捕获到此类SocketException之后采取重试机制或是记录日志以便后续分析原因所在。下面给出一段简单的示例代码用于展示如何优雅地应对该类问题: ```java public String sendPostRequest(String urlStr){ HttpURLConnection conn = null; int retryCount = 3; // 定义最多允许失败次数 while(retryCount-- > 0){ try{ URL url = new URL(urlStr); conn = (HttpURLConnection)url.openConnection(); // ...其他必要的初始化操作... InputStream is = conn.getInputStream(); // 可能抛出IOException BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line).append("\n"); } return response.toString().trim(); }catch(SocketException e){ System.err.println("发生Socket异常:" + e.getMessage()); continue; // 尝试重新发送请求 }finally{ if(conn!=null){ conn.disconnect(); } } } throw new RuntimeException("经过多次尝试仍无法成功获取数据!"); } ``` 以上方法综合运用可以从不同角度有效减少乃至避免`Unexpected end of file from server`这类SocketException的发生概率,提高系统的健壮性和用户体验度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值