I'm trying to simulate server - client socket connection at Java. I have that code:
private static Socket clientSocket;
private static PrintWriter pw;
private static Socket serverSocket;
private static final int PORT_NUMBER = 1146;
public static void init() throws IOException {
ServerSocket sSocket = new ServerSocket(PORT_NUMBER);
clientSocket = new Socket("localhost", PORT_NUMBER);
serverSocket = sSocket.accept();
OutputStream os = serverSocket.getOutputStream();
pw = new PrintWriter(os, true);
}
private void writePackage(int[] dataPackage) {
for (int packageByte : dataPackage) {
pw.write(packageByte);
}
pw.flush();
try {
InputStream is = clientSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
My data package has hexadecimal values. It is a 127 length array. When I check client input stream I see that only the first 26 elements are the ones that I wrote the output stream of the server. Others are different. ie 26th element is 0xC1
which is 193
in decimal but client input stream accepts is 196
in decimal.
What I miss?
您应该使用OutputStream
而不是PrintWriter
来写入二进制数据。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.