Guest User

Untitled

a guest
Jun 13th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. //PROGRAM FOR SERVER SIDE
  2. /**
  3. * @Program that Receives a datagram packet from this socket.
  4. * Receive1.java
  5. * Author:-RoseIndia Team
  6. * Date:-20-jun-2008
  7. */
  8. import java.io.*;
  9.  
  10. import java.net.*;
  11. import java.util.*;
  12.  
  13.  
  14. public class Receive1 {
  15.  
  16. public static void main(String args[])throws Exception {
  17. DatagramSocket socket = null;
  18.  
  19. socket = new DatagramSocket(1313);
  20. for (int i = 0; i < 10; i++) {
  21. byte[] buf = new byte[256];
  22. DatagramPacket packet = new DatagramPacket(buf, buf.length);
  23.  
  24. socket.receive(packet);
  25.  
  26. buf = new Date().toString().getBytes();
  27. InetAddress address = packet.getAddress();
  28. int port = packet.getPort();
  29. packet = new DatagramPacket(buf, buf.length, address, port);
  30. socket.send(packet);
  31. }
  32. socket.close();
  33.  
  34. }
  35. }
  36. /*
  37.  
  38. //PROGRAM FOR CLIENT SIDE
  39. /**
  40. * @Program that Sends a datagram packet from this socket.
  41. * send1.java
  42. * Author:-RoseIndia Team
  43. * Date:-20-jun-2008
  44. */
  45. /*
  46. import java.io.*;
  47. import java.net.*;
  48.  
  49. public class send1 {
  50.  
  51. public static void main(String[] args) throws Exception {
  52.  
  53. DatagramSocket socket = new DatagramSocket();
  54. for (int i = 0; i < 10; i++) {
  55. byte[] buf = new byte[256];
  56. InetAddress address = InetAddress.getByName("localhost");
  57.  
  58. DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 1313);
  59.  
  60. socket.send(packet);
  61.  
  62. packet = new DatagramPacket(buf, buf.length);
  63. socket.receive(packet);
  64. String received = new String(packet.getData());
  65. System.out.println("Current server time: " + received);
  66. Thread.sleep(1500);
  67. }
  68.  
  69. socket.close();
  70.  
  71.  
  72.  
  73. }
  74. }
  75.  
  76. */
Add Comment
Please, Sign In to add comment