0%

CentOS 环境

安装 udp 监测工具 netcat

1
$ sudo yum install nc

监听端口

1
2
3
4
5
6
$ nc -vul localhost 1080
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Listening on ::1:1080
Ncat: Connection from ::1.
123
1233

发送数据

1
2
3
4
5
$ nc -vu localhost 1080
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ::1:1080.
123
1233

在 Java 程序中,我们可以通过如下代码获取本地的 ip 地址:

1
InetAddress.getLocalHost().getHostAddress()

但由于安装了虚拟机,或者由于本地回环网的问题,获取的 ip 地址可能不是想要的那个。可以采用如下方法获取到所有的 ip 地址列表,再做筛选。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package gy.finolo.ipdemo;

import java.net.*;
import java.util.Enumeration;

public class IPDemo {

public static void main(String[] args) throws UnknownHostException, SocketException {

System.out.println(InetAddress.getLocalHost().getHostAddress());

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();

if (inetAddress instanceof Inet4Address) {

System.out.println(inetAddress.getHostAddress() + " loopback: " + inetAddress.isLoopbackAddress() +
" linklocal: " + inetAddress.isLinkLocalAddress() +
" sitelocal: " + inetAddress.isSiteLocalAddress());
}

if (inetAddress instanceof Inet6Address) {
System.out.println(((Inet6Address) inetAddress).getScopedInterface());
}
}
}
}
}

如果还不能区分,那就可能需要通过网卡名字来过滤了,这个是需要用到 Inet6Address 了。

我们使用 @Autowired 注解来注入依赖,下面通过代码,来简单演示一下其最基础的注入逻辑。

这里我将创建一个新的注解 @MyAutowired

MyAutowired.java

1
2
3
4
5
6
7
8
9
10
package gy.finolo.autowireddemo;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
@Documented
public @interface MyAutowired {
}

一个 Service 实现类,里面没有任何方法,主要用于测试。

UserService.java

1
2
3
4
package gy.finolo.autowireddemo;

public class UserService {
}

一个 Controller 类,我们把上面的 Service 类注入此类中。测试 main 方法也在这里面,方便测试。

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package gy.finolo.autowireddemo;


import java.util.stream.Stream;

public class UserController {

@MyAutowired
private UserService userService;

public static void main(String[] args) {

UserController userController = new UserController();
Class<? extends UserController> clazz = userController.getClass();
Stream.of(clazz.getDeclaredFields()).forEach(field -> {
// check the member variable whether annotated with @MyAutowired
MyAutowired annotation = field.getAnnotation(MyAutowired.class);
if (annotation != null) {
field.setAccessible(true);
// get the class type of the field
Class<?> type = field.getType();
try {
Object o = type.newInstance();
field.set(userController, o);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}
});

System.out.println(userController.userService);
}
}

运行后,可以看到非 null 的输出,证明已经成功把 UserService 依赖注入。