java.io.StreamCorruptedException:无效的流标头:54657374
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23262160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.StreamCorruptedException: invalid stream header: 54657374
提问by androidGenX
I am trying to read a string which is send from client using Socket program, The code as follows:
我正在尝试使用 Socket 程序读取从客户端发送的字符串,代码如下:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServerExample {
//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 5000;
public static void main(String args[]) throws IOException, ClassNotFoundException{
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for client request");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
//write object to Socket
oos.writeObject("Hi Client "+message);
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the ServerSocket object
server.close();
}
}
But I am getting error as follows while reading the string from client:
但是从客户端读取字符串时出现如下错误:
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 54657374
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:803)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at SocketServerExample.main(SocketServerExample.java:29)
I searched and not managed find the bug. Please help me.
我搜索并没有设法找到错误。请帮我。
采纳答案by user207421
Clearly you aren't sending the data with ObjectOutputStream:you are just writing the bytes.
显然,您不是在发送数据,ObjectOutputStream:而是在写入字节。
- If you read with
readObject()you must write withwriteObject(). - If you read with
readUTF()you must write withwriteUTF(). - If you read with
readXXX()you must write withwriteXXX(),for most values of XXX.
- 如果你阅读,
readObject()你必须写作writeObject(). - 如果你阅读,
readUTF()你必须写作writeUTF(). - 如果您使用 with 阅读,则
readXXX()必须writeXXX(),为 XXX 的大多数值编写 with 。
回答by Peter Lawrey
You can't expect ObjectInputStreamto automagically convert text into objects. The hexadecimal 54657374is "Test"as text. You must be sending it directly as bytes.
您不能指望ObjectInputStream自动将文本转换为对象。十六进制54657374是"Test"文本。您必须直接将其作为字节发送。

