Java Socket通信中传递Object对象时注意的问题
2012-03-13 09:44阅读:
1:对象实现Serializable接口时,保证serialVersionUID一致:
private static final long serialVersionUID =
-5240152692961888097L;
2:客户端和服务端传递的对象所在包的路径必须一致(这一点很容易出错)
代码如下:红色部分是注意的地方
Server:
package servertest;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import Student.Student;
public class Main {
private static final int PORT = 7777;
private Socket socket;
private ServerSocket serverSocket;
public Main() throws IOException {
serverSocket = new
ServerSocket(PORT);
while (true) {
socket =
serverSocket.accept();
new
DoService(socket);
}
}
class DoService extends Thread {
private Socket socket;
public DoService(Socket socket)
{
this.socket =
socket;
start();
}
public void run() {
try {
//
读取客户端发送来的student对象
ObjectInputStream ois = new
ObjectInputStream(socket.getInputStream());
Student student = (Student) ois.readObject();
String vReceptMsg = 'you send student - id: ' + student.getId() + '
name: ' + student.getName();
System.out.println(vReceptMsg);
//
向客户端发送新的对象
int
vNewId = student.getId() + 1;
String vNewName = '李四' + vNewId;
student.setId(vNewId);
student.setName(vNewName);
ObjectOutputStream oos = new
ObjectOutputStream(socket.getOutputStream());
oos.writeObject(student);
oos.flush();
} catch
(IOException e) {
e.printStackTrace();
} catch
(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
new Main();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
package clienttest;
import Student.Student;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Main {
private static final String IP = '127.0.0.1';
private static final int PORT = 7777;
public Main() throws UnknownHostException,
IOException, ClassNotFoundException {
Socket client = new Socket(IP,
PORT);
// 向服务器端发送学生对象
ObjectOutputStream oos = new
ObjectOutputStream(client.getOutputStream());
Student student = new
Student();
student.setId(123);
student.setName('tom');
oos.writeObject(student);
oos.flush();
// 服务器端返回的新学生数据
ObjectInputStream ois = new
ObjectInputStream(client.getInputStream());
Student newStudent = (Student)
ois.readObject();
String vReceptMsg = 'you get
student - id: ' + newStudent.getId() + ' name: ' +
newStudent.getName();
System.out.println(vReceptMsg);
}
public static void main(String[] args) {
try {
new Main();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Student类:
package Student;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID =
-5240152692961888097L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}