传递byte []的Java ssl服务器/客户端应用程序

时间:2013-06-19 00:00:12

标签: java sockets

我在java(服务器/客户端)中创建一个使用安全连接(ssl)的应用程序。

主类具有套接字和流的初始化:

服务器主类:

public static void main(String[] arstring) {
            DataOutputStream out = null;
    DataInputStream in = null;
    SSLServerSocket sslserversocket = null;
    SSLSocket sslsocket = null;
 ...

客户主要类:

public static void main(String[] args) throws IOException {
    BufferedReader console;
    DataInputStream in = null;
    DataOutputStream out = null;
    SSLSocket sslsocket = null;
  ...

我在每个类中创建了相应的套接字和流:

服务器套接字和流:

SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory
                    .getDefault();
            sslserversocket = (SSLServerSocket) sslserversocketfactory
                    .createServerSocket(port);
            System.out.println("Server: listening on port: " + port + "\n");
            System.out.println("Waiting for connection...." + "\n");
            sslsocket = (SSLSocket) sslserversocket.accept();
            connected = true;
            System.out.println("Connection accepted \n");

            InputStream inputstreamconsola = System.in;
            InputStreamReader inputstreamreaderconsola = new InputStreamReader(
                    inputstreamconsola);
            BufferedReader bufferedreaderconsola = new BufferedReader(
                    inputstreamreaderconsola);

            in = new DataInputStream(sslsocket.getInputStream());
            out = new DataOutputStream(sslsocket.getOutputStream());

客户端套接字和流:

 SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
          sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);
          in = new DataInputStream(sslsocket.getInputStream());
          out = new DataOutputStream(sslsocket.getOutputStream());

我遇到的问题是当我想从服务器向客户端传递byte []时,byte []在服务器中具有正确的值,但它以空值到达客户端。 注意:当我使用readUTF()和writeUTF()传递字符串时,发送的值被正确接收

在这种情况下会出现“错误”:

服务器:

     byte[] nonceBytes = new byte[(int) 8];
     nonce = System.currentTimeMillis();
     nonceBytes = longToByteArray(nonce);

    System.out.print("\nNonce gerado: ");
        for (int i = 0; i < 8; i++)
         System.out.print(getHexString(nonceBytes[i]));
    System.out.print("\n");
            out.writeUTF("pass#");
    out.write(nonceBytes, (int) 0, nonceBytes.length); VALUE HERE IS CORRECT

客户端:

                byte[] nonceBytes = new byte[(int) 8];
                int nbytes = 0;

                // read the nonce sent by server
                try {
                    nbytes = in.read(nonceBytes); !!nonceBytes gets the null value instead of the value passed by the server!!
                } catch (IOException e) {
                    System.err.println("Read: " + e.getMessage());
                    readInput = false;
                    break;
                }

我想了解为什么我不能通过方法out.write()发送byte []并通过in.read()方法在客户端获取它。

1 个答案:

答案 0 :(得分:1)

您忽略了read().返回的结果您无法假设它填充缓冲区。在这种情况下,您可能应该使用DataInputStream.readFully()

请注意,此问题与SSL无关。