22,899
社区成员




# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2793 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
Trying 133.64.36.249...
telnet: connect to address 133.64.36.249: Connection refused
new DatagramSocket(PORT, InetAddress.getByName("0.0.0.0")); -> bind any ip
DatagramSocket(int port)
javadoc
The socket will be bound to the wildcard address, an IP address chosen by the kernel.
[root@ddd-app btfb]# tcpdump -vnn udp dst port 2793
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
19:54:51.830036 IP (tos 0x0, ttl 56, id 19846, offset 0, flags [none], proto UDP (17), length 29)
1.80.2.130.33598 > 10.173.16.123.2793: UDP, length 1
19:54:54.349588 IP (tos 0x0, ttl 56, id 19848, offset 0, flags [none], proto UDP (17), length 29)
1.80.2.130.33598 > 10.173.16.123.2793: UDP, length 1
19:54:55.513648 IP (tos 0x0, ttl 56, id 19850, offset 0, flags [none], proto UDP (17), length 29)
1.80.2.130.33598 > 10.173.16.123.2793: UDP, length 1
19:54:58.482348 IP (tos 0x0, ttl 56, id 19852, offset 0, flags [none], proto UDP (17), length 33)
1.80.2.130.33598 > 10.173.16.123.2793: UDP, length 5
是不是可以认为是代码写错了?但是我测试的时候代码都没问题。。
Main类
package aaa;
public class Main {
public static void main(String[] args) {
ServerUDP t = new ServerUDP();
if(t.socket != null){
t.start(); //启动线程
}
}
}
ServerUDP类
package aaa;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class ServerUDP extends Thread{
private int i=0;
public DatagramSocket socket = null;
public ServerUDP(){
try {
socket = new DatagramSocket(2793);
} catch (Exception e) {
e.printStackTrace();
System.out.println("服务端初始化scoket失败!");
}
}
/**
* 继承父类方法
*/
public void run(){
byte[] buff = null;
DatagramPacket packet = null;
byte data[] = null;
while(true){
i++;
try {
buff = new byte[4096]; //接收数据的buf数组并指定大小
packet = new DatagramPacket(buff, buff.length);//创建接收数据包,存储在buf中
System.out.println(i+"次");
socket.receive(packet); //接收操作
data = packet.getData(); //接收的数据
InetAddress address = packet.getAddress();// 接收的地址
System.out.println("接收的ip地址:::" + address.toString());
System.out.println("接收的端口::" + packet.getPort());
System.out.println(i+"次,接收的文本:::" + new String(data));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
# netstat -nao
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State Timer
udp 0 0 :::2793 :::* off (0.00/0/0)
但是模拟发送udp包,程序还是收不到。