设想
具有两个类的 ssl 客户端。一个用于交易,另一个用于发送网络消息。他们共享连接功能。如果我分别运行每个,则建立连接并收到适当的响应。如果我同时运行它们,则会建立连接,但出现诸如 Errorjava.io.UTFDataFormatException: malformed input around byte 135 socket timeouts 之类的错误,系统最终会冻结。
问题
我怎样才能同时运行网络和处理消息而不会发生冲突?
// connect code
try {
secureRandom = new SecureRandom();
secureRandom.nextInt();
System.out.println("Done.. Initializing Random Numbers.....:-");
String host = getHost();
int port = getPort();
setupServerKeystore();
setupClientKeyStore();
setupSSLContext();
SSLSocketFactory sf = sslContext.getSocketFactory();
SSLSocket socket = (SSLSocket) sf.createSocket(host, port);
socket.setSoTimeout(10000);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
this.din = new DataInputStream(in);
this.dout = new DataOutputStream(out);
// snip
// Schedule the two timers to run with different delays.
pingTimer.schedule(new Ping("ping"), 0, pingdelay);
vendTimer.schedule(new Vend("vend"), 0, venddelay);
} // end main
// 堆栈跟踪
vend - Current time: 20.04.2012 12:04:28
ping - Current time: 20.04.2012 12:04:28
Incoming Errorjava.io.UTFDataFormatException: malformed input around byte 135
java.io.UTFDataFormatException: malformed input around byte 135
at java.io.DataInputStream.readUTF(DataInputStream.java:639)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at timerexample.Ping.run(Ping.java:76)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
=========== Connected is True
null
ping - Current time: 20.04.2012 12:04:30
Incoming Errorjava.net.SocketTimeoutException: Read timed out
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789)
=========== Connected is True
null
vend - Current time: 20.04.2012 12:04:46
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:746)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at java.io.DataInputStream.readUTF(DataInputStream.java:592)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at timerexample.Ping.run(Ping.java:76)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Incoming Errorjava.net.SocketTimeoutException: Read timed out
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:746)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at java.io.DataInputStream.readUTF(DataInputStream.java:592)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at timerexample.Ping.run(Ping.java:76)
=========== Connected is True
null
ping - Current time: 20.04.2012 12:05:02
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
java.net.SocketTimeoutException: Read timed out
Incoming Errorjava.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:746)
=========== Connected is True
null
ping - Current time: 20.04.2012 12:05:17
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:50)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:320)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at timerexample.Ping.run(Ping.java:76)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)