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 protocol, String host, int port, String file) |
|
URL ( String protocol, String host, int port, String file, URLStreamHandler handler) |
|
URL ( String protocol, String host, String file) |
|
URL ( URL context, String spec) |
|
URL ( URL context, String spec, URLStreamHandler handler) |
|
方法
getProtocal , get Host , get Port getFile( 资源名称)