Socket_UDP1 |
|
User Datagram Protocol
- Unreliable Datagram Protocol
- Packet Oriented, not stream oriented like TCP/IP
- Much faster but no error correction
- NFS, TFTP, and FSP use UDP/IP
- Must fit data into packets of about 8K or less
//
The UDP Classes
- Java's support for UDP is contained in two classes:
- java.net.DatagramSocket
- java.net.DatagramPacket
- A datagram socket is used to send and receive datagram packets.
java.net.DatagramSocket
- A DatagramSocket object is a local connection to a port that
conducts the sending and receiving.
- DataGramPackets are not mutable
- UDP socket and UDP server socket, are the same
- In contrast to TCP sockets, a DatagramSocket can send to
multiple, different addresses.
- The address to where the data was sent, would be stored in the
packet, and not in the socket.
|
//
UDP ports
- Each computer has 65,536 UDP ports as well as its 65,536 TCP ports.
- A TCP and UDP port can bind to the same port.
Two DatagramPacket Constructors
- public DatagramPacket(byte[] data, int length)
public DatagramPacket(byte[] data, int length, InetAddress iaddr, int
iport)
First is for receiving, second is for sending. For example,
String s = "Some UDP Packet" byte[] b = s.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length);
|
UDP -Destination:
try {
InetAddress intaddr = new InetAddess("intaddr.ABC.com");
int n1 = 40;
String str1 = "Another UDP Packet"
byte[] bt1 = str1.getBytes();
DatagramPacket dp = new DatagramPacket(bt1, bt1.length, intaddr, n1);
}
catch (UnknownHostException e) {
System.err.println(e);
} |
Datagram Methods:
- public synchronized void setAddress(InetAddress iaddr)
- public synchronized void setPort(int iport)
- public synchronized void setData(byte ibuf[])
- public synchronized void setLength(int ilength)
- public synchronized InetAddress getAddress()
- public synchronized int getPort()
- public synchronized byte[] getData()
- public synchronized int getLength()
|
DatagramSocket cleint server volleys.
- client datagram socket , sent prior to receiving any
public DatagramSocket() throws SocketException
- Followed by two server datagaram sockets to specify the port and
IP address of the socket
- public DatagramSocket(int port) throws SocketException
- public DatagramSocket(int port, InetAddress laddr) throws
SocketException
|
|
|