Socket是否也接受端口?

时间:2015-08-02 06:44:21

标签: java sockets

boolean canConnect = true;
try {
    SocketAddress addr; // initialization not shown
    Socket s = new Socket();
    s.connect(addr);
    s.close();
} catch (IOException e) {
    canConnect = false;
    e.printStackTrace();
}

我有一个全局字符串变量:

string ipaddress = "10.0.0.3:8080";

如何将此ipaddress指定给SocketAddress地址?

1 个答案:

答案 0 :(得分:2)

最基本的方式:

Socket s = new Socket(host, port);

请参阅documentation

或者,使用连接InetSocketAddress

 s.connect( new InetSocketAddress(host, port) );