-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-UDPS.java
More file actions
29 lines (23 loc) · 997 Bytes
/
5-UDPS.java
File metadata and controls
29 lines (23 loc) · 997 Bytes
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
// Write a program on datagram socket for client/server to display the messages on client side, typed at the server side.
import java.util.*;
import java.net.*;
public class UDPS{
public static void main(String[] args) throws Exception{
DatagramSocket serverSocket=new DatagramSocket(9876);
System.out.println("Server is ready for connection");
byte[] receiveData=new byte[1024];
byte[] sendData=new byte[1024];
while(true){
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
serverSocket.receive(receivePacket);
String sentence=new String(receivePacket.getData());
System.out.println("RECEIVED: "+sentence);
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);
}
}
}