开发者

Java: "Listening" to a DataStream

开发者 https://www.devze.com 2023-03-03 08:57 出处:网络
I have a program that connects to a server, and sends/receives data through an input and output data stream.

I have a program that connects to a server, and sends/receives data through an input and output data stream. I want to debug it and see if the messages that are being passed are correct, so I have to read whats being sent over the streams, but the connection has a 1 second timeout. (on the server side, and I cant control that) Is there a way to listen to the stream so I can get the data from it to the console AND have the data still be sent?

public void foo(DataInputStream in, DataOutputStream out){
    //Some code to make the console write whatever is sent in the ou开发者_如何学JAVAt stream
    //And received in the in stream. 

    out.writeUTF("SOME TEXT");
    String s = in.readUTF();
}


Jakarta Commons IO will help you. Where you create your DataInputStream, insert a TeeInputStream that writes to a ByteArrayOutputStream. You can then examine that byte array at your leisure.

0

精彩评论

暂无评论...
验证码 换一张
取 消