/**
* 测试对象流的传输
*/
package com.testClient;
import java.net.*;
import java.io.*;
import com.User.User;
public class TestClient_ObjectStream {
public static void main(String[] args) {
TestClient_ObjectStream tos = new TestClient_ObjectStream();
}
public TestClient_ObjectStream()
{
try {
Socket s = new Socket("127.0.0.1",9999);
//创建对象流向服务器发送客户请求
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
User u = new User();
u.setName("liuguo");
u.setPassword("jiayou");
oos.writeObject(u);
} catch (Exception e) {
e.printStackTrace();
}finally
{
//关闭流
}
}
}
package com.User;
import java.io.*;
public class User implements java.io.Serializable{
private String name;
private String password;
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;
}
}
package com.testServer;
import java.net.*;
import java.io.*;
import com.User.User;
public class TestServer_ObjectStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestServer_ObjectStream to = new TestServer_ObjectStream();
}
public TestServer_ObjectStream()
{
try {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器等待连接。。");
Socket s = ss.accept();
System.out.println("服务器连接成功。。。");
//创建对象流接受客户端发来的信息
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
User u = (User)ois.readObject();
System.out.println("姓名"+u.getName()+"密码"+u.getPassword());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally
{
}
}
}
package com.User;
import java.io.*;
public class User implements java.io.Serializable{
private String name;
private String password;
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;
}
}
|
|