6.6 网络编程的对象传输

h4 { margin-top: 0.49cm; margin-bottom: 0.51cm; line-height: 156%; page-break-inside: avoid; }h4.western { font-family: "Arial",sans-serif; font-size: 14pt; }h4.cjk { font-family: "黑体","SimHei"; font-size: 14pt; }h4.ctl { font-family: "DejaVu Sans"; font-size: 14pt; }h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; line-height: 173%; page-break-inside: avoid; }h2.western { font-family: "Arial",sans-serif; font-size: 16pt; }h2.cjk { font-family: "黑体","SimHei"; font-size: 16pt; }h2.ctl { font-family: "DejaVu Sans"; font-size: 16pt; }p { margin-bottom: 0.21cm; }a:link { color: rgb(0, 0, 255); }

 

/**

* 让网络编程传递对象

* */

public class Student implements Serializable{

int id ;

String name ;

int age ;

String department ;

public Student( int id, String name, int age, String department) {

super ();

this . id = id;

this . name = name;

this . age = age;

this . department = department;

}

public int getId() {

return id ;

}

public String getName() {

return name ;

}

public int getAge() {

return age ;

}

public String getDepartment() {

return department ;

}

}

服务器

public class ObjectServer {

 

/**

* @param args

*/

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

ServerSocket ss = new ServerSocket(8001);

Socket s = ss.accept();

OutputStream ops = s.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(ops);

Student stu = new Student(19, " 王祥 " ,22, "java" );

oos.writeObject(stu);

oos.close();

s.close();

ss.close();

}

 

}

客服端

 

public class ObjectClient {

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

Socket s = new Socket( "127.0.0.1" ,8001);

InputStream ips = s.getInputStream();

ObjectInputStream ois = new ObjectInputStream(ips);

Student stu = (Student)ois.readObject();

System. out .println( "id is " +stu.getId());

System. out .println( "age is " +stu.getAge());

System. out .println( "name is " +stu.getName());

System. out .println( "deparment is " +stu.getDepartment());

ois.close();

s.close();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e){

e.printStackTrace();

}

}

 

}

 

URL

构造方法摘要

URL ( String  spec)
          
根据 String 表示形式创建 URL 对象。

 

URL ( String  protocol, String  host, int port, String  file)
          
根据指定 protocol( 协议 ) host port 号和 file 创建 URL 对象。

 

URL ( String  protocol, String  host, int port, String  file, URLStreamHandler  handler)
          
根据指定的 protocol host port 号、file handler 创建 URL 对象。

 

URL ( String  protocol, String  host, String  file)
          
根据指定的 protocol 名称、host 名称和 file 名称创建 URL

 

URL ( URL  context, String  spec)
          
通过在指定的上下文中对给定的 spec 进行解析创建 URL

 

URL ( URL  context, String  spec, URLStreamHandler  handler)
          
通过在指定的上下文中用指定的处理程序对给定的 spec 进行解析来创建 URL

 

 

方法

getProtocal , get Host , get Port getFile( 资源名称)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值