Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- //Name:Muhammad Ashiq Ali bin Abdul Hameed
- //Group:SEP1
- //IpAddress:
- public class Rfc865UdpServer {
- public static void main(String[] argv) {
- //
- // 1. Open UDP socket at well-known port
- //
- String datapacket,dataSentence;
- int port;
- InetAddress IPAddress=null;
- DatagramSocket socket=null;
- byte[] receiveData =new byte[65536];
- byte[] sendData =new byte[65536];
- while (true) {
- try {
- //
- // 2. Listen for UDP request from client
- //
- socket = new DatagramSocket(17);//port 17 is used for qoute of the day protocol
- DatagramPacket requestData = new DatagramPacket(receiveData,receiveData.length);
- socket.receive(requestData);
- dataSentence= new String(requestData.getData());//get the data
- System.out.println("Received: "+ dataSentence);//print out the data
- IPAddress=requestData.getAddress();//get the address
- port = requestData.getPort();//get port number
- //
- // 3. Send UDP reply to client
- //
- // datapacket=sentence.toUpperCase();//caps the received sentence
- datapacket="Random Qoute of the day";
- sendData=datapacket.getBytes();//convert it into bytes
- DatagramPacket reply = new DatagramPacket(sendData,sendData.length, IPAddress,port);
- //Transmit the data back to sender stating that you have received the data
- //Acknowledgement of the received Data
- socket.send(reply);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement