java - 基本的java套接字编程问题

标签 java sockets inputstream outputstream

我读过很多关于java套接字编程的问题/答复,但没有一个适用于我当前的问题。

我编写了一个基本的java ssl套接字程序,其唯一目的(目前)是在客户端和服务器之间建立ssl连接并发送一条由客户端签名的消息。如果消息得到服务器的验证,它应该相应地回复(true!),否则回复(false!)。

握手后,当客户端向服务器发送“消息”并等待服务器响应时,我陷入困境。同时,服务器不处理客户端发送的消息,因此它无法响应,我正在等待他们中的任何一个来完成它。

你能告诉我哪里出错了吗?

为了不显示大量不相关的代码,我没有包含值对象 SignedMessage 或 Utils 类。 utils 类中使用的方法用于序列化和反序列化以及将 String 转换为字节数组。我知道我可以使用 String.getBytes(),但我喜欢手动完成转换。所有提到的方法都经过测试并且工作正常。

ServerThread run方法代码

public void run() {
    try {
        InputStream in = sslSocket.getInputStream();
        OutputStream out = sslSocket.getOutputStream();

        //if I write some output here the program works well, but this is not what I want to do 
        //out.write('!');

        //BASE64 DECODING
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int ch;
        while((ch = in.read()) != -1)
            bos.write(ch);

        ByteArrayOutputStream decoded = new ByteArrayOutputStream();
        Base64Encoder encoder = new Base64Encoder();
        encoder.decode(bos.toByteArray(), 0, bos.toByteArray().length, decoded);

        //reconstructing the the SignedMessage object

        SignedMessage signedMessage = (SignedMessage) Utils.deserializeObject(decoded.toByteArray());

        if(checkSignature(signedMessage.getMessage().getBytes("UTF-8"), signedMessage.getSignature(), signedMessage.getCertificate())){
            System.out.println(String.valueOf(signedMessage.getMessage()));

            //this i where I would like to write out the answer, but the code never get executed
            out.write(Utils.toByteArray("TRUE!"));
            out.flush();
        }
        else {
            out.write(Utils.toByteArray("false!"));
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            sslSocket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    System.out.println("Thread closed");
}

这是进行通信的客户端代码

static void doProtocol(SSLSocket sslSocket) throws IOException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException  {

    OutputStream     out = sslSocket.getOutputStream();
    InputStream      in = sslSocket.getInputStream();

    byte[] signature = generateSignature(Utils.toByteArray(Message), (PrivateKey) clientStore.getKey(Utils.CLIENT_NAME, Utils.CLIENT_PASSWORD), 
            clientStore.getCertificate(Utils.CLIENT_NAME).getPublicKey().getAlgorithm());

    //BASE64 ENCODING
    byte[] object = Utils.serializeObject(new SignedMessage(Message, signature, clientStore.getCertificate(Utils.CLIENT_NAME)));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Base64Encoder encoder = new Base64Encoder();
    encoder.encode(object, 0, object.length, bos);

    out.write(bos.toByteArray());
    out.flush();

    int ch;
    while((ch = in.read()) != '!')
        bos.write(ch);

    System.out.println("Sesion closed");
}

最佳答案

这个

while((ch = in.read()) != -1)
        bos.write(ch);

永远阻塞。当流(连接)关闭时,这实际上会解除阻塞。 因此,由您来确定消息的结尾。 我的意思是消息结束并不意味着流结束。因此,在收到消息后,您将无限期地等待 channel 中的其他消息。 正如您实际上在这里所做的那样

while((ch = in.read()) != '!')
    bos.write(ch);

关于java - 基本的java套接字编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6138744/

相关文章:

node.js - SocketIo.use(function(socket, next)) - 下一步去哪里,如何捕获或接收它?

inputstream - Linux 设备驱动程序调用时计数错误

java - 在 Scala 中,是否有一种安全、快捷的方式将 InputStream 写入文件?

java - 如何在 chrome selenium 中以特定缩放尺寸运行所有脚本?

c++ - 用于游戏(和其他任务)的简单而有效的 UDP 服务器策略

c - 本地套接字绑定(bind)错误

java - 在 Java 中通过 HTTP 列出 apache 服务器上的文件/子文件夹

java - 运行后如何找到 Javas 堆的最大大小?

java - 如何在带有变量的 PreparedStatement 中使用 SQL 通配符

Java "The label Label244 is missing"错误