帮忙写个socket客户端代码,要求能连接到Socks5代理服务器,服务器有用户名密码验证。

解决方案 »

  1.   

    先写一个空白文档import java.util.*;
    import java.io.*;
    class Password implements java.io.Serializable{
    String name;
    String password;
    public Password(){name = ""; password = "";}
    public Password(String n, String p){
    name = n;
    password = p;
    }
    public boolean equals(Object p){
    Password pw = (Password)p;
    return name.equals(pw.name) && password.equals(pw.password);
    }
    public String toString(){
    String s = name +" "+password;
    return s;
    }
    } import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.concurrent.*;
    class PasswordServer{
       final static int portNum = 1234; // any number > 1024
       final static int numThreads = 10;
       static ExecutorService pool;
       public static void main(String[] args){
         ArrayList<Password> passwords = new ArrayList<Password>(); 
         loadPasswords(passwords);
         pool = Executors.newFixedThreadPool(numThreads);
         System.out.print("Server running ...");             
         try{   
           ServerSocket servesock = new ServerSocket(portNum);
           // for service requests on port portSqrt
           while (true){  
              // wait for a service request on port portSqrt
              Socket socket = servesock.accept(); 
              // submit request to pool
              pool.submit(new CheckPassword(socket,passwords));
          }
         }catch(IOException e){}
       }
            static void loadPasswords(ArrayList<Password> p){
        String s;
        StringTokenizer t;
        try{
        FileReader fr = new FileReader("Password.txt");
        BufferedReader in = new BufferedReader(fr);
        s = in.readLine();
        while(s != null){
        t = new StringTokenizer(s);
        Password pWord = new Password(t.nextToken(),t.nextToken());
        p.add(pWord);
        s = in.readLine();
        }
        in.close();
        }catch(IOException e){
        System.out.println(e.getMessage());
        }
       }       
    }
    class CheckPassword implements Runnable{
    Socket socket;
    ArrayList<Password> pWords;
    public CheckPassword(Socket s, ArrayList<Password> p){
    socket = s;
    pWords = p;
    }
    public void run() {
                try { 
                    ObjectInputStream in = 
                        new ObjectInputStream(socket.getInputStream());
                    DataOutputStream out =
                        new DataOutputStream(socket.getOutputStream());
                    Password pw = (Password)in.readObject();
                    if(pWords.contains(pw)) // password found
                        out.writeBoolean(true); 
                    else
                        out.writeBoolean(false);
                    socket.close(); // close connection
                } 
                catch (IOException e) {}
                catch(ClassNotFoundException e1){}
                
        }
    }
    import java.io.*;
    import java.net.*;
    import java.util.*;
    class LoginPassword{
    private final static int port = 1234; 
    public static void main(String args[]){
          try{
           Socket socket;
            socket = new Socket(InetAddress.getLocalHost(),port);   
       DataInputStream in = new DataInputStream(socket.getInputStream());
            ObjectOutputStream out = new
     ObjectOutputStream(socket.getOutputStream());
            // get user name and password
            Scanner keyIn = new Scanner(System.in);
            System.out.print("User name: ");
            String user = keyIn.next();
            System.out.print("Password:  ");
            String pw = keyIn.next();
            Password pWord = new Password(user,pw);
              
            //write object and flush any buffered data
            out.writeObject(pWord);
            out.flush();
              
            // wait for reply from server 
            boolean valid = in.readBoolean();  
            if(valid)
                 System.out.println("Registered user");
            else
                 System.out.println("Not registered");
            socket.close();
            in.close();
            out.close();    
         }catch(IOException e){System.out.println(e);}
       }
      

  2.   

    参考:
    http://school.itzcn.com/special-spid-50.html
    上面讲解的比较详细,希望对楼主有所帮助。
      

  3.   

    我说的是   SOCKET5 , 是个代理服务器。
      

  4.   


    错了,是 Socks5 !
      

  5.   


    这里应该有密码验证的API。