-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPServer.java
41 lines (33 loc) · 1.42 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
/**
* SERVER:
*
* @author Hammad
*/
package cracker;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
while(true)
{
System.out.println("Waiting for packet...");
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData() );
System.out.println("got:" + sentence);
System.out.println("addr: " + receivePacket.getSocketAddress().toString());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}