Advertisement
hgr24

Untitled

Mar 29th, 2022
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. public class UDPC
  4. {
  5. public static void main(String[] args)
  6. {
  7. DatagramSocket skt;
  8. try
  9. {
  10. skt=new DatagramSocket();
  11. String msg= "text message ";
  12. byte[] b = msg.getBytes();
  13. InetAddress host=InetAddress.getByName("127.0.0.1");
  14. int serverSocket=6788;
  15. DatagramPacket request =new DatagramPacket (b,b.length,host,serverSocket);
  16. skt.send(request);
  17. byte[] buffer =new byte[1000];
  18. DatagramPacket reply= new DatagramPacket(buffer,buffer.length);
  19. skt.receive(reply);
  20. System.out.println("client received:" +new String(reply.getData()));
  21. skt.close();
  22. }
  23. catch(Exception ex)
  24. {
  25. }
  26. }
  27. }
  28.  
  29.  
  30.  
  31.  
  32.  
  33. import java.io.*;
  34. import java.net.*;
  35. public class UDPS
  36. {
  37. public static void main(String[] args)
  38. {
  39. DatagramSocket skt=null;
  40. try
  41. {
  42. skt=new DatagramSocket(6788);
  43. byte[] buffer = new byte[1000];
  44. while(true)
  45. {
  46. DatagramPacket request = new DatagramPacket(buffer,buffer.length);
  47. skt.receive(request);
  48. String[] message = (new String(request.getData())).split(" ");
  49. byte[] sendMsg= (message[1]+ " server processed").getBytes();
  50. DatagramPacket reply = new
  51. DatagramPacket(sendMsg,sendMsg.length,request.getAddress
  52. (),request.getPort());
  53. skt.send(reply);
  54. }
  55. }
  56. catch(Exception ex)
  57. {
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement