User Datagram Protocol

Information about User Datagram Protocol

Published on May 24, 2009

Author: ankush85

Source: authorstream.com

Content

User Datagram Protocol : User Datagram Protocol Overview : Overview UDP is a commonly used transport protocol employed by many types of applications A connectionless transport – doesn’t guarantee either packet delivery or packets arrive in sequential order Bytes are grouped together in discrete packets not an ordered sequence of bytes using I/O stream Cont. : Cont. The packet may travel along different path as selected by the various network routers that distribute traffic flow (network congestion, priority of routes and the cost of transmission) Possibility of packet missing or discarded exist No notification is available to update the status of delivery UDP Advantages : UDP Advantages UDP communication can be more efficient than guaranteed-delivery data streams. If the amount is small and the data is sent frequently, it may make sense to avoid the overhead of guaranteed delivery Overhead to establish connection can be reduced as compared to TCP Real time applications that demand up-to-the-second or better performance maybe the candidates for UDP, since fewer delay due to the error checking and flow control of TCP Cont. : Cont. UDP sockets can receive data from more than one host machine Some network protocols specify UDP as the transport mechanism requiring its use Java supports the UDP in the form of two classes: Java.net.DatagramPacket Java.net.DatagramSocket DatagramPacket Class : DatagramPacket Class Represents a data packet intended for transmission using the User Datagram Protocol Packets are containers for a small sequence of bytes and include addressing information such as IP address and a port DatagramPacket Representation : DatagramPacket Representation DatagramPacket IP Address (java.net.InetAddr) Port address Packet data Byte[ ] = {…,…,…,…,…,..,} Cont. : Cont. The meaning of the data stored in a DatagramPacket is determined by its context Incoming packet – IP address (sender) Outgoing packet – IP address (recipient) Creating a DatagramPacket : Creating a DatagramPacket Two reasons to create a new DatagramPacket To send data to a remote machine using UDP To receive data sent by a remote machine using UDP Constructors : Constructors The choice of which constructor to use is determined by its intended purpose Constructor requires the specification of a byte array used to store the UDP packet contents and the length of the data packet Receiving packet : Receiving packet Constructor to be used: DatagramPacket(byte[] buffer, int length) For example: DatagramPacket packet = new DatagramPacket( new byte[256], 356); Sending Packet : Sending Packet Constructor to be used: DatagramPacket(byte[ ] buffer, int length, InetAddress dest_addr, int dest_port) For example: InetAddress addr = InetAddress.getByName(“Hostname”); DatagramPacket packet = new DatagramPacket (new byte[128], 128, addr, 2000); Using a DatagramPacket : Using a DatagramPacket The DatagramPacket class provides some important methods that allow the remote address, remote port, data (as a byte array) and the length of the packet to be retrieved Methods: InetAddress getAddress( ) – returns the IP address from which a DatagramPacket was sent or the destination IP address byte[] getData( ) – returns the contents of the DatagramPacket represented as an array of bytes Cont. : Cont. int getLength( ) – returns the length of the data stored in a DatagramPacket int getPort( ) – returns the port number from which a DatagramPacket was sent or the destination port number void SetAddress(InetAddress addr) – assigns a new destination address to a DatagramPacket void setData(byte[ ] buffer) – assigns a new data buffer to the DatagramPacket void setLength(int length) – assigns a new length to the DatagramPacket void setPort(int port) – assigns a new destination port to a DatagramPacket DatagramSocket Class : DatagramSocket Class Provides access to a UDP socket that allows UDP packets to be sent and received A DatagramPacket is used to represent a UDP packet and must be created prior to receiving any packets The same DatagramSocket can be used to receive packets as well as to send them. Cont. : Cont. Read operations are blocking that means the application will continue to wait until a packet arrives Can cause an application to stall of the sender resubmit packets in case error occurs during transmission Use multiple threads of execution or nonblocking IO operation Creating a DatagramSocket : Creating a DatagramSocket Can be used to send and receive packets Each DatagramSocket binds to a port on the local machine which is used for addressing packets Important for a server application not for client Constructors : Constructors To create a client DatagramSocket, the constructor to be used: DatagramSocket( ) throws java.net.SocketException To create a server DatagramSocket, the constructor to be used: DatagramSocket( int port) – takes the port number to be bound by the UDP service Using a DatagramSocket : Using a DatagramSocket Is used to receive incoming and to send outgoing UDP packets Provides methods to send and to receive packets as well as tp specify a timeout value when nonblocking IO is being used, to inspect and modify maximum UDP packet sizes and to close the socket Methods : Methods void close ( ) – closes the socket and unbinds it from the local port void connect(InetAddress remote_addr, int remote_port) – restricts access to the specified remote address and port void disconnect( ) – disconnects the DatagramSocket and removes any restrictions imposed on it by an earlier connect operation InetAddress getInetAddress( ) – returns the local address to which the socket is connected or null if no such connection exists Cont. : Cont. int getPort( ) – returns the remote port to which the socket is connected or -1 if no such connection exists InetAddress getLocalAddress( ) – returns the local address to which the socket is bound int getLocalPort( ) – returns the local port to which the socket is bound int getReceiveBufferSize( ) – returns the maximum buffer size used for incoming UDP packets int getSendBufferSize( ) – returns the maximum buffer size used for outgoing UDP packets Cont. : Cont. Int getSoTimeout( ) – returns the value of the timeout socket option. This value is used to determine the number of miliseconds a read operation will block before throwing an exception. By default, the value will be 0 indicating that blocking IO will be used Void receive(DatagramPacket packet) – read a UDP packet and stores the contents in the specified packet Void send(DatagramPacket packet) – sends a UDP packet represented by the specified packet parameter Cont. : Cont. Void setReceiveBufferSize(int length) – sets the maximum buffer size used for incoming UDP packets Void setSendBufferSize(int length) – sets the maximum buffer size used for outgoing UDP packets Void setSoTimeout(int duration) – sets the value of the timeout socket option Listening for UDP Packets : Listening for UDP Packets Before an application can read UDP packets sent to it by remote machines, it must bind a socket to a local UDP port using DatagramSocket and create a DatagramPacket that will act as a container for the UDP packet’s data When an application wishes to read UDP packets, it calls the DatagramSocket.receive method which copies a UDP packet into the specified DatagramPacket. The contents of the DatagramPacket are processed and the process is repeated as needed Example : Example DatagramPacket packet = new DatagramPacket(new byte[256], 256); DatagramSocket = new DatagramSocket(2000); boolean finished = false; while (!finished) { socket.receive(packet); //process the packet } Socket.close(): Packet DatagramSocket DatagramPacket UDP Application Translates packet into a datagramPacket Reads packets Connect to Streams : Connect to Streams Many developers prefers to use Java I/O streams to process data, using DatInputStream or a BufferedReader to access the contents of byte arrays For example: ByteArrayInputStream bin = new ByteArrayInputStream (packet.getData() ); DataInputStream din = new DataInputStream (bin); //Read the contents of the UDP packet Sending UDP packets : Sending UDP packets Using the same interface (DatagramSocket) to receive and send packet When sending a packet, the application must create a datagramPacket, set the address and port information and write the data intended for transmission to its byte array If replying to a received packet, the address and port information will already be stored and only the data need to be overwritten Once the packet is ready, the send method will be invoked Sending packets : Sending packets UDP application DatagramSocket DatagramPacket packet Binds to a UDP port Constructs packet Send DatagramPacket Using DatagramSocket Code Snippet : Code Snippet DatagramSocket socket = new DatagramSocket (2000); DatagramPacket packet = new DatagramPacket(new byte[256], 256); Packet.setAddress(InetAddress.getByName(Somehost) ); Packet.setPort(2000); Boolean finished = false; While ( !finished ) { //write data to packet buffer ….. socket.send (packet); //Do something else, like read other packets etc ….. } Socket.close ( );

Related presentations


Other presentations created by ankush85

Nanotechnology
03. 04. 2009
0 views

Nanotechnology

Human Resource Management System
06. 03. 2009
0 views

Human Resource Management System

Nanotechnology
27. 03. 2009
0 views

Nanotechnology

INTRODUCTION TO NANOTECHNOLOGY
20. 11. 2009
0 views

INTRODUCTION TO NANOTECHNOLOGY

Nanotechnology in Sports
22. 11. 2009
0 views

Nanotechnology in Sports

Computer Architecture
14. 07. 2009
0 views

Computer Architecture

HR
18. 03. 2009
0 views

HR

Hospital Management System
08. 04. 2009
0 views

Hospital Management System

Welcome to Visual Basic
06. 03. 2009
0 views

Welcome to Visual Basic

HTML
20. 04. 2009
0 views

HTML

Computer Languages
03. 06. 2013
0 views

Computer Languages

Taking Care of Computer
03. 06. 2013
0 views

Taking Care of Computer

Understanding Camera
16. 10. 2012
0 views

Understanding Camera

Photography Technical Terms
25. 09. 2012
0 views

Photography Technical Terms

Basics of Photography
25. 09. 2012
0 views

Basics of Photography

AIR MUSCLES
03. 01. 2012
0 views

AIR MUSCLES

Molecular Nanotechnology
24. 11. 2009
0 views

Molecular Nanotechnology

Nanotech Innovation
22. 11. 2009
0 views

Nanotech Innovation

FINANCIAL  MARKET
16. 05. 2009
0 views

FINANCIAL MARKET

Operating System
19. 04. 2009
0 views

Operating System

Management Control
10. 07. 2009
0 views

Management Control

Accounting Principles
01. 07. 2009
0 views

Accounting Principles

Balance Sheet
21. 07. 2009
0 views

Balance Sheet

Balance Sheet Auditing
01. 07. 2009
0 views

Balance Sheet Auditing

BRANCH AND BOUND
05. 03. 2009
0 views

BRANCH AND BOUND

THE  THREE  BRANCHES  OF GOVERNMENT
13. 06. 2009
0 views

THE THREE BRANCHES OF GOVERNMENT

Structure of Atom
11. 05. 2009
0 views

Structure of Atom

Compression Techniques
05. 03. 2009
0 views

Compression Techniques

DYNAMIC PROGRAMMING
06. 03. 2009
0 views

DYNAMIC PROGRAMMING

Quantum Mechanics
25. 08. 2009
0 views

Quantum Mechanics

Marketing Plan
19. 06. 2009
0 views

Marketing Plan

Book Keeping
24. 06. 2009
0 views

Book Keeping

DFDS
02. 10. 2008
0 views

DFDS

Semiconductors
25. 04. 2009
0 views

Semiconductors

Organic  Chemistry
28. 04. 2009
0 views

Organic Chemistry

Atoms Molecules and Ions
17. 06. 2009
0 views

Atoms Molecules and Ions

Covalent Bond
25. 04. 2009
0 views

Covalent Bond

Cost Accounting Standards
25. 06. 2009
0 views

Cost Accounting Standards

Crisis Management
19. 06. 2009
0 views

Crisis Management

Marketing
18. 03. 2009
0 views

Marketing

Business Strategy
27. 04. 2009
0 views

Business Strategy

Time Management
23. 03. 2009
0 views

Time Management

Networking Protocols
23. 05. 2009
0 views

Networking Protocols

Network Layer
23. 05. 2009
0 views

Network Layer

Final Accounts
24. 06. 2009
0 views

Final Accounts

ARTIFICIAL  INTELLIGENCE
03. 04. 2009
0 views

ARTIFICIAL INTELLIGENCE

Play with C
08. 04. 2009
0 views

Play with C

Software Development Cycle
23. 03. 2009
0 views

Software Development Cycle

THE GREEDY METHOD
06. 03. 2009
0 views

THE GREEDY METHOD

JOB SEQUENCING
06. 03. 2009
0 views

JOB SEQUENCING

Electricity and Magnetism
29. 04. 2009
0 views

Electricity and Magnetism

Optical Fiber
24. 05. 2009
0 views

Optical Fiber

Quality Assurance
06. 03. 2009
0 views

Quality Assurance

Object-oriented Design
11. 04. 2009
0 views

Object-oriented Design

A.R. Rahman
08. 03. 2009
0 views

A.R. Rahman

Hollywood Female Celebrities
13. 03. 2009
0 views

Hollywood Female Celebrities

Flow nets
04. 09. 2009
0 views

Flow nets

Energy and Nanotechnology
22. 11. 2009
0 views

Energy and Nanotechnology

LOGIC  DESIGN
06. 03. 2009
0 views

LOGIC DESIGN

VB-IDE
06. 03. 2009
0 views

VB-IDE

DLF IPL FINAL
25. 05. 2009
0 views

DLF IPL FINAL

MINIMUM SPANNING TREES
06. 03. 2009
0 views

MINIMUM SPANNING TREES

CPU Scheduling
05. 03. 2009
0 views

CPU Scheduling

Virtual Memory
05. 03. 2009
0 views

Virtual Memory

POK
09. 05. 2009
0 views

POK

2D Transformations
05. 03. 2009
0 views

2D Transformations

Greedy Algorithms
05. 03. 2009
0 views

Greedy Algorithms

BACKTRACKING
05. 03. 2009
0 views

BACKTRACKING

DIVIDE And CONQUER
06. 03. 2009
0 views

DIVIDE And CONQUER

ELEMENTARY DATA STRUCTURES
06. 03. 2009
0 views

ELEMENTARY DATA STRUCTURES

JPEG Compression
06. 03. 2009
0 views

JPEG Compression

Mpeg-compression
06. 03. 2009
0 views

Mpeg-compression

NP - HARD
06. 03. 2009
0 views

NP - HARD

TRAVELLING SALESPERSON PROBLEM
06. 03. 2009
0 views

TRAVELLING SALESPERSON PROBLEM

VBA Introduction
06. 03. 2009
0 views

VBA Introduction

Introduction to Java
09. 03. 2009
0 views

Introduction to Java

Java  Basics
09. 03. 2009
0 views

Java Basics

Heuristic Search
13. 03. 2009
0 views

Heuristic Search

HSM
19. 03. 2009
0 views

HSM

Tata's  Nano  Car
25. 03. 2009
0 views

Tata's Nano Car

Air Cranes
08. 04. 2009
0 views

Air Cranes

Newborn Care
10. 04. 2009
0 views

Newborn Care

Photosynthesis Process
10. 04. 2009
0 views

Photosynthesis Process

ActionScript
13. 04. 2009
0 views

ActionScript

Xml
15. 04. 2009
0 views

Xml

Snakes mis use
17. 04. 2009
0 views

Snakes mis use

Php Web Development
21. 04. 2009
0 views

Php Web Development

Cricket Intro
21. 04. 2009
0 views

Cricket Intro

Cricket Umpiring and Rules
21. 04. 2009
0 views

Cricket Umpiring and Rules

Arm and Forearm
23. 04. 2009
0 views

Arm and Forearm

Elements Ions Isotopes
25. 04. 2009
0 views

Elements Ions Isotopes

Chemical Bond
25. 04. 2009
0 views

Chemical Bond

Discovering Newtons Laws
29. 04. 2009
0 views

Discovering Newtons Laws

FreeFall
29. 04. 2009
0 views

FreeFall

Digital photography
26. 04. 2009
0 views

Digital photography

Health Effects of Alcohol
26. 04. 2009
0 views

Health Effects of Alcohol

Poetry Terminology
27. 04. 2009
0 views

Poetry Terminology

Indian Force
05. 05. 2009
0 views

Indian Force

Machine Intelligence
14. 05. 2009
0 views

Machine Intelligence

Data Link Layer
16. 05. 2009
0 views

Data Link Layer

Database Development Cycle
15. 05. 2009
0 views

Database Development Cycle

Queue
15. 05. 2009
0 views

Queue

Presentaion Skills
23. 04. 2009
0 views

Presentaion Skills

Network Layers
23. 05. 2009
0 views

Network Layers

Narmada Dam, India
23. 05. 2009
0 views

Narmada Dam, India

Linear Momentum
28. 05. 2009
0 views

Linear Momentum

Stack and Queue
04. 06. 2009
0 views

Stack and Queue

Information Management
19. 06. 2009
0 views

Information Management

Role of Senior Management
19. 06. 2009
0 views

Role of Senior Management

Wake Up India
07. 06. 2009
0 views

Wake Up India

Adobe Flex 3.0
13. 06. 2009
0 views

Adobe Flex 3.0

Adobe Flex Presentation
13. 06. 2009
0 views

Adobe Flex Presentation

Introduction to Adobe Flex
13. 06. 2009
0 views

Introduction to Adobe Flex

Adobe Flash Media Server
13. 06. 2009
0 views

Adobe Flash Media Server

Dreamweaver
13. 06. 2009
0 views

Dreamweaver

Adobe Flash
13. 06. 2009
0 views

Adobe Flash

Flash CS4 Professional
13. 06. 2009
0 views

Flash CS4 Professional

Adobe Flash Lite
13. 06. 2009
0 views

Adobe Flash Lite

Digital Camera
13. 06. 2009
0 views

Digital Camera

Mozilla_Firefox
15. 06. 2009
0 views

Mozilla_Firefox

Managerial Accounting
27. 06. 2009
0 views

Managerial Accounting

Accounting  Information  System
01. 07. 2009
0 views

Accounting Information System

RETAILING AND MARKETING
06. 07. 2009
0 views

RETAILING AND MARKETING

ACCOUNTING IN BUSINESS
05. 07. 2009
0 views

ACCOUNTING IN BUSINESS

STRATEGIC RETAIL MANAGEMENT
05. 07. 2009
0 views

STRATEGIC RETAIL MANAGEMENT

Reiki
01. 08. 2009
0 views

Reiki

Using Buttons in PowerPoint
26. 04. 2009
0 views

Using Buttons in PowerPoint

Nanotechnology  for  Students
21. 08. 2009
0 views

Nanotechnology for Students

NANOSCIENCE
25. 08. 2009
0 views

NANOSCIENCE

Fundamentals of Nanoscience
27. 08. 2009
0 views

Fundamentals of Nanoscience

Nanoscience in Nature
27. 08. 2009
0 views

Nanoscience in Nature

Applied Mechanics
04. 09. 2009
0 views

Applied Mechanics

Accounts Payable Training
27. 06. 2009
0 views

Accounts Payable Training