欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Socket 连接超时

程序员文章站 2022-07-13 10:55:18
...
Java Socket 连接超时设置
String ip = ip;
Integer port = port;
final Integer TIMEOUT = 1000;// ms
        
InetAddress theAddress = InetAddress.getByName(ip);
        
InetSocketAddress inetSocketAddress = new InetSocketAddress(theAddress, port);
//        socket = new Socket(theAddress, port);
socket = new Socket();
socket.connect(inetSocketAddress, TIMEOUT);