Jan. 20041 Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.

Slides:



Advertisements
Similar presentations
Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
Advertisements

Referring to Java API Specifications
UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
© 2003 Andrea Calvagna 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Andrea Calvagna
© 1999 Elliotte Rusty Harold 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Elliotte Rusty Harold
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
1 Server-Client communication without connection  When the communication consists of sending and/or receiving datagram packets instead of a data stream.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Network Layers Application Transport Network Data-Link Physical bits.
Datagram Programming A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Babak Esfandiari (based on slides by Qusay Mahmoud)
Socket Programming (C/Java)
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Multicast Sockets What is a multicast socket?
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
CS 424/524: Introduction to Java Programming Lecture 25 Spring 2002 Department of Computer Science University of Alabama Joel Jones.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Socket Programming Lee, Sooyong
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
Networking Java (2/3)
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
Java Programming II Java Network (I) Java Programming II.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
Network Programming Introduction
Network Programming Introduction
Socket programming with TCP
„Networking”.
CSCD 330 Network Programming
Socket Programming.
CSCD 330 Network Programming
NETWORK PROGRAMMING CNET 441
Java Socket Programming
CPSC 441 UDP Socket Programming
Chapter 2: Application layer
Server-Client communication without connection
Socket Programming with UDP
Presentation transcript:

Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg

Jan User Datagram Protocol UDP (User Datagrarn Protocol) - a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival - not connection based like TCP If a UDP packet is lost, IT’S LOST. The packets appear in the order they are received not necessarily in the order they were sent.

Jan So Why UDP? Speed! UDP can be up to three times faster than TCP. There are applications where speed is more important than reliability, such as audio and video data.

Jan UDP Java classes using UDP -java. net DatagramPacket() DatagramSocket()

Jan UDP Classes Java has two classes for UDP support java.net.DatagramSocket java.net.DatagramPacket A DatagramSocket is used to send and receive DatagramPacket. Since UDP is connectionless, streams are not used. The maximum size of a Datagrarm packet is limited to slightly less than 64Kbytes.

Jan UDP Classes DatagramSockets are connected to a port that allow for sending and receiving of data. Unlike TCP sockets, there is no distinction between client and server sockets in UDP. Also, a DatagramSocket can be used to send data to multiple different hosts. - This is because the address is stored in the packet, not in the socket. There are 65,536 UDP ports as well as TCP ports that are separate from each other.

Jan DatagramPacket Class You construct a DatagrarmPacket by using one of the two constructors. - public DatagramPacket(byte[] data, int length) - public DatagramPacket(byte[] data, int length, InetAddress addr, int port) The byte array is passed by reference and not by value. A change in its contents will change the packet.

Jan DatagramPacket Class Example of using the constructors: String data = "My UDP Packet”; byte[] h = data.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length) You can also pass in the host and port to which the packet is to be sent.

Jan DatagramPacket Class try { InetAddress tr = new InetAddress(" int port = 9100; String data = "Another UDP Packet"; byte[] b = data.getBytes( ); DatagramPacket dp = new DatagramPacket(b, b.length, tr,port); }//try catch (UnknownHostException e) { System.err.printin(e); }//catch

Jan DatagramPacket Class After creation of a DatagramPacket, it is possible to change the date, the length of the data, the port, or the address by using the following methods: setAddress(InetAddress addr) setPort(int port) setData(byte buf[]) setLength(int length) To retrieve the current status of a DatagramPacket, use the corresponding get methods.

Jan DatagramSocket Class This class is a connection to a port that does the transmitting and receiving. Unlike TCP sockets, the same Datagramsocket can be used to send and receive. The Datagramsocket class has three constructors.

Jan DatagramSocket Class public DatagramSocket() public DatagramSocket(int port) public Datagramsocket(int port, InetAddress addr) all three constructors throw an IOException. The first constructor is mainly used to act as clients. The other two that specify a port and optionally an IP address are intended for servers that must run on a well known port.

Jan Sending UDP Datagrams 1) Convert the data into a byte array. 2) Pass this byte array, length of data, port and address to the DatagramPacket constructor. 3) Create a DatagramSocket object. 4) Pass the Datagram packet to the send method.

Jan Sending UDP Datagrams try { TnetAddress tr=new InetAddress("win.tr1abs.ca”); int pt=9100; string data = "Sending a Datagram"; byte[] b = data.getBytes(); DatagramPacket dP=new DatagramPacket(b,b.length,tr,pt); }//try catch (UnknownHostException e) { system.err.println(e); }//catch

Jan Sending UDP Datagrams try { DatagramSocket sender = new Datagramsocket(); sender.send(dP); }//try catch (IOException e) { system.out.printIn(e); }//catch

Jan Receiving UDP Datagrams 1) Construct a DatagramSocket object on the port you wish to listen. 2) Pass it an empty Datagrampacket object to the Datagramsocket’s receive() method. 3) Use methods getport ( ), getAddress( ), getData ( ), getLength( ) to retrieve information about the data.

Jan Receiving UDP Datagrams try { byte buf=new byte[655361; DatagramPacket dp = new DatagramPacket(buf, buf.length); DatagramSocket ds=newDatagramSocket(9100); ds.receive(dp); byte[] data=dp.getData(); String s=new String(data, 0, data.getLength()); System.out.println(s); }//try catch (IOException e) { System.err.println(e); }//catch

Jan UDP Echo Example As with the TCP echo port, the UDP echo port is port 7. When this port receives a datagram, it copies the data and sends it back to the user. As with the TCP echo example, the UDP echo example will read in data from System.in, send it to the echo server on port 7 and then display the results. Remember that since UDP is connectionless, a packet might be lost somewhere between the client and the server.

Jan UDP Echo Example import java.io.*; import java.net.*; public class UDPEcho extends Thread { static int port=7; static volatile boolean running=false; DatagramSocket ds; public static void main(String args[]) { InputstreamReader isr=new InputStreamReader(System.in); BufferedReader in = new BufferedReader(isr); String line;

Jan UDP Echo Example running=true; try{ while ((line==in.readLine()) != null) { byte[] data = line.getBytes(); DatagramPacket dP = new DatagramPacket (data, data.length, server, port); ds.send(dp); Thread.yield(); }//while }//try

Jan UDP Echo Example catch(IoException e) { system.err.println(e); } //catch running = false; } //main public UDPEcho(DatagramSocket ds) { this.ds=ds; } //UDPEcho constructor

Jan UDP Echo Example public void run() { byte[] buf = new byte[1024]; String line; DatagramPacket incoming = new DatagramPacket (buf, buf.length); while(running) { try{ ds.receive(incoming); byte[] data = incoming.getData(); line = new String(data,0,incoming.getLength());

Jan UDP Echo Example System.out.println("Echo: ", line); } //try catch(IOException e){ system.err.println(e); }//catch }//while }//run }//UDPEcho class