简体   繁体   English

基本java socket编程问题

[英]basic java socket programming problem

I have read a lot of the questions/replies to java socket programming, but none of them apply to my current issue.我已经阅读了很多对 java 套接字编程的问题/回复,但没有一个适用于我当前的问题。

I have written a basic java ssl socket program, whose sole purpose (for the moment) is to establish an ssl connection between the client and server and send one signed message by the client.我编写了一个基本的 java ssl 套接字程序,其唯一目的(目前)是建立一个 ssl 连接客户端和客户端之间的签名消息和服务器。 If the message gets verified by the server, it should reply accordingly with (true.) or otherwise with (false!).如果消息被服务器验证,它应该相应地回复 (true.) 或回复 (false!)。

I get stuck after the handshake pase, when the client sends a "message" to the server and waits for the server to respond.当客户端向服务器发送“消息”并等待服务器响应时,我在握手之后卡住了。 Meanwhile the server does not process the message the client sent, so it can't respond and there I am waiting for either of them go it over with.同时,服务器不处理客户端发送的消息,因此无法响应,我正在等待它们中的任何一个 go 结束。

Can you tell me where I made a mistake?你能告诉我我哪里做错了吗?

In order not to display huge amounts of not relevant code I have not included the value object SignedMessage nor the Utils class.为了不显示大量不相关的代码,我没有包含值 object SignedMessage 和 Utils class。 The methods used from the utils class are for serialization and deserialization and for converting a String to an array of bytes. utils class 中使用的方法用于序列化和反序列化以及将字符串转换为字节数组。 I know that I could have used String.getBytes(), but I liked to have the conversion done by hand.我知道我可以使用 String.getBytes(),但我喜欢手动完成转换。 All mentioned methods have been tested and work fine.所有提到的方法都已经过测试并且工作正常。

the ServerThread run method code ServerThread 运行方法代码

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");
}

This is the Client code which does the communication这是进行通信的客户端代码

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");
}

this这个

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

blocks forever.永远阻塞。 This actually will unblock when the stream (connection) is closed.这实际上会在 stream(连接)关闭时解除阻塞。 So it's up to you to identify end of message.因此,由您来确定消息的结尾。 I mean that end of message does not mean end of stream.我的意思是消息结束并不意味着 stream 结束。 So After you've received your message you will infinitely wait for smth else from the channel.因此,在您收到消息后,您将无限期地等待来自频道的其他消息。 As you actually did here正如你在这里所做的那样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM