java用socket通信连接C服务端,每次通信后都有断开连接,直接在eclipse上测试时正常的,但是打成jar包放到tomcat里面运行的时候每次都断不开,结果造成端口一直被占用,有大神知道这个怎么解决么,下面是通信的具体代码:
public String reconfig(String msg) {
try {
TelnetClient telnet = new TelnetClient();
telnet.connect(hostip, hostport);
telnet.setSoTimeout( 60000 );
PrintStream out = new PrintStream(telnet.getOutputStream());
out.write(msg.trim().getBytes());
out.write("\r".getBytes());
out.flush();
try {
Thread.sleep(50);
} catch(Exception ex) {}
DataInputStream din = new DataInputStream(telnet.getInputStream());
byte[] btLen = new byte[4];
btLen[0] = din.readByte();
btLen[1] = din.readByte();
btLen[2] = din.readByte();
btLen[3] = din.readByte();
String strLen = new String(btLen);
int len = Integer.parseInt(strLen.trim());
if(len == 0) {
return "";
}
byte[] msgArr = new byte[len];
int index = 0;
while(true) {
msgArr[index] = din.readByte();
index += 1;
if(index == len) {
break;
}
}
String receiMsg = new String(msgArr);
telnet.disconnect();
return receiMsg;
} catch(Exception ex) {
ex.printStackTrace();
return "";
}
}
------解决思路----------------------
DataInputStream 没关闭吧
------解决思路----------------------
能发一下具体的错误信息吗,按道理说,客户端的端口是系统自动分配的,就算是100个并发端口也不会重复的
你这是个socket通信,怎么会部署到tomcat里,是不是你的server端口和tomcat的某些端口冲突了
------解决思路----------------------
TelnetClient telnet = new TelnetClient();使用完之后要调用telnet.disconnect()关闭连接。
out
还要在catch里面加,或者在finally里面统一处理一下。