marcoantap
marcoantap

Reputation: 53

Java's InputStream destroys information, why?

I'm having problem with a server I'm building. I reduced the problem to a simple server-client pair in Java. I'm sending bytes 0 to 255. The problem is values from 128 (0x80) to 159 (0x9F) are being received by InputStream as a 63 (0x3F). This is killing my binary transfers. Why could this be happening?

This the output I get, see how after value 127 there are a bunch of 63's:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8
3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 10
7 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 12
7 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
63 63 63 63 63 63 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 17
5 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 19
5 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 21
5 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 23
5 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 25
5

This is my server code, based on the KnockKnockServer example:

import java.net.*;
import java.io.*;

public class Server {
        public static void main(String[] args) throws IOException {

                ServerSocket serverSocket = null;
                try {
                        serverSocket = new ServerSocket(4444);
                } catch (IOException e) {
                        System.err.println("Could not listen on port: 4444.");
                        System.exit(1);
                }

                Socket clientSocket = null;
                try {
                        clientSocket = serverSocket.accept();
                } catch (IOException e) {
                        System.err.println("Accept failed.");
                        System.exit(1);
                }

                OutputStream out = clientSocket.getOutputStream();
                InputStream in = clientSocket.getInputStream();

                while (true)
                {
                    int val = in.read();
                    if (val < 0)
                        continue;

                    System.out.print(val);
                    System.out.print(' ');
                    if (val == 255)
                        break;
                }

                out.close();
                in.close();
                clientSocket.close();
                serverSocket.close();
        }
}

And this is my client's code, based on the EchoClient example:

import java.io.*;
import java.net.*;

public class Client {
        public static void main(String[] args) throws IOException {

                Socket echoSocket = null;
                PrintWriter out = null;
                BufferedReader in = null;

                try {
                        echoSocket = new Socket("localhost", 4444);
                        out = new PrintWriter(echoSocket.getOutputStream(), true);
                        in = new BufferedReader(new InputStreamReader(
                                                                                echoSocket.getInputStream()));
                } catch (UnknownHostException e) {
                        System.err.println("Don't know about host: taranis.");
                        System.exit(1);
                } catch (IOException e) {
                        System.err.println("Couldn't get I/O for "
                                                             + "the connection to: taranis.");
                        System.exit(1);
                }

        for (int i = 0; i < 256; i++)
            out.print((char) i);
        out.flush();

        out.close();
        in.close();
        echoSocket.close();
        }
}

Run this and tell me if you get the same weird problem. Please help me. Thank you!

Upvotes: 1

Views: 412

Answers (4)

Neil
Neil

Reputation: 55382

The reason you are seeing 63s is that your PrintWriter is trying to convert from Unicode to ISO-8859-1 but the characters 128 to 159 are not valid Unicode characters so they get converted to the question mark character.

Upvotes: 1

Sergey Grinev
Sergey Grinev

Reputation: 34488

Do not use char for sending bytes. Java is not C++ so char is not 8bit type. Once you run out of basic ASCII set (127) you start sending Unicode stuff.

Drop PrintWriter and use next loop in your client instead:

    for (int i = 0; i < 256; i++) {
        echoSocket.getOutputStream().write(i);
    }

Upvotes: 1

chubbsondubs
chubbsondubs

Reputation: 38676

You are reading bytes, and sending characters. In Java they aren't the same size. Characters are 2 bytes, and bytes well they are single. If you want to send simply bytes you'll have to use the an OutputStream, and not PrintWriter.

Also remember bytes are signed from -128 to 128 so you can't expect to get numbers above 128 as positive numbers. You'll have to reverse the signed math.

Upvotes: 0

mvds
mvds

Reputation: 47034

From the documentation of PrintWriter at http://docs.oracle.com/javase/1.4.2/docs/api/java/io/PrintWriter.html

Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

So you are probably using the wrong tool for this job.

Upvotes: 4

Related Questions