2012-12-14 85 views
0

我使用套接字在java中創建了多個客戶端 - 服務器通信。我有一臺服務器和一臺客戶機。我想用多個客戶測試我的程序。我打算創建一個動態創建IP和端口的模擬器..爲此,我只想知道如何在套接字程序中設置客戶端IP和端口。任何人都可以幫助我..我使用InetAddress.getByName獲取客戶端地址。如何在java中使用客戶端套接字程序知道客戶端端口和IP地址

public Socket(Proxy proxy) 
    { 
    if (proxy != null && proxy.type() == Proxy.Type.SOCKS) 
    { 
    SecurityManager security = System.getSecurityManager(); 
     InetSocketAddress epoint = (InetSocketAddress) proxy.address(); 
     if (security != null) { 
      if (epoint.isUnresolved()) 
       epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort()); 
      if (epoint.isUnresolved()) 
       security.checkConnect(epoint.getHostName(), 
             epoint.getPort()); 
      else 
       security.checkConnect(epoint.getAddress().getHostAddress(), 
             epoint.getPort()); 
     } 
     impl = new SocksSocketImpl(proxy); 
     impl.setSocket(this); 
    } else { 
     if (proxy == Proxy.NO_PROXY) { 
      if (factory == null) { 
       impl = new PlainSocketImpl(); 
       impl.setSocket(this); 
      } else 
       setImpl(); 
     } else 
      throw new IllegalArgumentException("Invalid Proxy"); 
    } 
} 
+0

此代碼似乎是從java.net.Socket中。與你的問題的聯繫逃脫了我。你的問題本身就能逃脫我。 – EJP

回答

1

嘗試此

clientString = "Remote client: " + socket.getRemoteSocketAddress().toString().substring(1); 
0

簡單。要創建客戶端IP和端口,您應該在主程序中傳遞下面的參數。

只是嘗試這樣

  java Client localhost 1112 //To run 

      (ip: localhost 
      port: 1112) 

      // Code for main 

       public static void main(String[] args) 

      { 

      try 

      { 

        ipAdd=args[0]; 

        portNo=Integer.parseInt(args[1]); 

        Client s=new Client(); 

      } 

      catch (Exception e) 

       { 

         System.out.println(e); 
       } 

}