-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPServer.java
26 lines (19 loc) · 975 Bytes
/
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
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main (String[] args) throws IOException{
DatagramSocket serverSocket = new DatagramSocket(Integer.parseInt("5001"));
System.out.println("Server Started. Listening for Clients on port 5001" + "...");
byte[] receiveData=new byte[1024]; //create a data array to store the received message
DatagramPacket receivePacket;
while(true)
{
receivePacket =new DatagramPacket(receiveData,receiveData.length);// create a packet to store the incoming message properties
serverSocket.receive(receivePacket);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String msg= new String(receivePacket.getData());
System.out.println("[Client || IP: " + IPAddress + " ,Port: " + port +"] " + msg);
}
}
}