Practicum: - Client-Server Computing in Java 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.

Slides:



Advertisements
Similar presentations
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Advertisements

Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Network Programming Chapter 11 Lecture 6. Networks.
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.
Socket Programming.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
System Programming Practical session 10 Java sockets.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
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.
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
Chapter 26 Client Server Interaction Communication across a computer network requires a pair of application programs to cooperate. One application on one.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
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.
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.
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.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
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.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Socket programming. Socket programming with TCP.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
CS390- Unix Programming Environment CS 390 Unix Programming Environment 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 Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Peter Lee April 8, 2004.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Part 4: Network Applications Client-server interaction, example applications.
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.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
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.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
1 Network Communications A Brief Introduction. 2 Network Communications.
Network Programming. These days almost all devices.
Object-Orientated Analysis, Design and Programming
„Networking”.
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Socket Programming.
Socket Programming 2: Application Layer.
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004

2 Reminders  HW7 is out!  due on Wednesday, April 28, 11:59pm  Read:  Chapter 10

Intro to Distributed Computing Concepts

4 Distributed computing  Many applications involve coordinated computation by multiple host computers World-Wide Web Internet Chess Club Andrew File System services X Windows …

5 The client-server paradigm  Almost all modern distributed computing applications are organized around the client-server paradigm  Client  initiates communication  sends requests and receives responses  interacts with one (or a small number) of servers at a time  Server  waits and listens for incoming requests  receives requests and sends responses  is “always” running  interacts with many clients concurrently

6 Network communication  Network communication is organized into layers  hardware layer  network interface device, connected to a local area network, which in turn is connected to a (packet switched) Internet  protocol layer(s)  basic data transport mechanisms, providing addressing (eg, IP addresses), fragmention/reassembly, reliable transmission  application layer  client and server functionality  Unconcerned with data movement across network

7 Network layers Application Protocol Hardware Application Protocol Hardware network protocol data flow

8 Network layering  Decomposes network communication into simpler tasks.  Each layer provides information hiding from layers above and below.  The data flow is from the application level down the hierarchy, across the network, and back up the hierarchy to the peer application.  Appears as though each layer communicates with its peer at the same layer.

9 Hardware layer  Typically Ethernet-based  Data is transmitted in small packets  typically less than 1500 bytes  packets are easily lost, and often arrive in unpredictable order  Each packet contains routing information  A network device watches for packets that are addressed to itself, ignores the rest  Routers look for packets that are not addressed to local hosts, and forwards them to a non-local network router

10 Protocol layer  There are two main protocols used  both provide Internet addressing/routing  TCP  Transmission Control Protocol  connection (“session”) oriented  provides long data messages (via fragmentation and reassembly of packets)  provides reliable communication  UDP  Unreliable Datagram Protocol  not connection oriented  no transmission guarantees, but very lightweight and efficient

11 Application layer  The hardware and protocol layers are studied in  Here, we will focus on the application level  Most networking applications use a particular data structure, the socket  A socket provides a high-level abstract interface to a lower-level network protocol service  In Java, sockets are similar in some respects to input/output streams

12 Network layers Application Protocol Hardware Application Protocol Hardware network protocol data flow

13 Sockets  Sockets have a long history in network and operating system design  first appeared in BSD 4.1 Unix in 1981  Socket characteristics  applications explicitly create, use, and destroy sockets  distinction between client sockets and server sockets  different kinds of sockets, depending on the transport protocol (TCP vs UDP)

14 Socket communication process write read socket process read write socket network Host Sockets provide a bi-directional communication channel from one process to another. Messages are queued at sending socket until transmitted across the network and at receiving socket until received by the receiving process.

15 Sockets in Java  Java provides very good support for sockets in the java.net.* package  java.net.Socket  create: constructor methods (to create sockets)  I/O: getOutputStream(), getInputStream()  destroy: close()  java.net.ServerSocket  create: constructor methods  wait and listen: accept()  destroy: close()

16 Socket programming with TCP  Client must contact the server  server must first be waiting and listening  server must thus have created a socket that accepts client connection request  Client contacts server by:  creating its own client TCP socket  uses IP address and port number of server

17 Socket programming, cont’d  When client creates its socket, a TCP session with the server’s TCP is established  On the server side:  when contacted by the client, the server TCP creates a new socket for communication with the client  thus, each client session gets its own private socket on the server

18 Client-server interaction listenSocket = ServerSocket(port) connectionSocket = listenSocket.accept() read request(s) from connectionSocket write reply(s) to connectionSocket connectionSocket.close() clientSocket = Socket(hostid,port) send request(s) to clientSocket read reply(s) from clientSocket clientSocket.close() Server Client TCP session

19 Client Steps 1.Open a socket. 2.Create input and output streams. 3.Send request(s) and receive reply(s) according to the server application protocol. 4.Close streams and socket.

20 Server steps 1.Create a server socket to listen for connection requests. 2.Open a socket to communicate with client. 3.Open input and output streams. 4.Accept requests and send replies. 5.Close streams and socket.

21 Open Socket  Server side: ServerSocket listenSocket = new ServerSocket(portNumber); while (true) { Socket connectionSocket = listenSocket.accept(); … }  Client side: Socket clientSocket = new Socket(hostId, portNumber); Same port number

22 Create I/O streams  Client and server: E.g., character streams BufferedReader in = new BufferedReader( new InputStreamReader( theSocket.getInputStream())); PrintWriter out = new PrintWriter( theSocket.getOutputStream(), true); Buffering wrapper for efficiency Automatically flush with println

23 Client-Server Communication  Client side: out.println(request); reply = in.readLine();  Server side: request = in.readLine(); … reply = … out.println(reply);

24 Cleanup  Client and Server: out.close(); in.close(); theSocket.close();

Example: A Pig Latin Server

26 Client-server interaction listenSocket = ServerSocket(port) connectionSocket = listenSocket.accept() read request(s) from connectionSocket write reply(s) to connectionSocket connectionSocket.close() clientSocket = Socket(hostid,port) send request(s) to clientSocket read reply(s) from clientSocket clientSocket.close() Server Client TCP session

27 Example: Java client import java.io.*; import java.net.* public class Client { public static void main (String argv[]) throws Exception { BufferedReader user = new BufferedReader(…); Socket clientSocket = new Socket(“foo.cs.cmu.edu”, 6789); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(),true); … create user input stream connect to the server create socket output stream

28 Java client, cont’d … BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String sentence = user.readLine(); out.println(sentence); String pigLatin = in.readLine(); System.out.println(“Server says:” + pigLatin); in.close(); out.close(); clientSocket.close(); create socket input stream send request to the server read reply from server release the connection

29 Example: Java server import java.io.*; import java.net.* public class Server { public static void main (String argv[]) throws Exception { ServerSocket listenSocket = new ServerSocket(6789); while (true) { Socket connectionSocket = listenSocket.accept(); BufferedReader in = new BufferedReader( new InputStreamReader( connectionSocket.getInputStream())); create listening socket wait for client contact create socket input stream on host foo.cs.cmu.edu:

30 Java server, cont’d PrintWriter out = new PrintWriter( connectionSocket.getOutputStream(),true); clientSentence = in.readLine(); String pigLatin = pigTranslate(clientSentence); out.println(pigLatin); in.close(); out.close(); connectionSocket.close(); } create socket output stream read request from client service the request send reply to client end of while loop; go back and wait for another request

31 Practical issue: cleaning up  Closing connections  It is important to close connections  usually a strict limit on the number of open connections  This means it is very important to handle exceptions, in case the socket creation or I/O fail  exception handler should close any open connections

32 Exception handling … try { Socket clientSocket = new Socket(“foo.cs.cmu.edu”, 6789); … } catch (UnknownHostException e) { System.err.println( “Couldn’t find the server host!”); } catch (IOException e) { System.err.println( “I/O error!”); } finally { try { if (clientSocket != null) { out.close(); in.close(); clientSocket.close(); } } catch (IOException e) { … } } this is always executed, no matter what

33 Practical issue: concurrency  A TCP server usually must be designed to handle multiple clients concurrently  This means that the server should be set up so that the multiple copies of the main server loop can be running at the same time  Java provides a mechanism for such separate “threads” of control, in java.lang.Thread

34 Example: multithreaded server import java.io.*; import java.net.* public class Server extends Thread { ServerSocket listenSocket; public Server () { try { listenSocket = new ServerSocket(6789); } catch (IOException e) { … } this.start(); } … create listening socket start main server loop thread for it on host foo.cs.cmu.edu:

35 Multithreaded server, cont’d … public void run() { try { while (true) { Socket connectionSocket = listenSocket.accept(); Connection c = Connection(connectionSocket); … run() is invoked by start() accept a connection create a new thread to serve client