Object-Orientated Analysis, Design and Programming

Slides:



Advertisements
Similar presentations
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Advertisements

Socket Programming.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
1 Java Networking – Part I CS , Spring 2008/9.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
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.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
Julia Ljunbjörk and Anita Mugenyi. What is a socket? Like a house Between the layers.
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 in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
CS 3830 Day 11 Introduction : Application Layer 2 Server-client vs. P2P: example Client upload rate = u, F/u = 1 hour, u s = 10u, d min ≥ u s.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5 th edition. Jim Kurose, Keith Ross Addison-Wesley, April A note on the use.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
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.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
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.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
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.
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
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.
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
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.
Distributed Systems1 Socket API  The socket API is an Interprocess Communication (IPC) programming interface originally provided as part of the Berkeley.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
MCA – 405 Elective –I (A) Java Programming & Technology
CS 1652 Jack Lange University of Pittsburgh
The Socket API 9/17/2018.
Socket programming with TCP
CSCD 330 Network Programming
Clients and Servers 19-Nov-18.
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Clients and Servers 1-Dec-18.
CSCD 330 Network Programming
Socket Programming.
CSCD 330 Network Programming
NETWORK PROGRAMMING CNET 441
Socket Programming 2: Application Layer.
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Presentation transcript:

Object-Orientated Analysis, Design and Programming Presentation by Dr. Phil Legg Senior Lecturer Computer Science 10: Sockets and Datagrams Autumn 2016

Network Applications Network application consists of client program and server program (usually) on different hosts. The client process and the server process communicate with each other through their sockets. See the Oracle Tutorial for more details

Socket as an Interface Socket: a door between application process and end-end-transport protocol (UDP or TCP)

Stream mode sockets and datagram sockets Sockets are the interface (“doors”) between the application process and the transport layer. A socket programming construct can use either the UDP or TCP protocol. Sockets that use UDP for transport are known as datagram sockets, while sockets that use TCP are termed stream sockets or just socket.

Stream mode socket (using TCP) TCP provides reliable, in-order transfer of bytes ("pipe") between client and server. A pair of (data) sockets in client and server are connected by a pair of streams, one in each direction. Each socket has an input stream and an output stream. To send information from one process to the other, the sending process writes the information to its output stream, and the other process reads from its input stream.

Stream mode socket (using TCP)

Client and server interaction using TCP socket Server must be running server process must first be running server must have created server socket that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying address, and port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client

Java stream-mode Socket API In Java, the stream-mode socket API is provided with two classes: ServerSocket: used on the server for accepting connection requests; we will call an object of this class a connection socket. Socket: used on both processes for data exchange; we will call an object of this class a data socket.

Key methods in the ServerSocket class ServerSocket(int port) constructor creating an instance of the server socket (i.e. the “welcome socket” or “listening socket”) Socket accept() listens for a connection request and accepts it and returns an instance of a socket. A blocking operation. void close() closes this server socket

Key methods in the Socket class Socket(String addr, int port) constructor creates a socket and connects to addr and port void close() closes the socket InputStream getInputStream() returns an input stream so that data may be read from the socket. A read operation on the InputStream is blocking. OutputStream getOutputStream() returns an output stream so that data may be written to the socket. A write operation on the OutputStream is nonblocking.

Client/Server program structure

Client/Server in Java (1) Server creates listening socket ServerSocket welcomeSocket = new ServerSocket(6789); Server waits for income connection request Socket serverDataSocket = welcomeSocket.accept(); Client requests a connection to the server Socket clientDataSocket = new Socket("hostname", 6789);

Client/Server in Java (2) Set up InputStream from a socket BufferedReader socketInput = new BufferedReader( new InputStreamReader( clientDataSocket.getInputStream())); Receive message from the other process via socket Inputstream String message = socketInput.readLine();

Client/Server in Java (3) Set up OutputStream from a socket PrintWriter socketOutput = new PrintWriter( new OutputStreamWriter( clientDataSocket.getOutputStream()); Send message to the other process via socket Outputstream socketOutput.println(message);

Templates for Acceptor (Server) and Requestor (Client)

In-Class Activity Develop two simple programs (a Client and a Server). Use the templates from the previous slide as a guide. The client should be able to connect to the server, and send a message. The server should receive the message, reverse this, and return it to the client. // To reverse a String s, you may use the following line String result = new StringBuffer(s).reverse().toString();

Server - key concepts int portNo = 12345; String message = "Hello from server"; ServerSocket connectionSocket = new ServerSocket(portNo); Socket dataSocket = connectionSocket.accept(); OutputStream outStream = dataSocket.getOutputStream(); PrintWriter socketOutput = new PrintWriter(outStream); socketOutput.println(message); dataSocket.close( ); connectionSocket.close( );

Client - key concepts String acceptorHost =“localhost"; int acceptorPort = 12345; Socket mySocket = new Socket(acceptorHost,acceptorPort); InputStream inStream = mySocket.getInputStream(); BufferedReader socketInput = new BufferedReader( new InputStreamReader(inStream)); String message = socketInput.readLine( ); mySocket.close( );

Acceptor and Request Example First run ‘Acceptor’ to initialize server Then, if we run ‘Receptor’ as a client, it will receive the message from the server http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week10/Acceptor.java http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week10/Receptor.java

In-Class Activity Extend the Server to accept multiple Clients. You may want a loop that can establish a connection and then wait for new connections again

In-Class Activity Develop a chat system that could be used for the UWEfilm online booking system

Datagrams A socket programming construct can use either the UDP or TCP protocol. Stream sockets for TCP and Datagram sockets for UDP. The datagram socket API supports the exchange of discrete units of data Datagram sockets usually are used to support connectionless communication No need to establish connection But each time we need to include the receiver’s address

Java Datagram Socket API DatagramSocket class for the sockets. Sending and receiving DatagramPacket class for the datagram exchanged. Receiver’s address Data

DatagramSocket and DatagramPacket

DatagramSocket DatagramSocket() used if no need to receive datagram DatagramSocket(int port) used if need to receive packet. The port number can then be specified in the datagram packet sent by the other process. void send(DatagramPacket p) to send the datagram packet p void receive(DatagramPacket p) object referred to by p will hold the packet void setSoTimeout(int timeout) Sets a timeout period for the blocking receive operations

DatagramPacket DatagramPacket(byte[]buf, int l) used for received packet packet length upto l; data will be stored in the array buf DatagramPacket(byte[]buf,int l, InetAddress rcvAddr, int port) used for packet to be sent; buf contains the data, rcvAddr and port identifies the receiver.

Sending a Datagram in Java The sender process: creates a DatagramPacket object that represents the datagram itself. This object carries the payload data as a reference to a byte array, the destination address (the host ID and port number to which the receiver’s socket is bound). Creates a DatagramSocket, and issues a call to the send method in the DatagramSocket object, specifying a reference to the DatagramPacket object as an argument

Receiving a Datagram in Java In the receiving process, a DatagramSocket object must also be instantiated and bound to a local port. To receive datagrams sent to the socket, the process creates a DatagramPacket object which references a byte array Calls the receive method in the DatagramSocket object, specifying as argument a reference to the DatagramPacket object.

Example – Sending ‘hello’ Sender – version 1 Create a datagram socket Put the message “Hello!” in a byte array Create a datagram packet with the receiver’s address and port number, and a reference to the byte array Send datagram in the packet through the datagram socket Receiver – version 1 Create a datagram socket bound to a port (e.g.12345) Create a datagram packet includes a reference to a byte array Receive a data packet through the datagram socket

Code Examples http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week10/DGSender.java http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week10/DGReceiver.java

Working with Datagram After receiving a datagram packet, datagram.getAddress(); //returns address of datagram packet sender datagram.getPort(); //returns port of sender Issues with Datagrams Sockets normally provide non-blocking send and blocking receive The method receive blocks until a datagram packet is received as seen in Receiver, unless it sets a time out period. To avoid infinitely blocked, use timeouts Size of the buffer for holding the data

In-Class Activity Extend the hello example so that receive can reply to the sender You will need to get the sender’s address and port number from the datagram, and then construct a new datagram and the original sender’s address. The sender will need to be modified to accept a response back from the receiver.

Summary Working with datagram (UDP) sockets and stream (TCP) sockets Client / server interactions Working with client and server interactions in Java