mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add connect
This commit is contained in:
parent
6dc8524e69
commit
25e10f04d1
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.7.8 (2021-08-10)
|
# 5.7.8 (2021-08-11)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 MapProxy支持return this的setter方法(pr#392@Gitee)
|
* 【core 】 MapProxy支持return this的setter方法(pr#392@Gitee)
|
||||||
@ -12,6 +12,7 @@
|
|||||||
* 【json 】 增加JSONBeanParser
|
* 【json 】 增加JSONBeanParser
|
||||||
* 【poi 】 增加CellSetter,可以自定义单元格值写出
|
* 【poi 】 增加CellSetter,可以自定义单元格值写出
|
||||||
* 【poi 】 CsvReader增加readFromStr(pr#1755@Github)
|
* 【poi 】 CsvReader增加readFromStr(pr#1755@Github)
|
||||||
|
* 【socket 】 SocketUtil增加connection方法
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 改进NumberChineseFormatter算法,补充完整单元测试,解决零问题
|
* 【core 】 改进NumberChineseFormatter算法,补充完整单元测试,解决零问题
|
||||||
|
@ -1,24 +1,26 @@
|
|||||||
package cn.hutool.socket;
|
package cn.hutool.socket;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Socket;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.nio.channels.AsynchronousSocketChannel;
|
import java.nio.channels.AsynchronousSocketChannel;
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
|
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Socket相关工具类
|
* Socket相关工具类
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
* @since 4.5.0
|
* @since 4.5.0
|
||||||
*/
|
*/
|
||||||
public class SocketUtil {
|
public class SocketUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取远程端的地址信息,包括host和端口<br>
|
* 获取远程端的地址信息,包括host和端口<br>
|
||||||
* null表示channel为null或者远程主机未连接
|
* null表示channel为null或者远程主机未连接
|
||||||
*
|
*
|
||||||
* @param channel {@link AsynchronousSocketChannel}
|
* @param channel {@link AsynchronousSocketChannel}
|
||||||
* @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接
|
* @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接
|
||||||
*/
|
*/
|
||||||
@ -36,11 +38,61 @@ public class SocketUtil {
|
|||||||
/**
|
/**
|
||||||
* 远程主机是否处于连接状态<br>
|
* 远程主机是否处于连接状态<br>
|
||||||
* 通过判断远程地址获取成功与否判断
|
* 通过判断远程地址获取成功与否判断
|
||||||
*
|
*
|
||||||
* @param channel {@link AsynchronousSocketChannel}
|
* @param channel {@link AsynchronousSocketChannel}
|
||||||
* @return 远程主机是否处于连接状态
|
* @return 远程主机是否处于连接状态
|
||||||
*/
|
*/
|
||||||
public static boolean isConnected(AsynchronousSocketChannel channel) {
|
public static boolean isConnected(AsynchronousSocketChannel channel) {
|
||||||
return null != getRemoteAddress(channel);
|
return null != getRemoteAddress(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建Socket并连接到指定地址的服务器
|
||||||
|
*
|
||||||
|
* @param hostname 地址
|
||||||
|
* @param port 端口
|
||||||
|
* @return {@link Socket}
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
|
* @since 5.7.8
|
||||||
|
*/
|
||||||
|
public static Socket connect(String hostname, int port) throws IORuntimeException {
|
||||||
|
return connect(hostname, port, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建Socket并连接到指定地址的服务器
|
||||||
|
*
|
||||||
|
* @param hostname 地址
|
||||||
|
* @param port 端口
|
||||||
|
* @param connectionTimeout 连接超时
|
||||||
|
* @return {@link Socket}
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
|
* @since 5.7.8
|
||||||
|
*/
|
||||||
|
public static Socket connect(final String hostname, int port, int connectionTimeout) throws IORuntimeException {
|
||||||
|
return connect(new InetSocketAddress(hostname, port), connectionTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建Socket并连接到指定地址的服务器
|
||||||
|
*
|
||||||
|
* @param address 地址
|
||||||
|
* @param connectionTimeout 连接超时
|
||||||
|
* @return {@link Socket}
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
|
* @since 5.7.8
|
||||||
|
*/
|
||||||
|
public static Socket connect(InetSocketAddress address, int connectionTimeout) throws IORuntimeException {
|
||||||
|
final Socket socket = new Socket();
|
||||||
|
try {
|
||||||
|
if (connectionTimeout <= 0) {
|
||||||
|
socket.connect(address);
|
||||||
|
} else {
|
||||||
|
socket.connect(address, connectionTimeout);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IORuntimeException(e);
|
||||||
|
}
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user