Presentation is loading. Please wait.

Presentation is loading. Please wait.

NETWORK PROGRAMMING CNET 441

Similar presentations


Presentation on theme: "NETWORK PROGRAMMING CNET 441"— Presentation transcript:

1 NETWORK PROGRAMMING CNET 441
CHAPTER - 3 UDP Sockets

2 Chapter 3: Objectives After Completing the Chapter 3, the student can understand the following concepts. DatagramPacket Class Constructors for Receiving Datagram Constructors for Sending Datagram Get Methods Setter Methods DatagramSocket Class Sending and Receiving Datagrams UDP Server Creation Steps UDP Client Creation Steps

3 DatagramPacket and DatagramSocket Class
1. Java’s implementation of UDP is split into two classes: DatagramPacket and DatagramSocket. 2. The DatagramPacket class stuffs bytes of data into UDP packets called datagrams and lets you unstuff datagrams that you receive. 3. A DatagramSocket sends as well as receives UDP datagrams. 4. To send data, you put the data in a DatagramPacket and send the packet using a DatagramSocket. 5. To receive data, you take a DatagramPacket object from a DatagramSocket and then inspect the contents of the packet. 6. In UDP, everything about a datagram, including the address to which it is directed, is included in the packet itself; the socket only needs to know the local port on which to listen or send.

4 Structure of UDP Datagram

5 Constructors for receiving datagrams:
DatagramPacket Class In Java, a UDP datagram is represented by an instance of the DatagramPacket class: public final class DatagramPacket extends Object Constructors for receiving datagrams: 1. public DatagramPacket(byte[] buffer, int length) 2. public DatagramPacket(byte[] buffer, int offset, int length) If the first constructor is used, when a socket receives a datagram, it stores the datagram’s data part in buffer beginning at buffer[0] and continuing until the packet is completely stored or until length bytes have been written into the buffer. If the second constructor is used, storage begins at buffer[offset] instead. Otherwise, these two constructors are identical. length must be less than or equal to buffer.length - offset.

6 Constructors for sending datagrams
public DatagramPacket(byte[] data, int length, InetAddress destination, int port) public DatagramPacket(byte[] data, int offset, int length, InetAddress destination, int port) public DatagramPacket(byte[] data, int length, SocketAddress destination) public DatagramPacket(byte[] data, int offset, int length, SocketAddress destination)

7 The get Methods DatagramPacket has six methods that retrieve different parts of a datagram: the actual data plus several fields from its header. These methods are mostly used for datagrams received from the network. public InetAddress getAddress() public int getPort() public SocketAddress getSocketAddress() public byte[] getData() public int getLength() public int getOffset()

8 get methods (cont..) 1. public InetAddress getAddress() :
The getAddress() method returns an InetAddress object containing the address of the remote host. 2. public int getPort() : The getPort() method returns an integer specifying the remote port. 3. public SocketAddress getSocketAddress() : The getSocketAddress() method returns a SocketAddress object containing the IP address and port of the remote host. 4. public byte[] getData() : The getData() method returns a byte array containing the data from the datagram.

9 get methods (cont..) 5. public int getLength() :
The getLength() method returns the number of bytes of data in the datagram. 6. public int getOffset() : This method simply returns the point in the array returned by getData() where the data from the datagram begins. NOTE: Please refer Example 12-3 in Text Book.

10 Setter Methods Java also provides several methods for changing the data, remote address, and remote port after the datagram has been created. public void setData(byte[ ] data) public void setData(byte[] data, int offset, int length) public void setAddress(InetAddress remote) public void setPort(int port) public void setAddress(SocketAddress remote) public void setLength(int length)

11 Setter Methods (cont..) 1. public void setData(byte[ ] data) : The setData() method changes the payload of the UDP datagram. 2. public void setData(byte[] data, int offset, int length) : The setData() method provides an alternative approach to sending a large quantity of data. 3. public void setAddress(InetAddress remote) : The setAddress() method changes the address a datagram packet is sent to. 4. public void setPort(int port) : The setPort() method changes the port a datagram is addressed to.

12 Setter Methods (cont..) 5. public void setAddress(SocketAddress remote) : The setSocketAddress() method changes the address and port a datagram packet is sent to. 6. public void setLength(int length) : The setLength() method changes the number of bytes of data in the internal buffer, that are considered to be part of the datagram’s data.

13 DatagramSocket Class To send or receive a DatagramPacket, you must open a datagram socket. In Java, a datagram socket is created and accessed through the DatagramSocket class. Syntax: public class DatagramSocket extends Object Note: All datagram sockets bind to a local port, on which they listen for incoming data and which they place in the header of outgoing datagrams.

14 DatagramSocket Constructors
public DatagramSocket() throws SocketException public DatagramSocket(int port) throws SocketException public DatagramSocket(int port, InetAddress interface) throws SocketException public DatagramSocket(SocketAddress interface) throws SocketException protected DatagramSocket(DatagramSocketImpl impl) throws SocketException

15 DatagramSocket Constructors (cont..)
1. public DatagramSocket() throws SocketException : This constructor creates a socket that is bound to an anonymous port. Pick this constructor for a client that initiates a conversation with a server. 2. public DatagramSocket(int port) throws SocketException : This constructor creates a socket that listens for incoming datagrams on a particular port, specified by the port argument. Use this constructor to write a server that listens on a well-known port. 3. public DatagramSocket(int port, InetAddress interface) throws SocketException : This constructor is primarily used on multihomed hosts.

16 DatagramSocket Constructors (cont..)
4. public DatagramSocket(SocketAddress interface) throws SocketException : This constructor is similar to the previous one except that the network interface address and port are read from a SocketAddress. 5. protected DatagramSocket(DatagramSocketImpl impl) throws SocketException : This constructor enables subclasses to provide their own implementation of the UDP protocol.

17 Sending and Receiving Datagrams
The primary task of the DatagramSocket class is to send and receive UDP datagrams.One socket can both send and receive. Indeed, it can send and receive to and from multiple hosts at the same time. 1. public void send(DatagramPacket dp) throws IOException 2. public void receive(DatagramPacket dp) throws IOException 3. public void close() 4. public int getLocalPort() 5. public InetAddress getLocalAddress() 6. public SocketAddress getLocalSocketAddress()

18 Managing Connections public void connect(InetAddress host, int port)
public void disconnect() public int getPort() Public InetAddress getInetAddress() public InetAddress getRemoteSocketAddress()

19 Steps to Create UDP Server Socket
1. Create a DatagramSocket object DatagramSocket datagramSocket = new DatagramSocket(1234); 2. Create a buffer for incoming datagrams This is achieved by creating an array of bytes. Example: byte[] buffer = new byte[256]; 3. Create a DatagramPacket object for the incoming datagrams The constructor for this object requires two arguments: • the previously-created byte array. • the size of this array. Example: DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length); 4. Accept an incoming datagram Example: datagramSocket.receive(inPacket);

20 Steps to Create UDP Server Socket (cont..)
5. Accept the sender’s address and port from the packet Methods getAddress and getPort of our DatagramPacket object are used for this. Example: InetAddress clientAddress = inPacket.getAddress(); int clientPort = inPacket.getPort(); 6. Retrieve the data from the buffer For convenience of handling, the data will be retrieved as a string, using an overloaded form of the String constructor that takes three arguments: a byte array the start position within the array (equal to 0 here) the number of bytes (equal to full size of buffer here) String message = new String(inPacket.getData(),0,inPacket.getLength());

21 Steps to Create UDP Server Socket (cont..)
7. Create the response datagram Create a DatagramPacket object, using an overloaded form of the constructor that takes four arguments: the byte array containing the response message the size of the response the client’s address the client’s port number The first of these arguments is returned by the getBytes method of the String class (acting on the desired String response). Example: DatagramPacket outPacket = new DatagramPacket(response.getBytes(), response.length(),clientAddress, clientPort); (Here, response is a String variable holding the return message.)

22 Steps to Create UDP Server Socket (cont..)
8. Send the response datagram This is achieved by calling method send of our DatagramSocket object, supplying our outgoing DatagramPacket object as an argument. Example: datagramSocket.send(outPacket); Note: Steps 4–8 may be executed indefinitely (within a loop). Under normal circumstances, the server would probably not be closed down at all. However, if an exception occurs, then the associated DatagramSocket should be closed, as shown in step 9 below. 9. Close the DatagramSocket This is effected simply by calling method close of our DatagramSocket object. Example: datagramSocket.close(); Sample Program – please refer Text Book

23 Client – UDP Socket

24 Steps to Create UDP Client Socket
1. Create a DatagramSocket object This is similar to the creation of a DatagramSocket object in the server program, but the constructor here requires no argument, since a default port (at the client end) will be used. Example: DatagramSocket datagramSocket = new DatagramSocket(); 2. Create the outgoing datagram This step is exactly as for step 7 of the server program. Example: DatagramPacket outPacket = new DatagramPacket(message.getBytes(), message.length(), host, PORT); Note: Here, message is a string variable holding the required message. 3. Send the datagram message This is achieved by calling method send of the DatagramSocket object, supplying our outgoing DatagramPacket object as an argument. Example: datagramSocket.send(outPacket);

25 Steps to Create UDP Client Socket (cont..)
Note: Steps 4–6 below are exactly the same as steps 2–4 of the server procedure. 4. Create a buffer for incoming datagrams Example: byte[] buffer = new byte[256]; 5. Create a DatagramPacket object for the incoming datagrams Example: DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length); 6. Accept an incoming datagram Example: datagramSocket.receive(inPacket); 7. Retrieve the data from the buffer This is the same as step 6 in the server program. Example: String response = new String(inPacket.getData(),0, inPacket.getLength()); 8. Close the DatagramSocket This is the same as step 9 in the server program. Example: datagramSocket.close(); Program – Please refer Text Book


Download ppt "NETWORK PROGRAMMING CNET 441"

Similar presentations


Ads by Google