Advertisement
khatta_cornetto

example2swnderreceiver.java

Jan 30th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. /**
  5.  * A subclass of DatagramSocket which contains
  6.  * methods for sending and receiving messages
  7.  */
  8. public class MyDatagramSocket extends DatagramSocket {
  9.     static final int MAX_LEN = 10;
  10.  
  11.     MyDatagramSocket(int portNo) throws SocketException {
  12.         super(portNo);
  13.     }
  14.  
  15.     public void sendMessage(InetAddress receiverHost, int receiverPort,
  16.                             String message) throws IOException {
  17.         byte[] sendBuffer = message.getBytes();
  18.         DatagramPacket datagram =
  19.             new DatagramPacket(sendBuffer, sendBuffer.length,
  20.                                receiverHost, receiverPort);
  21.         this.send(datagram);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement