NETWORK PROGRAMMING CNET 441 CHAPTER 3 UDP Sockets

  • Slides: 25
Download presentation
NETWORK PROGRAMMING CNET 441 CHAPTER - 3 UDP Sockets 1

NETWORK PROGRAMMING CNET 441 CHAPTER - 3 UDP Sockets 1

Chapter 3: Objectives After Completing the Chapter 3, the student can understand the following

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

Datagram. Packet and Datagram. Socket Class 1. Java’s implementation of UDP is split into

Datagram. Packet and Datagram. Socket Class 1. Java’s implementation of UDP is split into two classes: Datagram. Packet and Datagram. Socket. 2. The Datagram. Packet class stuffs bytes of data into UDP packets called datagrams and lets you unstuff datagrams that you receive. 3. A Datagram. Socket sends as well as receives UDP datagrams. 4. To send data, you put the data in a Datagram. Packet and send the packet using a Datagram. Socket. 5. To receive data, you take a Datagram. Packet object from a Datagram. Socket 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. 3

Structure of UDP Datagram 4

Structure of UDP Datagram 4

Datagram. Packet Class In Java, a UDP datagram is represented by an instance of

Datagram. Packet Class In Java, a UDP datagram is represented by an instance of the Datagram. Packet class: public final class Datagram. Packet extends Object Constructors for receiving datagrams: 1. public Datagram. Packet(byte[] buffer, int length) 2. public Datagram. Packet(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. 5

Constructors for sending datagrams 1. public Datagram. Packet(byte[] data, int length, Inet. Address destination,

Constructors for sending datagrams 1. public Datagram. Packet(byte[] data, int length, Inet. Address destination, int port) 2. public Datagram. Packet(byte[] data, int offset, int length, Inet. Address destination, int port) 3. public Datagram. Packet(byte[] data, int length, Socket. Address destination) 4. public Datagram. Packet(byte[] data, int offset, int length, Socket. Address destination) 6

The get Methods Datagram. Packet has six methods that retrieve different parts of a

The get Methods Datagram. Packet 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. 1. public Inet. Address get. Address() 2. public int get. Port() 3. public Socket. Address get. Socket. Address() 4. public byte[] get. Data() 5. public int get. Length() 6. public int get. Offset() 7

get methods (cont. . ) 1. public Inet. Address get. Address() : The get.

get methods (cont. . ) 1. public Inet. Address get. Address() : The get. Address() method returns an Inet. Address object containing the address of the remote host. 2. public int get. Port() : The get. Port() method returns an integer specifying the remote port. 3. public Socket. Address get. Socket. Address() : The get. Socket. Address() method returns a Socket. Address object containing the IP address and port of the remote host. 4. public byte[] get. Data() : The get. Data() method returns a byte array containing the data from the datagram. 8

get methods (cont. . ) 5. public int get. Length() : The get. Length()

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

Setter Methods Java also provides several methods for changing the data, remote address, and

Setter Methods Java also provides several methods for changing the data, remote address, and remote port after the datagram has been created. 1. public void set. Data(byte[ ] data) 2. public void set. Data(byte[] data, int offset, int length) 3. public void set. Address(Inet. Address remote) 4. public void set. Port(int port) 5. public void set. Address(Socket. Address remote) 6. public void set. Length(int length) 10

Setter Methods (cont. . ) 1. public void set. Data(byte[ ] data) : The

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

Setter Methods (cont. . ) 5. public void set. Address(Socket. Address remote) : The

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

Datagram. Socket Class To send or receive a Datagram. Packet, you must open a

Datagram. Socket Class To send or receive a Datagram. Packet, you must open a datagram socket. In Java, a datagram socket is created and accessed through the Datagram. Socket class. Syntax: public class Datagram. Socket 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. 13

Datagram. Socket Constructors 1. public Datagram. Socket() throws Socket. Exception 2. public Datagram. Socket(int

Datagram. Socket Constructors 1. public Datagram. Socket() throws Socket. Exception 2. public Datagram. Socket(int port) throws Socket. Exception 3. public Datagram. Socket(int port, Inet. Address interface) throws Socket. Exception 4. public Datagram. Socket(Socket. Address interface) throws Socket. Exception 5. protected Datagram. Socket(Datagram. Socket. Impl impl) throws Socket. Exception 14

Datagram. Socket Constructors (cont. . ) 1. public Datagram. Socket() throws Socket. Exception :

Datagram. Socket Constructors (cont. . ) 1. public Datagram. Socket() throws Socket. Exception : 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 Datagram. Socket(int port) throws Socket. Exception : 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 wellknown port. 3. public Datagram. Socket(int port, Inet. Address interface) throws Socket. Exception : This constructor is primarily used on multihomed hosts. 15

Datagram. Socket Constructors (cont. . ) 4. public Datagram. Socket(Socket. Address interface) throws Socket.

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

Sending and Receiving Datagrams The primary task of the Datagram. Socket class is to

Sending and Receiving Datagrams The primary task of the Datagram. Socket 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(Datagram. Packet dp) throws IOException 2. public void receive(Datagram. Packet dp) throws IOException 3. public void close() 4. public int get. Local. Port() 5. public Inet. Address get. Local. Address() 6. public Socket. Address get. Local. Socket. Address() 17

Managing Connections 1. public void connect(Inet. Address host, int port) 2. public void disconnect()

Managing Connections 1. public void connect(Inet. Address host, int port) 2. public void disconnect() 3. public int get. Port() 4. Public Inet. Address get. Inet. Address() 5. public Inet. Address get. Remote. Socket. Address() 18

Steps to Create UDP Server Socket 1. Create a Datagram. Socket object Ø Datagram.

Steps to Create UDP Server Socket 1. Create a Datagram. Socket object Ø Datagram. Socket datagram. Socket = new Datagram. Socket(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 Datagram. Packet object for the incoming datagrams The constructor for this object requires two arguments: • the previously-created byte array. • the size of this array. Example: Datagram. Packet in. Packet = new Datagram. Packet(buffer, buffer. length); 4. Accept an incoming datagram Example: datagram. Socket. receive(in. Packet); 19

Steps to Create UDP Server Socket (cont. . ) 5. Accept the sender’s address

Steps to Create UDP Server Socket (cont. . ) 5. Accept the sender’s address and port from the packet Methods get. Address and get. Port of our Datagram. Packet object are used for this. Example: Inet. Address client. Address = in. Packet. get. Address(); int client. Port = in. Packet. get. Port(); 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) Example: String message = new String(in. Packet. get. Data(), 0, in. Packet. get. Length()); 20

Steps to Create UDP Server Socket (cont. . ) 7. Create the response datagram

Steps to Create UDP Server Socket (cont. . ) 7. Create the response datagram Create a Datagram. Packet 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 get. Bytes method of the String class (acting on the desired String response). Example: Datagram. Packet out. Packet = new Datagram. Packet(response. get. Bytes(), response. length(), client. Address, client. Port); (Here, response is a String variable holding the return message. ) 21

Steps to Create UDP Server Socket (cont. . ) 8. Send the response datagram

Steps to Create UDP Server Socket (cont. . ) 8. Send the response datagram This is achieved by calling method send of our Datagram. Socket object, supplying our outgoing Datagram. Packet object as an argument. Example: datagram. Socket. send(out. Packet); 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 Datagram. Socket should be closed, as shown in step 9 below. 9. Close the Datagram. Socket This is effected simply by calling method close of our Datagram. Socket object. Example: datagram. Socket. close(); 22 Sample Program – please refer Text Book

Client – UDP Socket 23

Client – UDP Socket 23

Steps to Create UDP Client Socket 1. Create a Datagram. Socket object This is

Steps to Create UDP Client Socket 1. Create a Datagram. Socket object This is similar to the creation of a Datagram. Socket object in the server program, but the constructor here requires no argument, since a default port (at the client end) will be used. Example: Datagram. Socket datagram. Socket = new Datagram. Socket(); 2. Create the outgoing datagram This step is exactly as for step 7 of the server program. Example: Datagram. Packet out. Packet = new Datagram. Packet(message. get. Bytes(), 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 Datagram. Socket object, supplying our outgoing Datagram. Packet object as an argument. 24 Example: datagram. Socket. send(out. Packet);

Steps to Create UDP Client Socket (cont. . ) Note: Steps 4– 6 below

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 Datagram. Packet object for the incoming datagrams Example: Datagram. Packet in. Packet = new Datagram. Packet(buffer, buffer. length); 6. Accept an incoming datagram Example: datagram. Socket. receive(in. Packet); 7. Retrieve the data from the buffer This is the same as step 6 in the server program. Example: String response = new String(in. Packet. get. Data(), 0, in. Packet. get. Length()); 8. Close the Datagram. Socket This is the same as step 9 in the server program. Example: datagram. Socket. close(); Program – Please refer Text Book 25