-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUdpServer.java
68 lines (48 loc) · 1.96 KB
/
UdpServer.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package socket.demo.UDPTEST;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpServer {
public static final int LOCAL_PORT = 39000;
public static final int CLIENT_PORT = 39000;
public static final String CLIENT_IP = "192.168.1.63";
public static final int BUFFER_SIZE = 1024;
public static void main(String[] args) {
DatagramSocket serverSocket= null;
DatagramSocket forwardSocket = null;
try {
serverSocket = new DatagramSocket(LOCAL_PORT);
forwardSocket = new DatagramSocket();
DatagramPacket forwardPacket;
byte[] recvBuf = new byte[BUFFER_SIZE];
DatagramPacket recvPacket
= new DatagramPacket(recvBuf , recvBuf.length);
while (true) {
serverSocket.receive(recvPacket);
//String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());
int port = recvPacket.getPort();
InetAddress addr = recvPacket.getAddress();
forwardPacket = new DatagramPacket(recvPacket.getData(), recvPacket.getLength(),
InetAddress.getByName(CLIENT_IP), CLIENT_PORT);
forwardSocket.send(forwardPacket);
StringBuilder sb = new StringBuilder();
sb.append("len =" + recvPacket.getLength()).append("\n");
sb.append("from: " + addr.getHostAddress());
System.out.println(sb.toString());
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
if (forwardSocket != null) {
forwardSocket.close();
}
}
}
}