————网络编程案例(1)——————————
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());
}
}
}
}
—————————(2)—————————————
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());
}
}
}
}