Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.DatagramPacket;
  3. import java.net.DatagramSocket;
  4. import java.net.InetAddress;
  5. import java.net.SocketException;
  6. //Name:Muhammad Ashiq Ali bin Abdul Hameed
  7. //Group:SEP1
  8. //IpAddress:
  9. public class Rfc865UdpServer {
  10. public static void main(String[] argv) {
  11. //
  12. // 1. Open UDP socket at well-known port
  13. //
  14. String datapacket,dataSentence;
  15. int port;
  16. InetAddress IPAddress=null;
  17.  
  18. DatagramSocket socket=null;
  19. byte[] receiveData =new byte[65536];
  20. byte[] sendData =new byte[65536];
  21. while (true) {
  22.  
  23. try {
  24. //
  25. // 2. Listen for UDP request from client
  26. //
  27. socket = new DatagramSocket(17);//port 17 is used for qoute of the day protocol
  28. DatagramPacket requestData = new DatagramPacket(receiveData,receiveData.length);
  29. socket.receive(requestData);
  30. dataSentence= new String(requestData.getData());//get the data
  31. System.out.println("Received: "+ dataSentence);//print out the data
  32. IPAddress=requestData.getAddress();//get the address
  33. port = requestData.getPort();//get port number
  34.  
  35. //
  36. // 3. Send UDP reply to client
  37. //
  38. // datapacket=sentence.toUpperCase();//caps the received sentence
  39. datapacket="Random Qoute of the day";
  40. sendData=datapacket.getBytes();//convert it into bytes
  41. DatagramPacket reply = new DatagramPacket(sendData,sendData.length, IPAddress,port);
  42. //Transmit the data back to sender stating that you have received the data
  43. //Acknowledgement of the received Data
  44. socket.send(reply);
  45. } catch (Exception e) {
  46. System.out.println(e.getMessage());
  47. }
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement