basic java socket programming problem

倖福魔咒の 提交于 2019-12-13 03:31:53

问题


I have read a lot of the questions/replies to java socket programming, but none of them apply to my current issue.

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. If the message gets verified by the server, it should reply accordingly with (true!) or otherwise with (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.

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. The methods used from the utils class are for serialization and deserialization and for converting a String to an array of bytes. I know that I could have used String.getBytes(), but I liked to have the conversion done by hand. All mentioned methods have been tested and work fine.

the ServerThread run method code

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

回答1:


this

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

blocks forever. This actually will unblock when the stream (connection) is closed. So it's up to you to identify end of message. I mean that end of message does not mean end of 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);


来源:https://stackoverflow.com/questions/6138744/basic-java-socket-programming-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!