CSCD 330 Network Programming Winter 2019

Similar documents
CSCD 330 Network Programming Spring 2018

CPSC 441 UDP Socket Programming. Department of Computer Science University of Calgary

Communication in Distributed Systems: Sockets Programming. Operating Systems

Chapter 2 Application Layer

Introduction to Sockets 9/25/14

CPSC 441 Tutorial - 11 UDP Socket Programming Department of Computer Science University of Calgary

Chapter 2: outline. 2.1 principles of network applications. 2.6 P2P applications 2.7 socket programming with UDP and TCP

Chapter 2 Application Layer

Chapter 2 outline. 2.1 Principles of app layer protocols

TCP and UDP Socket Programming in JAVA TCP Socket Programming 1. Write code for Client and save in GreetingClient.java

Unit 1 Java Networking

Computer Networking Introduction

Lecture 3. Java Socket Programming. TCP, UDP and URL

Layer 4 - Transport Layer. What do we need a Transport Layer for? Transport Protocols in the TCP/IP Reference Model. The Transport Layer TCP and UDP

Layer 4 - Transport Layer

Lecture 05: Application Layer (Part 02) FTP, Peer-to-Peer, . Dr. Anis Koubaa

CSCD 330 Network Programming Spring 2018

Process Communication COMPUTER NETWORKING Part 2

Computer Networks Unit II Transport layer (2012 pattern)

Computer Networks. 2.Application Layer. László Böszörményi Computer Networks Application Layer - 1

JAVA SOCKET PROGRAMMING

SOURCE CODE. //Socket Programming using TCP Sockets. Server.java

Part 2: Application Layer

CS 355. Computer Networking. Wei Lu, Ph.D., P.Eng.

Chapter 2: Application Layer last updated 22/09/03

Πρωτόκολλα Διαδικτύου (ΨΣ-326 DS151)

UDP Sockets TCP guarantees the delivery of packets and preserves their order on destination. Sometimes these features are not required, since they do

SE322 Software Design and Architecture

JAVA Network API. 2 - Connection-Oriented vs. Connectionless Communication

CSE 461 Module 11. Connections

CPSC 441 Assignment-3 Discussion. Department of Computer Science University of Calgary

Lecture 3: Socket Programming

Chapter 2 Application Layer

Chapter 2 APPLICATIONS

Networking Basics. network communication.

Chapter 2 Application Layer

Chapter 2 Application Layer

Computer Networks 1 (Mạng Máy Tính 1) Lectured by: Dr. Phạm Trần Vũ

Chapter 2 Application Layer

Chapter 2 Application Layer

Chapter 2 Application Layer

Chapter 2 Application Layer

COMP 211 Chapter 2 Application Layer

COMP 3331/9331: Computer Networks and Applications

Computer Communication Networks Socket Programming

Chapter 2 Application Layer. 2: Application Layer 1

Chapter 2 Application Layer

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Telematics Chapter 8: Transport Layer

Chapter 2 Application Layer

Application Programming Interfaces

CPSC 441 Assignment-3 Discussion. Department of Computer Science University of Calgary

A note on the use of these ppt slides:

Chapter 2 Application Layer

Chapter 2 Application Layer

Principles, Models and Applications for Distributed Systems M

Advanced Java Programming. Networking

Chapter 2: Application layer. Chapter 2 Application Layer. Some network apps. Chapter 2: Application Layer. Chapter 2: Application layer

Chapter 2: Application layer. Chapter 2 Application Layer. Chapter 2: Application Layer. Some network apps

We will cover in this order: 2.1, 2.7, 2.5, 2.4, 2.2

Data Communications & Networks. Session 2 Main Theme Application Layer. Dr. Jean-Claude Franchitti

Data Communications & Networks. Session 2 Main Theme Application Layer. Dr. Jean-Claude Franchitti

Chapter 2: Application layer. Chapter 2 Application Layer. Chapter 2: Application Layer. Some network apps

Chapter II: Application Layer

Chapter 2 Application Layer

COMPUTER NETWORKS CHAP 2 : APPLICATION LAYER

Java Networking (sockets)

Transport layer protocols. Lecture 15: Operating Systems and Networks Behzad Bordbar

Tommy Färnqvist, IDA, Linköping University

CSCD 330 Network Programming

Computer Networks. Application Layer

CSCI Computer Networks Spring 2017

Chapter 2 Application Layer. 2: Application Layer 1

55:134/22C:178 Computer Communications Lecture Note Set 2 Summer 2004

Client/Server Computing & Socket Programming

Distributed Systems. 3. Access to the Transport Layer. Werner Nutt

Tonight. Communication. Middleware Protocols. Remote Procedure Call (RPC) Several common communication mechanisms RPC RMI Sockets Next week: HTTP,

CSCD 330 Network Programming

Interprocess Communication

Chapter 2: Application layer. Computer Networks. Application Layer. Chapter 2: Application Layer. Some network apps. Bu-Ali Sina University, Hamedan

Networking and Security

Chapter 2 Applications and

Basic Java Network Programming. CS211 July 30 th, 2001

Chapter 2 APPLICATIONS

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Chapter 9: UDP sockets & address conversion function

CSCD 330 Network Programming

protocols September 15,

Java Socket Workshop. July Purpose of this workshop:

Assignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command.

Chapter 2a SOCKET PROGRAMMING

TCP and Concurrency. The third assignment at DA

Java Socket Application. Distributed Systems IT332

CSCD 330 Network Programming

Networking: IPv6, UDP and TCP. Network Programming in Java UDP and TCP

Construction d Applications Réparties / Master MIAGE

Software Practice 1 - Socket

CS September 2017

Communication Paradigms

Java Support for developing TCP Network Based Programs

Transcription:

CSCD 330 Network Programming Winter 2019 Lecture 7 Application Layer Socket Programming in Java Reading: Chapter 2, Java links Relevant Links page Some Material in these slides from J.F Kurose and K.W. Ross All material copyright 1996-2007

Review Client/Server Programming Firefox HTTP 80 So far, Host has IP Address 146.187.134.22 Network Layer identifier Every network device has this identifier Phone, Toaster, etc. Processes running on Hosts Assigned a port number Port numbers identifiers for processes Some port numbers reserved 1-1023 Other port numbers reserved for widely recognized processes

Review Client/Server Programming Communication Between Client/Server Uses object, Socket Socket is the API between a program and the TCP/IP stack of the OS It has an input stream and an output stream built in to it Both the client and server define different ends of this socket Link to the Java.net Package API https://docs.oracle.com/javase/10/docs/api/java/net/package-summary.html

TCP/IP Client/Server How this works in Java Server Socket 1. Binds socket to specific port number 2. Listens for incoming connections on that port 3. When connection attempted, it accepts connection, creates a regular socket for communication to client 4. Port number on the client side is different and selected by stack software 5. What you see on the Server side is the same port number for the server program

TCP/IP Client/Server Java Code for Server Server socket listens on a port ss = new ServerSocket (port); // Loop forever While (true) { // Get a connection Socket newsocket = ss.accept (); // Deal with the connection } //... Inside loop waits for connection Creates a new socket object representing new connection What is not obvious is that the new connection is through a different port number on the client side

TCP/IP Client/Server Java code for Client, Send/Receive Data // Create a socket for communicating with server Socket clientsocket = new Socket ("hostname", 6789); Create a client TCP socket, with host and port // Create data streams for communicating through the socket BufferedReader in = new BufferedReader (new InputStreamReader (clientsocket.getinputstream ()); PrintWriter out = new PrintWriter (clientsocket.getoutputstream ()); System.out.println (in.readline ()); // Print output to screen Then, create streams to send input and get it back from server

User Datagram Protocol (UDP)

UDP Introduction User Datagram Protocol (UDP) is a communication protocol that transmits independent packets over network with no guarantee of arrival and no guarantee of order of delivery Most communication over Internet takes place over Transmission Control Protocol (TCP) why do you think that is true?

UDP Advantages Advantage of UDP is that it requires much less overhead than TCP because No hand-shaking, No retry if an acknowledge isn't received, No buffering and numbering of packets, Where do we use UDP? Connectionless protocols used either for onepacket messages for which delivery is not crucial Responses to time requests, or To reduce the transmission overhead for timecritical data such as streaming audio/video

UDP Building UDP applications is very similar to building a TCP system Difference is that we don t establish a point to point connection between a client and a server The setup is very straightforward too. Java ships with built-in networking support for UDP which is part of the java.net package. To use UDP, we only need to import classes from the java.net package: java.net.datagramsocket and java.net.datagrampacket.

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

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

UDP Summary Client No connection setup no pipe Server More Differences with TCP: 1. Each batch of bytes sent with attached address information 2. No special ServerSocket class in java

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

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!!!!

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 public DatagramPacket(byte[ ] ibuff, int ilength); ibuf is the byte array into which the data portion of the datagram will be copied. ilength is the number of bytes to copy from the datagram into the array receiving the data

Java Classes for UDP 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 public DatagramPacket (byte[] ibuf, int length, InetAddress iaddr, int iport); ibuf is the array of bytes that stores data of the message, length is length of byte array being sent via this datagram iaddr stores the IP address of recipient. port identifies port datagram should be sent to on the receiving host.

Java Classes for UDP DatgramSocket represents connectionless socket It provides three constructors, 1. Programmer can specify a port OR 2. Allow system to randomly use a port 3. System can also select a specific IP address public DatagramSocket() throws IOException public DatagramSocket(int port) throws IOException public DatagramSocket(int port, InetAddress localaddr) throws IOException

Java Classes for UDP public DatagramSocket() throws IOException First constructor allows you to create a socket at an unused ephemeral port, generally used for client applications Second constructor allows you to specify a port, which is useful for server applications public DatagramSocket(int port) throws IOException

Java Classes for UDP public DatagramSocket(int port, InetAddress localaddr) throws IOException Final constructor is useful for machines with multiple IP interfaces. You can use this constructor to send and listen for datagrams from one of the IP addresses assigned to the machine

Java Classes for UDP DatgramSocket represents connectionless socket 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

UDP Example Program Sentence Capitalizer

UDP Sentence Capitalizer Client 1. Read string from keyboard, convert to bytes 2. Create DatagramSocket for communicating to UDP Server 3. Create DatagramPacket with string to send in bytes, length, IPAddress server, port server 4. Send DatagramPacket through Socket 5. Create DatagramPacket to receive reply from Server 6. Receive reply from Server, print Capitalized string

UDP Sentence Capitalizer Server 1. Create socket for server with port 2. Create DatagramPacket to receive sentence from client 3. Wait to receive from client 4. Convert bytes from client to sentence 5. Get IPAddr and port from received packet 6. Capitalize the sentence, convert to bytes 7. Create send DatagramPacket to send back to Client, with IPAddr and port 8. Send packet to client

Sentence Capitalizer (Again) Example: Java client (UDP) Create input stream Create client socket import java.io.*; import java.net.*; class UDPClient { public static void main(string args[]) throws Exception { Keyboard BufferedReader infromuser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientsocket = new DatagramSocket(); Translate hostname to IP address using DNS InetAddress IPAddress = InetAddress.getByName("localhost"); byte[] senddata = new byte[1024]; byte[] receivedata = new byte[1024]; String sentence = infromuser.readline(); senddata = sentence.getbytes(); Read from keyboard

Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port Send datagram to server Read datagram from server DatagramPacket sendpacket = new DatagramPacket(sendData, senddata.length, IPAddress, 9876); clientsocket.send(sendpacket); DatagramPacket receivepacket = new DatagramPacket(receiveData, receivedata.length); clientsocket.receive(receivepacket); Send thru socket Recve thru socket String modifiedsentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedsentence); clientsocket.close(); } }

Sentence Capitalizer (Again) Example: Java server (UDP) import java.io.*; import java.net.*; class UDPServer { public static void main(string args[]) throws Exception Create { datagram socket at port 9876 DatagramSocket serversocket = new DatagramSocket(9876); Create space for receive/send datagrams Receive datagram byte[] receivedata = new byte[1024]; byte[] senddata = new byte[1024]; while(true) { DatagramPacket receivepacket = new DatagramPacket(receiveData, receivedata.length); serversocket.receive(receivepacket); Create receive Datagram

Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); Get IP addr port #, of sender InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.getport(); String capitalizedsentence = sentence.touppercase(); Create datagram to send to client Write out datagram to socket } } } senddata = capitalizedsentence.getbytes(); DatagramPacket sendpacket = new DatagramPacket(sendData, senddata.length, IPAddress, port); serversocket.send(sendpacket); End of while loop, loop back and wait for another datagram

More Java.net Package Always good to see the documentation on the Classes and methods Look up DatagramSocket and DatagramPacket https://docs.oracle.com/javase/10/docs/api/java/net/packagesummary.html

Summary Brief coverage of Java sockets - TCP/UDP Should be enough to get started Examples available as links on the main class page Also, practice client-server in next lab Read references in RelatedLinks for tutorials and more information on Java Client Server

I put up the next assignment.. HW 3 Practice HTTP