62,614
社区成员
发帖
与我相关
我的任务
分享
ServerSocket ss = new ServerSocket(6756);
Socket s = ss.accept();
InputStream ins = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
String str = br.readLine();
System.out.println(str);
ObjectInputStream bis = new ObjectInputStream(ins);
/* 上面的语句报 java.io.StreamCorruptedException: invalid stream header: 73720004 错 */
User user = (User)bis.readObject();System.out.println(user.name);
bis.close();
s.close();
OutputStream outs = s.getOutputStream();
PrintWriter pw = new PrintWriter(outs, true);
pw.println("test");
ObjectOutputStream oos = new ObjectOutputStream(outs);
oos.writeObject(new User("hello"));
oos.close();
s.close();
package com.csdn;
import java.io.Serializable;
public class User implements Serializable
{
String name;
public User(String name)
{
this.name = name;
}
}
protected void readStreamHeader()
throws IOException, StreamCorruptedException
{
short s0 = bin.readShort();
short s1 = bin.readShort();
if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
throw new StreamCorruptedException(
String.format("invalid stream header: %04X%04X", s0, s1));
}
}