Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright 1996-2007 Lecture 8a Application.

Similar presentations


Presentation on theme: "1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright 1996-2007 Lecture 8a Application."— Presentation transcript:

1 1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright 1996-2007 Lecture 8a Application Layer – Socket Programming in Java - UDP Reading: Chapter 2 and Relevant Links

2 UDP Sockets in Java Less complex Used in bandwidth-limited applications, where the overhead associated with resending packets is not tolerable A good example: real-time applications Incurs fewer overheads

3 UDP

4 4 UDP Socket Programming UDP no real “connection” between client and server No handshaking Sender attaches IP address and destination port to each packet Server must extract IP address and port of sender from received packet, so answer can be sent back! UDP, transmitted data may be received out of order, or lost

5 5 Client/server socket interaction: UDP close clientSocket Server (running on hostid ) ‏ read reply from clientSocket create socket, clientSocket = DatagramSocket()‏ Client Create, address ( hostid, port=50)‏ send datagram request using clientSocket create socket, port= 50, for incoming request: serverSocket = DatagramSocket()‏ read request from serverSocket write reply to serverSocket specifying client host address, port number

6 6 UDP Summary No connection setup – no “pipe” Main Differences between UDP vs TCP Each batch of bytes sent with attached address information No special ServerSocket class in java

7 UDP Summary Create a packet Push it out into the network through a socket Server accepts the packet addressed to him Mail is a lot like UDP Each letter needs the address of the destination Independent letters sent to the same address

8 The Two Major Classes DatagramSocket Class In order for two agents to talk to each other over a UDP connection, a DatagramSocket object is created to ensure they both connected to a port on their local machines. DatagramPacket Class Each DatagramPacket contains a data buffer, the address of the remote host to send the data to, and the port number the remote agent is listening to

9 9 The DatagramPacket class stuffs bytes of data into UDP packets called datagrams and lets you unstuff datagrams that you receive A DatagramSocket sends and receives data using UDP packets, represented as DatagramPacket objects The remote process can receive the data in the form of a DatagramPacket by calling the receive() method on its DatagramSocket How does it work?

10 10 Java Classes for UDP Datagrams for connectionless protocol Two classes implement datagrams in Java: java.net.DatagramPacket java.net.DatagramSocket DatagramPacket is actual packet of information, an array of bytes, that is transmitted over the network. DatagramSocket is socket that sends and receives DatagramPackets across the network. Think of DatagramPacket as a letter and DatagramSocket as the mailbox that the mail carrier uses to pick up and drop off your letters Need both classes for UDP sockets

11 11 Java Classes for UDP DatagramPacket class provides programmer with two constructors. First is for DatagramPackets that receive data Constructor needs Array to store the data Amount of data to receive Second is for DatagramPackets that send data Constructor needs Array to store the data Amount of data to send Plus destination address and port number

12 12 Java Classes for UDP DatgramSocket represents connectionless socket It provides two constructors, 1. Programmer can specify a port OR 2. Allow system to randomly use a port Methods Two most important methods, send() and receive()‏ Each takes an argument of a constructed DatagramPacket send() method Data in packet is sent to specified host and port receive() method Will block execution until a packet is received by underlying socket, then data copied into packet provided

13 Running Example Capitalize Echo Client/Server  Client  User types line of text  Client program sends line to server  Server  Server receives line of text  Capitalizes all the letters  Sends modified line to client  Client  Receives line of text  Displays M2-Internet Java 13 H. Fauconnier

14 14 Sentence Capitalizer (Again)‏ Example: Java client (UDP)‏ import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS

15 15 Example: Java client (UDP), cont. DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } Create datagram with data-to-send, length, IP addr, port Send datagram to server Read datagram from server Create datagram to receive from server,

16 16 Sentence Capitalizer (Again)‏ Example: Java server (UDP)‏ import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket (9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram

17 17 Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } Get IP addr port #, of sender Write out datagram to socket End of while loop, loop back and wait for another datagram Create datagram to send to client

18 18 Summary Brief coverage of Java sockets - TCP/UDP Should be enough to get started Examples will be available as links on the main class page Will cover threads next First assignment will be a multi-threaded Web Server … Also, will practice client-server in the lab Do read references in RelatedLinks for more information

19 19 End References – http://docs.oracle.com/javase/tutorial/networ king/datagrams/index.html There is a new assignment …. Ping Program


Download ppt "1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright 1996-2007 Lecture 8a Application."

Similar presentations


Ads by Google