-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPServer.java
38 lines (34 loc) · 1.25 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.georgiasouthern.csci5332;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
/**
*
* @author hzhang
*/
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(5332);
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while (true) {
socket.receive(packet);
InetAddress ip = packet.getAddress();
int port = packet.getPort();
System.out.println("ip addr: " + ip);
System.out.println("port: " + port);
String msg = new String(buf, StandardCharsets.UTF_8);
System.out.println("msg: " + msg);
byte[] sbuf = ("Server: " + msg).getBytes();
DatagramPacket spacket = new DatagramPacket(sbuf, sbuf.length, ip, port);
socket.send(spacket);
}
}
}