Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 10: Networking using Socket Programming. Client-Server Model b The term server applies to any program that offers a service that can be reached.
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.
1 Java Networking – Part I CS , Spring 2008/9.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
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 Java Networking CS , Spring 2008/9. 2 Today’s Menu Networking Basics  TCP, UDP, Ports, DNS, Client-Server Model TCP/IP in Java Sockets URL 
1 Networking with Java 2: The Server Side. 2 Some Terms Mentioned Last Week TCP -Relatively slow but enables reliable byte-stream transmission UDP -Fast.
System Programming Practical session 10 Java sockets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
COMP1681 / SE15 Introduction to Programming
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
1 Java Networking – part I CS , Winter 2007/8.
1 Network Layers Application Transport Network Data-Link Physical bits.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
2: Application Layer 1 Socket Programming TCP and UDP.
1 Java Networking CS , Spring Today’s Menu Networking Basics  TCP, UDP, Ports, DNS, Client-Server Model TCP/IP in Java Sockets URL  The.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Ryerson University CPS Distributing Computing with Java.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
DBI Representation and Management of Data on the Internet.
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.
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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
NETWORK PROGRAMMING.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols,
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.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
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.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
URL Programming Mimi Opkins CECS277. What is a URL?  URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Working with URLs CSIE, National University of Tainan.
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:
Java 13. Networking public class SumTest {
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
Network Programming Introduction
Networking with Java 2.
Network Programming Introduction
„Networking”.
Chapter 2: Application layer
Presentation transcript:

Lecture 9 Network programming

Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. Sample structure of a URL. The resource name part may contain: host name, file name, port number(optional) and reference (optional) Sample structure of a URL. The resource name part may contain: host name, file name, port number(optional) and reference (optional) you can create a URL object in Java from an absolute URL or a relative URL. you can create a URL object in Java from an absolute URL or a relative URL. The URL class provides several methods implemented on URL objects. You can get the protocol, host name, port number, and filename from a URL. The URL class provides several methods implemented on URL objects. You can get the protocol, host name, port number, and filename from a URL. Protocol Identifier Resource Name

Example code import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception { URL aURL = new URL(" + "tutorial/index.html#DOWNLOADING"); URL aURL = new URL(" + "tutorial/index.html#DOWNLOADING"); System.out.println("protocol = " + System.out.println("protocol = " + aURL.getProtocol()); System.out.println("host = " + aURL.getProtocol()); System.out.println("host = " + aURL.getHost()); System.out.println("filename = " + aURL.getHost()); System.out.println("filename = " + aURL.getFile()); System.out.println("port = " + aURL.getFile()); System.out.println("port = " + aURL.getPort()); System.out.println("ref = " + aURL.getPort()); System.out.println("ref = " + aURL.getRef()); aURL.getRef()); }} Output of the above code: protocol = http host = java.sun.com filename = /docs/books/tutorial/index.html port = 80 ref = DOWNLOADING

Connecting with a URL (1) openStream() : returns a java.io.InputStream object, from which you can read easily as reading from an input stream. It may throw an IOException openStream() : returns a java.io.InputStream object, from which you can read easily as reading from an input stream. It may throw an IOException Example code import java.net.*; import java.io.*; public class ReadURL { public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception { URL osu = new URL(" URL osu = new URL(" BufferedReader in = new BufferedReader( new InputStreamReader(osu.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); System.out.println(inputLine);in.close(); }} This prints out the source code for the webpage

Connecting with a URL (2) openConnection() : Returns a URLConnection object that represents a connection to the remote object referred to by the URL. It may throws an IOException openConnection() : Returns a URLConnection object that represents a connection to the remote object referred to by the URL. It may throws an IOException try { URL osu = new URL(" URL osu = new URL(" URLConnection osuConnection = osu.openConnection(); URLConnection osuConnection = osu.openConnection(); } catch (MalformedURLException e) { // new URL() failed } catch (IOException e) {......} The URLConnection class provides many methods to communicate with the URL, such as reading and writing. The URLConnection class provides many methods to communicate with the URL, such as reading and writing.

Sockets Sometimes you need a low-level network communication, such as a client-server application Sometimes you need a low-level network communication, such as a client-server application The TCP protocol provides a reliable point-to-point communication channel via the sockets. The TCP protocol provides a reliable point-to-point communication channel via the sockets. A socket is an endpoint for reliable communication between two machines. To connect with each other, each of the client and the server binds a socket to its end for reading and writing. A socket is an endpoint for reliable communication between two machines. To connect with each other, each of the client and the server binds a socket to its end for reading and writing. The java.net package provides two classes — Socket and ServerSocket — to implement the client and the server, respectively. The java.net package provides two classes — Socket and ServerSocket — to implement the client and the server, respectively.

Establishing a simple server Five steps: Five steps: 1).Create a ServerSocket object ServerSocket server=new ServerSocket(port,queueLength); 2).The server listens indefinitely (or blocks) for an attempt by a client to connect Socket connection = server.accept(); 3).Get the OutputStream and InputStream objects that enable the server to communicate with the client by sending and receiving bytes InputStream input = connection.getInputStream(); OutputStream output = connection.getOutputStream(); * You can get a stream of other data types from the InputStream and OutputStream 4).Processing phase: the server and the client communicate via the InputStream and the OutputStream objects 5).After the communication completes, the server closes the connection by invoking close() on the Socket and the corresponding streams

Establishing a simple client Four steps: Four steps: 1).Create a Socket object Socket connection = new Socket (serverAddress, port); Socket connection = new Socket (serverAddress, port); 2).Get the OutputStream and InputStream of the Socket. The server and the client must send and receive the data in the same format 3).Processing phase: the server and the client communicate via the InputStream and the OutputStream objects 4).After the communication completes, the client closes the connection.

The server side import java.lang.*; import java.io.*; import java.net.*; class Server { public static void main(String args[]) { public static void main(String args[]) { String data = "Let's test if we can connect..."; String data = "Let's test if we can connect..."; try { try { ServerSocket server_socket = new ServerSocket(1234); ServerSocket server_socket = new ServerSocket(1234); System.out.println("I’ve started, dear clients..."); System.out.println("I’ve started, dear clients..."); Socket socket = server_socket.accept(); Socket socket = server_socket.accept(); System.out.print("Server has connected!\n"); System.out.print("Server has connected!\n"); PrintWriter outToClient = new PrintWriter(socket.getOutputStream(), true); PrintWriter outToClient = new PrintWriter(socket.getOutputStream(), true); System.out.print("Sending string: ‘" + data + “’\n"); System.out.print("Sending string: ‘" + data + “’\n"); outToClient.print(data); outToClient.print(data); outToClient.close(); outToClient.close(); socket.close(); socket.close(); server_socket.close(); server_socket.close(); } catch(Exception e) { catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); System.out.print("Whoops! It didn't work!\n"); } }} (this example is got from A simple server/client pair example

A simple server/client pair example (cont.) The client side import java.lang.*; import java.io.*; import java.net.*; class Client { public static void main(String args[]) { public static void main(String args[]) { try { try { Socket socket = new Socket("localhost", 1234); Socket socket = new Socket("localhost", 1234); BufferedReader inFromServer = new BufferedReader(new BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream())); InputStreamReader(socket.getInputStream())); System.out.print("Received string: ‘"); System.out.print("Received string: ‘"); while (inFromServer.ready()) while (inFromServer.ready()) System.out.println(in.readLine()); //Read one line and output it System.out.println(in.readLine()); //Read one line and output it inFromServer.close(); inFromServer.close(); } catch(Exception e) { catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); System.out.print("Whoops! It didn't work!\n"); } }} (this example is got from

A simple server/client pair example (cont.) What happens on the screen if you run the code? What happens on the screen if you run the code? First run Server.java First run Server.java Then run Client.java Then run Client.java

A simple server/client pair example (cont.) If you run Client.java without running Server.java If you run Client.java without running Server.java

Datagrams The UDP protocol provides a mode of network communication whereby datagrams are sent over the network. DatagramSocket s are used for the connection. The UDP protocol provides a mode of network communication whereby datagrams are sent over the network. DatagramSocket s are used for the connection. A datagram’s arrival, arrival time and order of arrival is not guaranteed. It’s used whenever an information needs to be broadcast periodically A datagram’s arrival, arrival time and order of arrival is not guaranteed. It’s used whenever an information needs to be broadcast periodically

UDP example import java.net.*; class GetDate { final static int PORT_DAYTIME = 13; final static int PORT_DAYTIME = 13; public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception { DatagramSocket dgsocket = new DatagramSocket(); DatagramSocket dgsocket = new DatagramSocket(); InetAddress destination = InetAddress.getByName("news.cis.ohio-state.edu");; InetAddress destination = InetAddress.getByName("news.cis.ohio-state.edu");; DatagramPacket datagram; DatagramPacket datagram; byte[] msg = new byte[256]; byte[] msg = new byte[256]; datagram = new DatagramPacket(msg, msg.length, destination, PORT_DAYTIME); datagram = new DatagramPacket(msg, msg.length, destination, PORT_DAYTIME); dgsocket.send(datagram); dgsocket.send(datagram); datagram = new DatagramPacket(msg, msg.length); datagram = new DatagramPacket(msg, msg.length); dgsocket.receive(datagram); dgsocket.receive(datagram); String received = new String(datagram.getData()); String received = new String(datagram.getData()); System.out.println("Time of machine:" + received); System.out.println("Time of machine:" + received); dgsocket.close(); dgsocket.close(); }} Sample excution: alpha> java GetDate Time of machine: Tue May 24 00:16:

Supplemental reading Custom networking Custom networking JavaTM Programming Language Basics, Socket Communications JavaTM Programming Language Basics, Socket Communications Java2/socket.html Java2/socket.html