DatagramSocket And DatagramPacket in java


DatagramSocket and DatagramPacket : Class facilitate connectionless networking between different clients. A datagram socket object provides object representation of UDP socket.
A DatagramSocket object can be creted using following constructors :-

        public DatagramSocket(int portno);
Commonly used methods are :-
send() :- Is used to send a UDP Packet.
public void send(DatagraPacket packet);

receive() :- Is used to receive a UDP packet.
public void receive(DatagraPacket packet);

close() :- Is used to close connection.
public void close();

DatagraPacket :- Class provides object representation of UDP packet. A DatagramPacket object can be created using eithere of the following constructors:

public DatagramPacket (byte[] data, int size, InetAddress ipadd, int receiveport);

public DatagramPacket(byte[] data, int size);

Commonly used methods are:-
getData() :- Returns the data of UDP packet as byteArray.

public byte[] getData[];

getPort() :- Returns the port number contains in a packet.

public int getPort();

getHost() :- Returns the IP address of host of a UDP packet.

etc...


Example : Send and Receive Message Using UDP in Java

1 comment:

  1. Personal training is without a doubt seasonal;
    you will have great months and you will have bad months.

    ReplyDelete