| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 
 | package ab;
import java.io.*;
import java.net.*;
import java.util.*;
public class server {
    private static Map<Integer,String> prot = new HashMap<Integer,String>();
    private static Map<Integer,ServerSocket> sock = new HashMap<Integer,ServerSocket>();
    
    public static void main(String[] args) {        
        // Listening Ports
        prot.put(21,"ftp");
            
        // Listening Sockets
        Iterator i = prot.entrySet().iterator();
        Map.Entry m;
        Integer k;
        while (i.hasNext()) {
            m = (Map.Entry) i.next();
            k = (Integer) m.getKey();            
            
            try {sock.put(k,new ServerSocket(k));} 
            catch (IOException e) {}
        }  
        
        while (true) iterate();
        
        // ...
    }
    
    public static void iterate() {        
        Iterator i = sock.entrySet().iterator();
        Map.Entry m;
        ServerSocket v;
        while (i.hasNext()) {
            m = (Map.Entry) i.next();
            v = (ServerSocket) m.getValue();
            
            try {new client(v.accept());} 
            catch (IOException e) {}            
        }
    }
}
class client extends Thread {
    private Socket sock,data;    
    private PrintWriter sos,dos;
    private BufferedReader sis,dis;
    
    private String psd;
    private String pwd;
    private String dir = "/";
    private String tt;
    client(Socket sock) {
        this.sock = sock;        
        try {
            this.sos = new PrintWriter(this.sock.getOutputStream());
            this.sis = new BufferedReader(new InputStreamReader(this.sock.getInputStream()));
        } catch (IOException e) {}
               
        this.send("220 about:blank [alphaTest]...");
        this.start();
    }
    
    public void run() {
        String data = null;
        try {
            while ((data = this.sis.readLine()) != null) {
                System.out.println("client@" + this.sock.getInetAddress().toString() + ":" + this.sock.getPort() + ": " + data);
                this.iterate(data);
            }
        } catch (IOException e) {}
    }
  
    protected void finalize() {
        try {
            this.sos.close();
            this.sis.close();
            this.sock.close();
        } catch (IOException e) {}
    }
    
    private void send(String msg) {
        System.out.println("server@" + this.sock.getInetAddress().toString() + ":" + this.sock.getPort() + ": "+msg);
        
        this.sos.println(msg);
        this.sos.flush();
    }
    
    private void iterate(String data) {        
        String cmd,prm,msg;
        
        if (data.length() > 3) {
            cmd = data.substring(0,4).trim();
            prm = data.substring(4).trim();
        } else {
            cmd = data.trim();
            prm = "";
        }
        
        if (cmd.equals("USER")) {
            this.psd = prm;
            msg = "331 Password required for " + this.psd;
        } else if (cmd.equals("PASS")) {
            this.pwd = prm;
            
            // ...
            
            msg = "230 User " + this.psd + " logged in from " + this.data.getInetAddress().toString();
        } else if (cmd.equals("SYST")) {
            msg = "215 UNIX Type: L8";
        } else if (cmd.equals("HELP")) {
            msg = "214-about:blank [alphaTest]\n"+
                    "214-Commands available:\n"+
                    "214-USER\n"+
                    "214-PASS\n"+
                    "214-SYST\n"+
                    "214-HELP\n"+
                    "214-QUIT\n"+
                    "214-PWD\n"+
                    "214 HELP command successful";
        } else if (cmd.equals("TYPE")) {
            if (prm.equals("A") || prm.equals("I")) {
                this.tt = prm;
                msg = "200 type set";
            } else {
                msg = "501 Syntax error in parameters or arguments";
            }
        } else if (cmd.equals("PASV")) {
            for (int i=0;i<3;i++) {                
                try {
                    this.data  = new Socket(this.data.getInetAddress(),(int) (Math.random() * (65536-1024)) + 1024);   
                    this.dos = new PrintWriter(this.data.getOutputStream());
                    this.dis = new BufferedReader(new InputStreamReader(this.data.getInputStream()));
                } catch (IOException e) {}  
                
                if (this.data.isBound()) break;
            }
            
            msg = this.data.isBound() ? 
                "227 Entering Passive Mode (" + this.data.getLocalAddress().toString().replace(".",",") + "," + (this.data.getPort() >> 8) + "," + (this.data.getPort() & 0xFF) + ")": 
                "452 Can't open data connection";       
        } else if (cmd.equals("LSIT")) {
            if (!this.data.isBound()) {
                msg = "425 Can't open data connection";
            } else {
                this.send("150 Opening  " + this.tt() + " data connection");
                
                // ...
                
                msg = "226 Transfer complete";
            }
        } else if (cmd.equals("QUIT")) {
            msg = "221 Disconnected from about:blank [alphaTest] FTP Server";  
            // ...
        } else if (cmd.equals("PWD")) {
            msg = "257 \"" + this.dir + "\" is current directory";
        } else if (cmd.equals("CWD")) {
            this.dir += "/" + prm;
            
            msg = "450 Requested file action not taken";
            msg = "250 CWD command succesful";	
        } else {            
            msg = "502 Command not implemented";
        }
        
        this.send(msg);
    }
    
    private String tt() {return this.tt.equals("A") ? "ASCII mode" : "Binary mode";}
    private String eol() {return this.tt.equals("A") ? "\r\n" : "\n";}
} | 
Partager