求救,关于对象流 读写的问题.
服务器端为什么接收不到客户端发送过去的数据?
找了很久也没有找到是什么原因.
所以贴出来让大家帮忙解决,谢谢了.
服务端:
import
java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
public class Server extends java.lang.Thread{
ServerSocket ss = null;
ObjectInputStream ois = null;
public Server() {
try {
ss = new ServerSocket(1860);
System.out.println( "服务器已启动...! ");
} catch (
IOException e) {
e.printStackTrace();
}
}
public void run() {
String type = null;
while (true) {
try {
Socket client = ss.accept();
ois = new ObjectInputStream(client.getInputStream());
try {
System.out.println(ois.readObject());
} catch (
ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
new Server().start();
}
}
客户端:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.
UnknownHostException;
public class Client{
public static void main(String[] args) {
try {
Socket s = new Socket( "127.0.0.1 ",1860);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
UserInfo uinfo = new UserInfo();
uinfo.setName( "lxh ");
uinfo.setPassword( "asdf ");
oos.writeObject(uinfo);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class UserInfo implements java.io.Serializable{
private String name = null;
private String password = null;
public UserInfo(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private void writeObject(ObjectOutputStream oos){
try {
oos.writeUTF(name);
oos.writeUTF(password);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void readObject(ObjectInputStream ois) {
try {
name = ois.readUTF();