Java的InputStream会破坏信息,为什么?

标签 java sockets inputstream

我正在构建的服务器出现问题。我将问题简化为 Java 中的简单服务器-客户端对。我正在发送字节 0 到 255。问题是从 128 (0x80) 到 159 (0x9F) 的值被 InputStream 作为 63 (0x3F) 接收。这正在杀死我的二进制传输。为什么会这样?

这是我得到的输出,看看在值 127 之后有一堆 63:

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

这是我的服务器代码,基于 KnockKnockServer 示例:

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();
        }
}

这是我客户的代码,基于 EchoClient 示例:

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();
        }
}

运行它并告诉我你是否遇到了同样的奇怪问题。请帮我。谢谢!

最佳答案

来自 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/PrintWriter.html 的 PrintWriter 文档

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.

所以您可能为这项工作使用了错误的工具。

关于Java的InputStream会破坏信息,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9448423/

相关文章:

java - Java 中的自动文件/输入流处理

java - 查找到所选点特定距离内的所有地址的最佳方法是什么

java - Lambda 表达式和通用接口(interface)

java - 在不同的目录中使用 Java 的 exec 运行 .sh 文件?

visual-studio-2010 - 安装UnityVS时出现套接字异常

java - 使用输出流发送文件长度并使用输入流接收长度和字节 [] 以将帧从一个设备流式传输到另一个 Android/Java

java.util.MissingFormatArgumentException : Format specifier '%s'

c# - 套接字 OnData 事件

jQuery 套接字 - 'An attempt was made to use an object that is not, or is no longer, usable'

java - 使用 TCP/IP 拦截 http 文件上传