简体   繁体   English

使用Java从服务器读取数据所需的帮助

[英]Help needed with reading data from server in java

i am having a bit of trouble with DataInputStreams, 我在使用DataInputStreams时遇到了麻烦,

So i have data coming for a local server, i know that the bytes i read in will follow this format 所以我有要用于本地服务器的数据,我知道读取的字节将遵循这种格式

0x01 to specify it is a string 0x01指定它是一个字符串

then random amount of bytes 然后随机的字节数

followed by trailing 0x00 0x00, 后面跟着0x00 0x00,

i am having trouble reading from the server though, 我虽然无法从服务器读取信息,

here is my method for reading 这是我的阅读方法

    public static String convertFromServer(DataInputStream dis) throws IOException{
    //Buffer to hold bytes being read in
    ByteArrayOutputStream buf = new ByteArrayOutputStream();

    if(dis.read() != -1){
        //Check to see if first byte == 0x01
    if(dis.read() == 0x01){
        //Check if byte dosnt equal 0x00, i need it to check if it is actually 0x00 0x00
        while(dis.read() != 0x00){
            buf.write(dis.read());
        }
    }

    if(dis.read() == 0x03){    

        while(dis.read() != 0x00){

            buf.write(dis.read());
        }
    }
    }
    String messageRecevied = new String(buf.toByteArray());
    return messageRecevied;

}

If i am a little ambiguous let me know. 如果我有点am昧,请告诉我。

i am getting data back , its just not fully correct, basically what i do is send across a byte array, with with the first element being 0x01 to specify string, then the string in bytes and then finally the last 2 elements are 0x00 and 0x00, then this data is then sent back to me from the server, the server definitely receives the data, just when i am reading back it not correct, letter will be missing 我要取回数据,它只是不完全正确,基本上我要做的是跨字节数组发送,第一个元素为0x01以指定字符串,然后以字节为单位的字符串,最后最后两个元素为0x00和0x00 ,然后将此数据从服务器发送回给我,服务器肯定会接收到该数据,只是当我读回它不正确时,会丢失字母

this code writes encodes the data i nthe format 0x01, then message in bytes, then 0x00,0x00 此代码以0x01格式写入数据,然后以字节为单位消息,然后为0x00,0x00

  public static void writeStringToBuffer(ByteArrayOutputStream buf,String message){

    buf.write(0x01);

    byte[] b = message.getBytes();

    for(int i =1; i<message.getBytes().length+1;i++ ){

        buf.write(b[i-1]);

     }
    buf.write(0x00);
    buf.write(0x00);


}

Something that always gets me when I'm doing socket work is making sure the orders are reversed on the server and the client. 在执行套接字工作时,总能得到我的东西是确保在服务器和客户端上颠倒订单。

If the server reads first, the client has to write first. 如果服务器先读取,则客户端必须先写入。 If the Server writes first, the client has to read first. 如果服务器先写入,则客户端必须先读取。 If they're both trying to read or both trying to write, you won't get any output. 如果他们都试图阅读或都试图写,则不会获得任何输出。

My recommendation is to do this: 我的建议是这样做:

if(dis.read() == 0x01) {
    int zeroesInARow = 0;

    while(zeroesInARow < 2) {
        int b = dis.read()
        if(b == 0x00) zeroesInARow++;
        else zeroesInARow = 0;
        buf.write(b);
    }
    String rawMessage = new String(buf.toArray());
    // take off the last two 0s
    String messageRecevied = rawMessage.substring(0,rawMessage.length()-2);
    return messageRecevied;
}

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

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