CS18000: Problem Solving and Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Advertisements

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
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.
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.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Client/Server In Java An Introduction to TCP/IP and Sockets.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick 1.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
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 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
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 ( 李德成 )
CSE 501N Fall ‘09 22: Introduction to Networking November Nick Leidenfrost.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Object-Orientated Analysis, Design and Programming
Echo Networking COMP
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
Lecture 21 Sockets 1 (Not in D&D) Date.
Networking Serialization
Networking COMP
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
Client-server Programming
NETWORK PROGRAMMING CNET 441
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Clients and Servers 19-Nov-18.
Basic Protocols 24-Nov-18.
Clients and Servers 1-Dec-18.
CS18000: Problem Solving and Object-Oriented Programming
Basic Protocols 19-Feb-19.
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Presentation transcript:

CS18000: Problem Solving and Object-Oriented Programming Welcome slide for display pre-bell.

External Communication Network Communication

Some (Simplified) Definitions Internet Protocol (IP): Identifies hosts (servers, workstations, laptops, etc.) with a unique address (e.g., 128.10.2.21) Domain Name System (DNS): Maps domain names (e.g., galahad.cs.purdue.edu) to IP addresses (e.g., 128.10.9.143) Transmission Control Protocol (TCP): Identifies ports on hosts for a network connection Socket: IP address plus TCP port Two sockets makes a network connection Digress for a minute on the meaning of “protocol”: from diplomatic terminology, a set of rules by which communication takes place.

Client-Server A Server is a process that waits for a connection A Client is a process that connects to a server At different times, a process may be both a client and a server Need not be associated with a specific computer: Any computer can have both client and server processes running on it Once connected, the client and server can both read and write data to one another asynchronously (“a bi-directional byte pipe”)

Use of Sockets Clients and Servers communicate via Sockets Socket: IP address plus TCP port Think: street name plus house number IP addresses Identifies a computer on the Internet Public addresses are globally unique Represented using dotted-decimal (byte) notation: 128.10.9.143 Some firewalls translate addresses to internal ones (e.g., PAL) Port number 0-65535 (16 bits) Low-valued port numbers are reserved for privileged processes

Objects and Networking in Java You know that Java objects can be written to and read from files Java objects can also be exchanged over network connections Uses ObjectOutputStream and ObjectInputStream Tricky bits… ObjectOutputStream generates a “header” of information that must be read Requires “flush” to ensure ObjectInputStream reader is not blocked

ObjectStream Client-Server Timeline Server Process Client Process Server starts: opens ServerSocket and blocks (waiting for client) Client starts: opens Socket to Server Server receives connection Server opens ObjectOutputStream, sends header with flush Client opens ObjectOutputStream, sends header with flush Server receives object stream header Client receives object stream header Client and Server exchange objects in agreed upon order

Java Networking Class: Socket Models a TCP/IP socket Used by Client to identify Server IP address (or DNS name) Port number new Socket("pc.cs.purdue.edu", 12190) Used by Server to identify connected Client Provides streams for communications: getOutputStream() getInputStream()

Java Networking Class: ServerSocket Used by Server to wait for a Client to connect Constructor specifies TCP port number to use: ServerSocket ss = new ServerSocket(4242); Method accept() blocks waiting for connection Socket socket = ss.accept();

Example: Object Server import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException, ClassNotFoundException { // create socket on agreed-upon port... ServerSocket serverSocket = new ServerSocket(4242); // wait for client to connect, get socket connection... Socket socket = serverSocket.accept(); // open output stream to client, flush send header, then input stream... ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.flush(); // ensure data is sent to the client ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); // send object(s) to client... String s1 = "hello there"; oos.writeObject(s1); System.out.printf("sent to client: %s\n", s1); // read object(s) from client... String s2 = (String) ois.readObject(); System.out.printf("received from client: %s\n", s2); // close streams... oos.close(); ois.close(); }

Example: Object Client import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException { // create socket on agreed upon port (and local host for this example)... Socket socket = new Socket("data.cs.purdue.edu", 4242); // open input stream first, gets header from server... ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); // open output stream second, send header to server... ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.flush(); // ensure data is sent to the server // read object(s) from server... String s1 = (String) ois.readObject(); System.out.printf("received from server: %s\n", s1); // write object(s) to server... String s2 = s1.toUpperCase(); oos.writeObject(s2); oos.flush(); // ensure data is sent to the server System.out.printf("sent to server: %s\n", s2); // close streams... oos.close(); ois.close(); }

Client-Server with Threads In many (most?) cases, a single server is connected to by multiple clients Server must be able to communicate with all clients simultaneously—no blocking Technique: server creates a separate thread to handle each client as it connects Client and server may also each create separate thread for reading and writing Use web browser, web server analogy for threading and non-blocking behavior.

Example: Echo Server Simple server that accepts connections from multiple clients Spawns a thread for each client Reads lines from the connection, logs information, echoes lines back Useful for debugging network and code

Echo Server Timeline Server Process Client Process Server starts: opens ServerSocket and blocks (waiting for client) Client 1 starts: opens Socket to Server Server receives connection Client 2 starts: opens Socket to Server Server creates thread Client 1 sends line Client 3 starts: opens Socket to Server Server thread 1 reads/echoes line Client 1 reads line

Example: Echo Server (1) import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class EchoServer implements Runnable { Socket socket; public EchoServer(Socket socket) { this.socket = socket; } // continued…

Example: Echo Server (2) // run method for thread... public void run() { System.out.printf("connection received from %s\n", socket); try { // socket open: make PrinterWriter and Scanner from it... PrintWriter pw = new PrintWriter(socket.getOutputStream()); Scanner in = new Scanner(socket.getInputStream()); // read from input, and echo output... while (in.hasNextLine()) { String line = in.nextLine(); System.out.printf("%s says: %s\n", socket, line); pw.printf("echo: %s\n", line); pw.flush(); } // input done, close connections... pw.close(); in.close(); } catch (IOException e) { e.printStackTrace();

Example: Echo Server (3) // main method... public static void main(String[] args) throws IOException { // allocate server socket at given port... ServerSocket serverSocket = new ServerSocket(4343); System.out.printf("socket open, waiting for connections on %s\n", serverSocket); // infinite server loop: accept connection, // spawn thread to handle... while (true) { Socket socket = serverSocket.accept(); EchoServer server = new EchoServer(socket); new Thread(server).start(); }

Network Communication in Java Uses standard file I/O classes: low-level, high-level, object, and text Adds abstractions to deal with network connections ServerSocket to wait for connections Socket abstracts a TCP socket (IP address + port) Uses threads to improve responsiveness and avoid blocking