您的位置 首页 java

Spring底层:从org.springframework.util.socketUtils到Java底层

class SocketUtils: Simple utility methods for working with network sockets — for example, for finding available ports on {@code localhost}. Within this class, a TCP port refers to a port for a {@link Server socket }; whereas, a UDP port refers to a port for a {@link DatagramSocket} 简单的工具方法,用来检查TCP/UDP network sockets的某数字段的可用端口;

public class SocketUtils {

private enum SocketType {

TCP {

@ Override

protected boolean isPortAvailable(int port) {

try {

ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(

port, 1, InetAddress.getByName(” localhost “));

serverSocket. close ();

return true;

}

catch (Exception ex) {

return false;

}

}

},

UDP {

@Override

protected boolean isPortAvailable(int port) {

try {

Datagram Socket socket = new DatagramSocket(port, InetAddress.getByName(“localhost”));

socket.close();

return true;

}

catch (Exception ex) {

return false;

}

}

};

….

}

从以上代码中可用看出,org. Spring framework.util.socketUtils就是对socket的一种简单扩展,与socket有关的两个库 java .net, javax .net,像上面code里有用到java.net.ServerSocket和 javax.net.ServerSocketFactory对象。

对这两个对象一直追踪,发现最后到了这里:

JAVA UDP 底层

JAVA TCP 底层

对,最后的方法都是Native级的, 所以,可以看出,Java.net和javax.net其实也只是对Socket 加壳 封装,真正的Socket应该在C级了

文章来源:智云一二三科技

文章标题:Spring底层:从org.springframework.util.socketUtils到Java底层

文章地址:https://www.zhihuclub.com/173203.shtml

关于作者: 智云科技

热门文章

网站地图