1 / 10

Datagram Programming

Datagram Programming. A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed. Connectionless It need a socket It need a package

delling-ull
Download Presentation

Datagram Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Datagram Programming • A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed. • Connectionless • It need a socket • It need a package • It is more efficient for some application which does not require the reliable, point-to-point channel provided by TCP

  2. DatagramPacket class • The DatagramPacket class represents a datagram packet. Datagram packets are used to implement a connectionless packet delivery service. • Each message is routed from one machine to another based solely on information contained within the packet. • Packet delivery is not guaranteed

  3. Java.net.DatagramPacket int length InetAddress address int port DatagramPacket(byte[] buf, int length, InetAddress host, int port) DatagramPacket(byte[] buf, int length) byte[] getData() void setData(byte[] buf)

  4. DatagramSocket class • DatagramSocket class represents a socket for sending and receiving datagram packets. • A datagram socket is the sending or receiving point for a packet delivery service. • Each packet sent or received on a datagram socket is individually addressed and routed • Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.

  5. Create a server datagram • To create a server DatagramSocket use the constructor DatagramScoket(int port), which binds the socket with the specified port on the local host machine. • To create a client DatagramSocket, use the constructor DatagramSocket(), which binds the socket with any available port on the local host machine. • To send data, you need to create a packet, fill in the contents, specify the Internet address and port number for the receiver and invoke the send(packet) method on a DatagramSocket • To receive data, you need to create an empty packet and invoke the receive (packet) method on a DatagramSocket.

  6. DatagramServer • DatagramSocket socket; • socket = new DatagramSocket(8000); • DatagramPacket recv = new DatagramPacket( buf, buf.length); • socket.receive(recv); • Get data from buf or from recv.getData()

  7. DatagramClient • DatagramSocket socket; • socket = new DatagramSocket(); • byte[] buf = new byte[256]; • InetAddress add = new InetAddress(serverName); • DatagramPacket sd = new DatagramPacket(buf, buf.length, add, 8000); • // fill in the contents in buf • socket.send(sd); • DatagramPacket recv = new DatagramPacket(buf, buf.length); • socket.receive(recv);

  8. notes • Since datagram are connectionless, a DatagramPacket can be sent to multiple clients, and multiple clients can receive a packet from a same server. • The server creates a DatagramSocket on port 8000. No DatagramSocket can be created again on the same port • The client creates a DatagramSocket on an available port. The port number is dynamically assigned to the socket. • You can launch multiple clients simultaneously and each client’s datagram will be different

  9. Convert data for sending • Read data as a string (str)from a text field • Str.trim() will return a string(trim_str) with blank characters trimmed on both sides • Trim_str.getBytes() will store the string in array of Bytes and return it • Then create the packet with the byte [] • Then call socket.setData( packet)

  10. Extract data from receiving • Recv = new DatagramPacket(buf, buf.length); • socket.receive(recv); • Convert to a string • String str = new String(buf); • str.trim(); • Convert to numerical value • Double x = Double.parseDouble(str);

More Related