2011-10-05 75 views
-1

我目前正在用java實現一個多線程代理服務器,它將接受來自客戶端的消息,並將它們轉發給另一個服務器,然後服務器會確認接收消息。但是,我遇到麻煩了。有人能指出我做錯了什麼嗎?謝謝。在java中實現的多線程代理服務器

ProxyApp:

public class ProxyApp { 

    public static ServerSocket server = null; 
    public static Socket client = null; 


    public static void main(String[] args) 
    { 
     try 
     { 
      server = new ServerSocket(6789); 
      Socket clientsocket = null; 

      while(true) 
      { 
       client = server.accept();  

       if(client.isConnected()) 
       { 
        System.out.println("Proxy is currently listening to client on port 6789"); 
       } 
       Thread t1 = new ProxyHandler(client); 
       t1.start(); 

       clientsocket = new Socket(InetAddress.getLocalHost(), 6780); 
       if(clientsocket.isBound()) 
       { 
        System.out.println("Clientsocket successfully connected on port 6780"); 
       } 

       ProxyHandler t2 = new ProxyHandler(clientsocket); 
       t2.start();                
      } 
     } 
     catch(IOException io) 
     { 
      System.err.println("IOException: " + io.getMessage()); 
     } 
    } 
} 

ProxyHandler

public class ProxyHandler extends Thread { 

    private Socket socket; 
    private String message; 

    public ProxyHandler(Socket socket) 
    { 
     this.socket = socket;   
    } 

    @Override 
    public void run() 
    {  
     message = ""; 
     try 
     { 
      DataInputStream in = new DataInputStream(socket.getInputStream()); 
      DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
      while(true) 
      { 
       message = in.readUTF(); 
       out.writeUTF(message); 

       System.out.println(message); 
      } 
     } 
     catch(IOException io) 
     { 
      System.err.println("IOException: " + io.getMessage()); 
      System.exit(2); 
     } 
    } 
} 

ClientClass:

public class ClientClass { 

    public static void main(String[] args) 
    { 
     try 
     { 
      Socket client = new Socket(InetAddress.getLocalHost(), 6789); 

      if(client.isBound()) 
      { 
       System.out.println("Successfully connected on port 6789"); 
      } 

      Scanner scanner = new Scanner(System.in); 

      DataInputStream inFromProxy = new DataInputStream(client.getInputStream()); 
      DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream()); 

      while(true) 
      { 
       String message; 

       System.out.print("Enter your message: "); 
       message = scanner.next(); 

       outToProxy.writeUTF(message); 
       System.out.println(inFromProxy.readUTF()); 
      } 
     } 
     catch(IOException io) 
     { 
      System.err.println("IOException: " + io.getMessage()); 
      System.exit(2); 
     } 
    } 
} 

ServerClass:

public class ServerClass { 

    public static void main(String[] args) 
    { 
     try 
     { 
      ServerSocket server = new ServerSocket(6780); 

      if(server.isBound()) 
      { 
       System.out.println("Server successfully connected on port 6780"); 
      } 

      Socket client = null; 
      while(true) 
      { 
       client = server.accept(); 

       if(client.isConnected()) 
       { 
        System.out.println("Proxy is connected"); 
       } 

       DataInputStream inFromProxy = new DataInputStream(client.getInputStream()); 
       DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream()); 

       System.out.println(inFromProxy.readUTF()); 

       outToProxy.writeUTF("Message has been acknowledged!"); 
      }  
     } 
     catch(IOException io) 
     { 
      System.err.println("IOException: " + io.getMessage()); 
      System.exit(2); 
     } 
    } 
} 
+3

-1您應該詳細說明「這樣做有問題。」期望SO用戶在沒有任何進一步信息的情況下查看這麼多代碼來尋找「你做錯了什麼」是不合理的。 –

回答

3

在下面的代碼中進出,並在同一臺機器代表消息:

DataInputStream in = new DataInputStream(socket.getInputStream()); 
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 

所以當你在讀取和寫入時,你正在做的正在創建一個回聲服務。

message = in.readUTF(); 
out.writeUTF(message); 

對於代理,您希望從客戶端讀取並寫入服務器,反之亦然。

看來你想要的東西更接近這個:

client = server.accept();  
    clientsocket = new Socket(InetAddress.getLocalHost(), 6780); 
    Thread t1 = new ProxyHandler(client, clientsocket); 
    t1.start(); 
    Thread t2 = new ProxyHandler(clientsocket, client); 
    t2.start(); 

在第一線的工作是從客戶端發送數據到服務器和第二的工作是從服務器發送到客戶端。

+0

它的工作!感謝提示! :) – Moshin