为什么 我的代码会报无法的序列化对象的错误呢?

zrc 发布于 2014/09/01 15:15
阅读 290
收藏 0
package org.JavaSENetProgramYinyong1;


import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;


public class LogInThread extends Thread implements Serializable {
Socket socket = null;


public LogInThread(Socket socket) {
this.socket = socket;
}


@Override
public void run() {
// TODO Auto-generated method stub
try {
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);// 反序列化输入流
User user = (User) ois.readObject();// 解析对象
if (!(user == null)) {
System.out.println("服务端,用户" + user.getUsername() + "\t"
+ user.getPassword());
}
OutputStream os = socket.getOutputStream();
os.write("thanks".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}
package org.JavaSENetProgramYinyong1;


import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;


public class SoketServerYinyong implements Serializable{
public SoketServerYinyong() throws IOException {
ServerSocket ssk = new ServerSocket(8888);
Socket sk = null;
while (true) {
sk = ssk.accept();
LogInThread login = new LogInThread(sk);
login.start();
}


}


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new SoketServerYinyong();
}


}

package org.JavaSENetProgramYinyong1;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;


public class SoketClinetYinyong implements Serializable {// 不实现这个借口是无法完成序列化的对象的
// 使用ObjectOutputStream会报错
public SoketClinetYinyong() throws IOException {
Socket socket = new Socket("localhost", 8888);
User user = new User();
user.setUsername("firbbc1991");
user.setPassword("zrc2370606");
// 向服务器传递信息
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);// 序列化对象
oos.writeObject(user);// 将整个对象写入
// 读取服务端的输出流
socket.shutdownOutput();
InputStream is = socket.getInputStream();
String reply = null;
BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
while ((reply = bfr.readLine()) != null) {
System.out.println(reply);
}
bfr.close();
is.close();
//oos.close();
socket.close();
}


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new SoketClinetYinyong();
}


}


加载中
0
last
last
User对象没有实现 Serializable 呗
0
zrc
zrc
好的,谢谢。
返回顶部
顶部