Socket---簡單測試和埠號獲取
阿新 • • 發佈:2019-02-07
SocketAddress addr = new InetSocketAddress("172.20.33.70", 1080); Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); Socket socket = new Socket(proxy); InetSocketAddress dest = new InetSocketAddress("baidu.com", 80); try { socket.connect(dest); } catch (IOException e) { e.printStackTrace(); } /** * 獲得遠端伺服器的IP 地址. */ InetAddress inetAddress = socket.getInetAddress(); Log.d(TAG, "inetAddress = "+inetAddress); /** * 獲得遠端伺服器的埠. */ int port = socket.getPort(); Log.d(TAG, "port = "+port); /** * 獲得客戶本地的IP 地址. */ InetAddress localAddress = socket.getLocalAddress(); Log.d(TAG, "localAddress = "+localAddress); /** * 獲得客戶本地的埠. */ int localPort = socket.getLocalPort(); Log.d(TAG, "localPort = "+localPort); /** * 獲取本地的地址和埠號 */ SocketAddress localSocketAddress = socket.getLocalSocketAddress(); Log.d(TAG, "localSocketAddress = "+localSocketAddress); /** * 獲得遠端的地址和埠號 */ SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress(); Log.d(TAG, "remoteSocketAddress = "+remoteSocketAddress);