Java Sockets

02/20/2012 18:29 Vinator#1
Hello again,
Now that the packet sending works properly I've got a problem with the Reading. When I receive bytes from 80 - 9F I get wrong data.
Left expected, right actual data:
Code:
80 - 128 - 8364
81 - 129 - 65533 #
82 - 130 - 8218
83 - 131 - 402
84 - 132 - 8222
85 - 133 - 8230
86 - 134 - 8224
87 - 135 - 8225
88 - 136 - 710
89 - 137 - 8240
8A - 138 - 352
8B - 139 - 8249
8C - 140 - 338
8D - 141 - 65533 #
8E - 142 - 381
8F - 143 - 65533 #
90 - 144 - 65533 #
91 - 145 - 8216
92 - 146 - 8217
93 - 147 - 8220
94 - 148 - 8221
95 - 149 - 8226
96 - 150 - 8211  #
97 - 151 - 8211  #
98 - 152 - 732
99 - 153 - 8482
9A - 154 - 353
9B - 155 - 8250
9C - 156 - 339
9D - 157 - 65533 #
9E - 158 - 382
9F - 159 - 376
As you can see there are some bytes that have got the same data which breaks my workaround of replacing the wrong byte.
So does anyone have experienced such a thing?
02/20/2012 18:47 Schickl#2
I hope you don't save it in a char variable
in Java char has 2 bytes which would explain the high values
There's no way a single byte could represent a value higher than 255
02/20/2012 19:26 Vinator#3
Quote:
Originally Posted by Schickl View Post
I hope you don't save it in a char variable
in Java char has 2 bytes which would explain the high values
There's no way a single byte could represent a value higher than 255
That's the problem the InputStream of the Sockets is storing the input as 16 bits, storing this in byte would need a cast which will result in a lot of FFs, because as you said these are 2 bytes of data. (If the data is correct there is just one byte, that's how my current workaround is working except for the FFFD bytes.
02/20/2012 19:39 Schickl#4
Quote:
Originally Posted by Vinator View Post
That's the problem the InputStream of the Sockets is storing the input as 16 bits, storing this in byte would need a cast which will result in a lot of FFs, because as you said these are 2 bytes of data. (If the data is correct there is just one byte, that's how my current workaround is working except for the FFFD bytes.
How do you read the data?
I never used sockets in java, but since it's using normal Streams and there are read methods which return a byte array i doubt that it you can get 2 byte values at any time
02/20/2012 19:59 Vinator#5
Quote:
Originally Posted by Schickl View Post
How do you read the data?
I never used sockets in java, but since it's using normal Streams and there are read methods which return a byte array i doubt that it you can get 2 byte values at any time
I need to use chars, because of the workaround with a byte buffer this data gets cut off and results in FF. Usually the sockets return just bytes.
02/20/2012 20:37 Schickl#6
Could you post the function(or at least a part of it) where you read from the stream?
02/20/2012 22:21 Vinator#7
Code:
public void readPacket() {
        char[] packetLength = new char[2];
        String packetString;
        
        try {
            receive.read(packetLength);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if(packetLength[0] == -1) {
            packet = "";
        } else {
            BotConnection.size = Integer.parseInt(ToHex(packetLength[1]) + ToHex(packetLength[0]), 16);
            char[] packetArray = new char[BotConnection.size + 4];
            try {
                receive.read(packetArray);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Couldn't read packet data.");
                e.printStackTrace();
            }
            packetString = getPacketString(packetArray);
            BotConnection.opCode = reverseWord(packetString.substring(0, 4));

            if(packetString.length() > 8) {
                packet = packetString.substring(8, packetString.length());
            } else {
                packet = "0";
            }
        }
}

public String getPacketString(char[] value) {
        StringBuffer strB = new StringBuffer(BotConnection.size * 2 + 4);
        
        for(int i = 0; i < value.length; i++) {
            if(value[i] > 255) {
                switch(value[i]) {
                case 338:
                    strB.append(Integer.toHexString(0x8C).toUpperCase());
                    break;
                case 339:
                    strB.append(Integer.toHexString(0x9C).toUpperCase());
                    break;
                case 352:
                    strB.append(Integer.toHexString(0x8A).toUpperCase());
                    break;
                case 353:
                    strB.append(Integer.toHexString(0x9A).toUpperCase());
                    break;
                case 376:
                    strB.append(Integer.toHexString(0x9F).toUpperCase());
                    break;
                case 381:
                    strB.append(Integer.toHexString(0x8E).toUpperCase());
                    break;
                case 382:
                    strB.append(Integer.toHexString(0x9E).toUpperCase());
                    break;
                case 402:
                    strB.append(Integer.toHexString(0x83).toUpperCase());
                    break;
                case 710:
                    strB.append(Integer.toHexString(0x88).toUpperCase());
                    break;
                case 732:
                    strB.append(Integer.toHexString(0x98).toUpperCase());
                    break;
                case 8211:
                    strB.append(Integer.toHexString(0x96).toUpperCase()); //96, 97
                    break;
                case 8212:
                    strB.append(Integer.toHexString(0x96).toUpperCase());
                    break;
                case 8216:
                    strB.append(Integer.toHexString(0x91).toUpperCase());
                    break;
                case 8217:
                    strB.append(Integer.toHexString(0x92).toUpperCase());
                    break;
                case 8218:
                    strB.append(Integer.toHexString(0x82).toUpperCase());
                    break;
                case 8220:
                    strB.append(Integer.toHexString(0x93).toUpperCase());
                    break;
                case 8221:
                    strB.append(Integer.toHexString(0x94).toUpperCase());
                    break;
                case 8222:
                    strB.append(Integer.toHexString(0x84).toUpperCase());
                    break;
                case 8224:
                    strB.append(Integer.toHexString(0x86).toUpperCase());
                    break;
                case 8225:
                    strB.append(Integer.toHexString(0x87).toUpperCase());
                    break;
                case 8226:
                    strB.append(Integer.toHexString(0x95).toUpperCase());
                    break;
                case 8230:
                    strB.append(Integer.toHexString(0x85).toUpperCase());
                    break;
                case 8240:
                    strB.append(Integer.toHexString(0x89).toUpperCase());
                    break;
                case 8249:
                    strB.append(Integer.toHexString(0x8B).toUpperCase());
                    break;
                case 8250:
                    strB.append(Integer.toHexString(0x9B).toUpperCase());
                    break;
                case 8364:
                    strB.append(Integer.toHexString(0x80).toUpperCase());
                    break;
                case 8482:
                    strB.append(Integer.toHexString(0x99).toUpperCase());
                    break;
                case 65533:
                    strB.append(Integer.toHexString(0x8D).toUpperCase()); //81, 8D, 8F, 90, 9D
                    break;
                default:
                    strB.append(ToHex(value[i]));
                    break;
                }
            } else {
                strB.append(ToHex(value[i]));
            }
        }
        
        return strB.toString();
}
02/20/2012 22:37 Schickl#8
did you ever try passing a byte array instead of an char array?...
I've never seen anyone passing a char array to a read function of a stream lol
02/20/2012 23:23 Vinator#9
Quote:
Originally Posted by Schickl View Post
did you ever try passing a byte array instead of an char array?...
I've never seen anyone passing a char array to a read function of a stream lol
Yes I did and as I said It will cut the 2 bytes which doesn't work for my workaround.

Edit: The InputStreamReader reads bytes but decodes them to a char. That's where I suppose the mistake to be, since it uses a specified charset.

Edit: fixed it, stupid me has never tried a different charset. Anyway thanks Schickl.
02/20/2012 23:36 Schickl#10
InputStreamReader is not the way you should go
DataInputStream should work out very well for you
There are methods for all primitive datatypes, which makes reading them a bit easier
02/20/2012 23:38 Vinator#11
Quote:
Originally Posted by Schickl View Post
InputStreamReader is not the way you should go
DataInputStream should work out very well for you
There are methods for all primitive datatypes, which makes reading them a bit easier
Yes had it before will use it again. Thanks again ^^