[Android]socket使用ip和域名建立通信

局域网通过ip连接socket

	Socket socketClient= new Socket();
	SocketAddress localAddr = new InetSocketAddress("192.168.1.234", 9070);
	socketClient.connect(localAddr);

广域网通过域名连接socket

    try {
        Socket socketClient= new Socket();
        InetAddress netAddress = InetAddress.getByName("baidu");
        InetSocketAddress socketAddress = new InetSocketAddress(netAddress, 80);
        socketClient.connect(socketAddress);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

你可能感兴趣的:(Android,java)