网络编程
服务(Service), 如: 食堂, 商店 Host
WEB
Client -> Service
1 TCP 编程
食堂买饭 服务器
A 食堂有地址 A IP地址(域名)
B 每个食堂,有提供不同服务类型窗口 B 不同的端口(ServerSocket) 65535,
C 每个窗口可以为多个同学服务 C 每个端口可以建立多个Socket实例,
每个实例代表一个对客户的服务
学生买饭的过程 客户端连接
A 找到一个食堂 A 使用域名(IP)找到指定的服务器
B 找到一个窗口 B 使用服务器的端口号找到服务端口
C 请求指定服务 C 建立Socket 对象.
D 有窗体提供对应的反馈 D 使用流与服务器通信.
网络编程实例:
简单的 FTP 协议实现(?, ls, get)
A 服务器请求是文本行请求
B 服务器响应包含两个部分: 第一行是服务器响应头, 头有两种:
1 text,num 是文本响应结果, num是响应中包含的文本行数.
2 file,size,filename 是文件数据响应结果, size是响应流中文件的长度.
反射 (自省) 是自我管理的机制
java 代码管理Java的类和方法等
名词->类
名词: 类, 方法, 属性, 构造器
Class: Class, Method, Field, Constructor
任何类都是Class的具体实例.
类加载到内存中的结果是Class的实例, 是一个对象.
类可以作为对象管理.
Student s = new Student();
Class cls = Student.class;
Student.class
1 Class实例的获得方式
A Java类只按需加载一次
B 3 种方法: 1 类的.class属性
2 对象的getClass() 方法
3 Class类的静态寻找方法: Class.forName();
2 Class实例的用途
反射应用:
简单的测试框架实现
package day21;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class FtpClient {
Socket socket;
OutputStream out;
InputStream in;
public static void main(String[] args)
throws Exception {
FtpClient client = new FtpClient();
client.open("localhost", 8800);
}
public void open(String host, int port)
throws Exception {
socket = new Socket(host, port);
in = socket.getInputStream();
out = socket.getOutputStream();
new RequestSender(out).start();
new ResponseReceiver(in).start();
}
class RequestSender extends Thread{
OutputStream out;
public RequestSender(OutputStream out) {
this.out = out;
}
public void run() {
PrintWriter out = new PrintWriter(this.out, true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(System.in));
String str;
try{
while((str = in.readLine())!=null){
out.println(str);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
class ResponseReceiver extends Thread{
InputStream in;
public ResponseReceiver(InputStream in) {
this.in = in;
}
public void run() {
BufferedReader in =
new BufferedReader(
new InputStreamReader(this.in));
try {
String str;
while((str = in.readLine())!=null){
if(str.startsWith("text")){
String num = str.substring(str.indexOf(",")+1);
printText(in, Integer.parseInt(num));
}else if(str.startsWith("file")){// file,4567,filename
saveFile(this.in, str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveFile(InputStream in,
String head) throws IOException {
String[] data = head.split(",");
int length = Integer.parseInt(data[1]);
String name = data[2];
OutputStream out =
new BufferedOutputStream(
new FileOutputStream("ftp-"+name));
for(int i=0; i<length; i++){
int b = in.read();
out.write(b);
}
out.close();
System.out.println("下载了文件:" +name);
}
private void printText(
BufferedReader in, int num)
throws IOException {
for(int i=0; i<num; i++){
System.out.println(in.readLine());
}
}
}
}
————————————————————————————
package day21;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class FtpServer {
public static void main(String[] args)
throws Exception{
FtpServer server = new FtpServer();
server.listen(8800);
}
public void listen(int port) throws Exception{
ServerSocket ss = new ServerSocket(port);
while(true){
System.out.println("等待客户端连接...");
Socket socket = ss.accept();
System.out.println("客户连接进来了.");
new ClientAgent(socket).start();
}
}
class ClientAgent extends Thread{
Socket socket;
InputStream in;
OutputStream out;
public ClientAgent(Socket socket)
throws IOException{
this.socket = socket;
in = socket.getInputStream();
out = socket.getOutputStream();
}
@Override
public void run() {
BufferedReader in =
new BufferedReader(
new InputStreamReader(this.in));
PrintWriter out =
new PrintWriter(this.out, true);
try{
out.println("text,1");
out.println("你好欢迎使用FTP Demo!");
while(true){
String cmd = in.readLine();
if("?".equals(cmd)){
out.println("text,1");
out.println("支持命令: ls, get, ?, bye");
}else if("ls".equals(cmd)){
listDir(out);
}else if(cmd.matches("^get\\s+.+")){
sendFile(cmd, out, this.out);
}else{
out.println("text,1");
out.println("不知可否!");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
private void sendFile (
String cmd, PrintWriter out,
OutputStream os) throws IOException {
String name = cmd.split("\\s+")[1];
File file = new File(name);
if(! file.exists()){
out.println("text,1");
out.println("没有找到文件!" + name);
return;
}
out.println("file,"+file.length()+","+name);
InputStream in = new BufferedInputStream(
new FileInputStream(file));
int b;
while((b=in.read())!=-1){
os.write(b);
}
os.flush();
in.close();
}
private void listDir(PrintWriter out) {
File dir = new File(".");
File[] files =
dir.listFiles(new FileFilter(){
public boolean accept(File pathname) {
return pathname.isFile();
}
});
out.println("text,"+(files.length+1));
out.println("在目录:"+dir+"中, 有文件:"+files.length);
for (File file : files) {
out.println(file.getName());
}
}
}
}