Clients and Servers 13-Sep-19.

Slides:



Advertisements
Similar presentations
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
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.
COEN 445 Communication Networks and Protocols Lab 4
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
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.
28-Jun-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
DBI Representation and Management of Data on the Internet.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
CS 11 java track: lecture 6 This week: networking basics Sockets Vectors parsing strings.
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.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
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
Threads in Java Two ways to start a thread
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
I/O Basics.
Sockets and URLs 17-Sep-18.
Sockets and URLs 13-Nov-18.
Clients and Servers 19-Nov-18.
Basic Protocols 24-Nov-18.
URL in Java C343 Lab (Week 13).
Hypertext Transfer Protocol
Clients and Servers 1-Dec-18.
Sockets and URLs 3-Dec-18.
Hypertext Transfer Protocol
Basic Protocols 19-Feb-19.
CSCD 330 Network Programming
Hypertext Transfer Protocol
Uniform Resource Locators
Threads.
Hypertext Transfer Protocol
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

Clients and Servers 13-Sep-19

URL review A URL has the syntax: protocol://hostname:port/path#anchor import java.net.*; This is the package that defines sockets, URLs, etc. URL url = new URL(String); Constructs a URL object from a text string MalformedURLException This exception is thrown if the given String cannot be parsed by newURL(String) We have used URLs to display a page in an applet: appletContext.showUrl(URL)

HTTP review HTTP is a protocol--a formal description of a language that computers use to communicate An HTTP message consists of three parts: The request or the response line A request line typically contains either GET or PUT A response line contains the status code, such as 404 Not Found A header section Contains name-value pairs, such as Content-type: text/html Ends with a blank line The body of the message The body is optional

Using a URL URLConnection c = url.openConnection(); The URLConnection is the basic way to access the resource information c.getHeaderField(name) Returns the value of the named header field (as a String) Frequently used fields have shorthand methods, for example, c.getLastModified() = c.getHeaderField("last-modified") getHeaderField(int) Returns the value of the int-th header field (as a String) The 0-th header field is the status line c.getInputStream() Returns an InputStream containing the “content” of the resource url.openStream() is shorthand for url.openConnection().getInputStream()

Socket review A socket is a low-level software device for connecting two programs (possibly on different computers) together new Socket(String host, int port) Creates a client socket and makes the connection Methods include getInputStream(), getOutputStream(), and close() new ServerSocket(int port) Creates a server socket that listens on the specified port accept() returns a Socket that can be used for I/O accept() is a blocking method, so multithreading is highly desirable

How to write a server ServerSocket server = new ServerSocket(port) The port should be a number above 1024 Socket client = server.accept(); accept() blocks while it waits for a connection InputStream inStream = client.getInputStream(); InputStreamReader reader = new InputStreamReader(inStream); BufferedReader input = new BufferedReader(reader); char ch = input.read(), String s = input.readLine() OutputStream outStream = client.getOutputStream(); PrintWriter output = new PrintWriter(outStream, true); true is so that you auto-flush, that is, don’t fill the buffer output .print(X), output .println(X), output .println() input.close(), output.close(), server.close(), client.close()

How to write a client Socket server = new Socket(ip_address, port) The ip_address can be the String "localhost" This method makes the actual connection InputStream inStream = server.getInputStream(); As on the previous slide OutputStream outStream = server.getOutputStream(); input.close(), output.close(), server.close()

How to write an HTTP server An HTTP server is just a server that follows the HTTP protocol (request/status line, header, blank line, body) Since HTTP is a text-based protocol, compliance is easy There are two versions of HTTP: 1.0 and 1.1 HTTP 1.0 is simpler and should be used if the special features of 1.1 are not required The most important change in HTTP 1.1 is that it can accommodate proxy servers The client and server must agree which version of HTTP is being used Most HTTP servers can use both

Proxy servers Proxies are important because they allow more than one server to use the same IP address There aren’t enough IP addresses to go around If you have a lot of clients, you need a lot of servers--but the user should not have to try multiple IP addresses client proxy has IP address server With a proxy client server has IP address Without a proxy

Multithreading server.accept() is a blocking call--Java stops and waits for a response before it continues This is only acceptable if the server never has more than one client A server needs to have a separate thread for each client There are two ways to create a Thread: Write a class that extends Thread Override the public void run() method Create an instance of your class and call its (inherited) start() method Write a class that implements Runnable Implement the public void run() method Create an instance of your class Create a Thread object with this instance as a parameter to the constructor Call the Thread object’s start() method

Synchronization While an object is being modified by one thread, no other thread should try to access it This leads to unpredictable (and difficult to debug) results You can synchronize an object: synchronized (obj) { code that uses/modifies obj } synchronized is a statement type, like if or while No other code that is synchronized on this object can use or modify the object at the same time You can synchronize a method: synchronized void addOne(arg1, arg2, ...) { code } synchronized is a method modifier, like public or abstract Only one synchronized method in a class can be used at a time (but this doesn’t restrict other, non-synchronized methods) Synchronization can really hurt efficiency (and response time) It can be very difficult to make a program both safe and efficient

A synchronization analogy Imagine that you have a building with two entrances One entrance is always kept locked, and has a single key The other entrance is never locked Synchronization is like this Code that always uses the locked (synchronized) entrance has to wait for other code to exit and hand over the key Any code that uses the unlocked entrance can go into the building at any time, regardless of what other code may be there Thus, synchronizing code only protects you from other synchronized code!

The End