Register for your free account! | Forgot your password?

You last visited: Today at 10:42

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Java Sockets

Discussion on Java Sockets within the SRO Coding Corner forum part of the Silkroad Online category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
Java Sockets

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?
Vinator is offline  
Old 02/20/2012, 18:47   #2
 
Schickl's Avatar
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,064
Received Thanks: 539
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
Schickl is offline  
Old 02/20/2012, 19:26   #3
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
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.
Vinator is offline  
Old 02/20/2012, 19:39   #4
 
Schickl's Avatar
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,064
Received Thanks: 539
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
Schickl is offline  
Old 02/20/2012, 19:59   #5
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
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.
Vinator is offline  
Old 02/20/2012, 20:37   #6
 
Schickl's Avatar
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,064
Received Thanks: 539
Could you post the function(or at least a part of it) where you read from the stream?
Schickl is offline  
Old 02/20/2012, 22:21   #7
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
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();
}
Vinator is offline  
Old 02/20/2012, 22:37   #8
 
Schickl's Avatar
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,064
Received Thanks: 539
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
Schickl is offline  
Old 02/20/2012, 23:23   #9
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
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.
Vinator is offline  
Old 02/20/2012, 23:36   #10
 
Schickl's Avatar
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,064
Received Thanks: 539
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
Schickl is offline  
Old 02/20/2012, 23:38   #11
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
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 ^^
Vinator is offline  
Reply


Similar Threads Similar Threads
[JAVA Error] Could not create the java virtual machine
07/21/2013 - Technical Support - 10 Replies
Schönen Abend! Leider hat es sich aus einem unerfindlichen Grund ergeben, dass sobald ich die Minecraft.exe starten will die Errormeldung kommt. Die Tips auf Minecraft.net habe ich schon ohne Erfolg befolgt. Hoffe ihr könnt mir weiterhelfen... Mein PC:
Java Sockets and phConnector
01/25/2012 - SRO Coding Corner - 6 Replies
So I'd like to know if anyone tested if the java sockets send the packets correctly to the phConnector, reading the proxy's output seems to work fine, but when I try to send a packet even if it's not valid nothing happens. At least the proxy seems to receive my packets, because when send an oversized packet I'm getting the "increase the max data size..." message.
[Java] Could not create the Java virtual machine
06/22/2011 - Minecraft - 1 Replies
hallo ihr minecraftler ^^ habe seit heute das problem das wenn ich minecraft starte original als auch cracked das diese fehlermeldung kommt: Java virtual machine Launcher Could not create the Java virtual machine



All times are GMT +1. The time now is 10:44.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.