[问题] java 1.6 vs 1.8 ssl socket连线问题

楼主: JGC18 (JGC)   2015-03-26 11:34:25
各位好,我想请教一个java 1.6 vs 1.8的问题
我目前有一支程式要跟主机用SSL沟通,丢一些资料
底下是我连线,收送的部分
我在jre1.6环境下跑
不用disconnect,只要socket建通,直接write and read
不管送收几次都可以
但是只要环境切到1.8下,第一次执行收送是成功的,
但只要在马上执行一次同样动作
主机那边似乎就收到他们解码不出来的东西,所以没有回应我
我client就会timeout
我疑惑的是
"socket建立起来,第一次收送,成功正常,但socket没有断,只是再第二次送资料,结
果就是主机收到解不出来的东西"
不知道有没有人可以给我一点方向去查问题呢
感谢回答
//connect
public boolean connect() {
logger.info("Start");
TrustManager[] trustManager = null;
SSLContext context = null;
try {
trustManager = new TrustManager [] {
(TrustManager) new MyX509TrustManager(CAFilePath,clentCertPath)
};
context = SSLContext.getInstance("SSL");
context.init(null, trustManager, null);
SocketFactory sf = context.getSocketFactory();
sslSocket = (SSLSocket) sf.createSocket(mUrl, mPort);
sslSocket.setSoTimeout(10*1000);//timeout 10 sec
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
logger.error(e1.getMessage());
}
if(sslSocket==null)
return false;
logger.info("sslSocket OK");
logger.info("end");
return true;
}
//收送
public String sendRequest(String req){
try {
if (sslSocket == null) {
//throw new IOException(ICMASResponse.scErr_NoConnection);
logger.error("SSL Socket NoConnection");
return null;
//throw new IOException("SSL Socket NoConnection");
}
OutputStream out= sslSocket.getOutputStream();
InputStream in = sslSocket.getInputStream();
out.write(req.getBytes("UTF-8"));
out.flush();
String resp = null;
int len = in.read(mBuffer);
if (len > 0) {
resp = new String(Arrays.copyOf(mBuffer, len), "UTF-8");
}
logger.info("resp:"+resp);
//out.close(); //!!Note: close() means closeSocket
//in.close(); //!!Note: close() means closeSocket
return resp;
} catch(IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
return null;
}
}
// main
public static void main(String args[])
{
logger.info("AP Start");
SSL ssl = null;
Properties logp = new Properties();
try {
logp.load(SSL_Test.class.getClassLoader().
getResourceAsStream("log4j.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String req = "<1100>000</1100><1200>999</1200>";
String resp = null;
ssl = new SSL("192.168.1.23",
7000,
null,
null);
if(!ssl.connect())
{
logger.info("connect fail");
return;
}
//no.1
resp = ssl.sendRequest(req);
logger.info("1st resp:"+resp);
//no.2
resp = ssl.sendRequest(req);
logger.info("2nd resp:"+resp);
//no.3
resp = ssl.sendRequest(req);
logger.info("3th resp:"+resp);
} catch (Exception e)
{
logger.error(e.getMessage());
e.printStackTrace();
}
finally{
ssl.disconnect();
}
}
作者: reon (Traveler)   2015-04-24 23:44:00
我没用过1.8 但是Server端在收封包的时候 最好指定array大小

Links booklink

Contact Us: admin [ a t ] ucptt.com