我有一个客户端/服务器对,我在其中有这个消息传递应用程序,我正在尝试添加SSL支持。客户端/服务器通常使用DataInputStream和DataOutputStream来发送/接收它们的信息,使用字节数组(DataOutputStream.write(byte [])和DataInputStream.readFully(byte []))。但由于某种原因,这不适用于SSL套接字。我已经尝试使用DataOutputStream.writeUTF()和DataInputStream.readUTF(),它的工作原理。我甚至尝试使用其他类型的读/写器,如BufferedReader / BufferedWriter,它们可以工作,但是当我尝试使用字节时,它不会发送任何东西。更奇怪的是,在检查双方的DataOutputStream.size()之后,他们都打印出他们已经发送过的数据。
服务器端:
logger.info("Waiting to accept a new client socket");
clientSocket = (SSLSocket) serverSocket.accept();
logger.info("Socket accepted successfully");
DataOutputStream dataOutputStream=newDataOutputStream(clientSocket.getOutputStream());
DataInputStream dataInputStream=new DataInputStream(clientSocket.getInputStream());
logger.trace("Socket is going to read");
int data=dataInputStream.available();
byte[] message=new byte[data];
dataInputStream.readFully(message);
logger.trace("Message received: {}",message);
logger.trace("Gonna respond");
if(message.length>0){
dataOutputStream.write(message,0,message.length);
}else{
String response=new String("wot?".getBytes(),"ASCII");
dataOutputStream.write(response.getBytes(),0,response.getBytes().length);
}
dataOutputStream.flush();
logger.trace("Chars written: {}",dataOutputStream.size());
客户方:
SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
logger.trace("SSL Socket factory created");
javax.net.ssl.SSLSocket sslSocket = (javax.net.ssl.SSLSocket)sslsocketfactory.createSocket(url, port);
logger.trace("SSL Socket created");
sslSocket.setSoTimeout(6000);
outputStream = sslSocket.getOutputStream();
inputStream=sslSocket.getInputStream();
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
DataInputStream dataInputStream=new DataInputStream(inputStream);
logger.trace("Input and output created");
//dataOutputStream.writeBytes(frame);
//dataOutputStream.writeUTF(frame);
dataOutputStream.write(frame.getBytes(),0,frame.getBytes().length);
dataOutputStream.flush();
logger.trace("Frame {} sent",frame);
logger.trace("Bytes sent: {}",dataOutputStream.size());
int data=dataInputStream.available();
byte[] messageRead = new byte[data];
dataInputStream.readFully(messageRead);
任何帮助都将受到高度赞赏。我一直在四处寻找,我不知道还有什么可以尝试。