Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.

Slides:



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

Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
Referring to Java API Specifications
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.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Jan Java Networking TCP Yangjun Chen Dept. Business Computing University of Winnipeg.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
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.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Client/Server In Java An Introduction to TCP/IP and Sockets.
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.
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.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
DBI Representation and Management of Data on the Internet.
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.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
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.
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.
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.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
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.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
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 Inetaddress Class When establishing a connection across the Internet, addresses.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Chapter 03 Networking & Security
Topic: Network programming
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Java Network Programming
„Networking”.
CSCD 330 Network Programming
Sockets and URLs 13-Nov-18.
Networking.
Sockets and URLs 3-Dec-18.
CSCD 330 Network Programming
Java Socket Programming
Programming TCP Sockets
Presentation transcript:

Object Oriented Programming in Java Lecture 16

Networking in Java Concepts Technicalities in java

Client / Server Model Relationship between two computer programs Client –Initiates communication –Requests services Server –Receives requests –Provides services Client Server

Addressing How a service uniquely identified in the internet? IP Address Port

Running a Server 1.Determine server location - port (& IP address) 2.Listen for connections on the port (&IP) 3.Open network connection to client 4.Read data from client (request) 5.Write data to client (response) 6.Close network connection to client 7.Stop server

Client Operations 1.Determine server location – IP address & port 2.Open network connection to server 3.Write data to server (request) 4.Read data from server (response) 5.Close network connection 6.Stop client

Classes in java.net The core package java.net contains a number of classes that allow programmers to carry out network programming –ContentHandler –DatagramPacket –DatagramSocket –DatagramSocketImplHttpURLConnection –ABNSSDKmsna –ServerSocket –Socket –SocketImpl –URL –URLConnection –URLEncoder –URLStreamHandler

Exceptions in Java BindException ConnectException MalformedURLException NoRouteToHostException ProtocolException SocketException UnknownHostException UnknownServiceExceptionZx

The InetAddress Class Handles Internet addresses both as host names and as IP addresses Static Method getByName returns the IP address of a specified host name as an InetAddress object Methods for address/name conversion: public static InetAddress getByName(String host) throws UnknownHostException public static InetAddress[] getAllByName(String host) throws UnknownHostException public static InetAddress getLocalHost() throws UnknownHostException public boolean isMulticastAddress() public String getHostName() public byte[] getAddress() public String getHostAddress() public int hashCode() public boolean equals(Object obj) public String toString()

import java.net.*; import java.io.*; public class IPFinder { public static void main(String[] args) throws IOException { String host; BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); System.out.print("\n\nEnter host name: "); host = input.readLine(); try { InetAddress address = InetAddress.getByName(host); System.out.println("IP address: " + address.toString()); } catch (UnknownHostException e) { System.out.println("Could not find " + host); }

Retrieving the current machine’s address import java.net.*; public class MyLocalIPAddress { public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println (address); } catch (UnknownHostException e) { System.out.println("Could not find local address!"); }

The Java.net.Socket Class Connection is accomplished through the constructors. Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new Socket object. public Socket(String host, int port) throws UnknownHostException, IOException public Socket(InetAddress address, int port) throws IOException public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException public Socket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException Sending and receiving data is accomplished with output and input streams. There are methods to get an input stream for a socket and an output stream for the socket. public InputStream getInputStream() throws IOException public OutputStream getOutputStream() throws IOException There's a method to close a socket: public void close() throws IOException

The Java.net.SocketSocket Class The java.net.ServerSocket class represents a server socket. It is constructed on a particular port. Then it calls accept() to listen for incoming connections. –accept() blocks until a connection is detected. –Then accept() returns a java.net.Socket object that is used to perform the actual communication with the client. public ServerSocket(int port) throws IOException public ServerSocket(int port, int backlog) throws IOException public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException public Socket accept() throws IOException public void close() throws IOException

TCP Sockets SERVER: 1.Create a ServerSocket object ServerSocket servSocket = new ServerSocket(1234); 2.Put the server into a waiting state Socket link = servSocket.accept(); 3.Set up input and output streams 4.Send and receive data out.println(awaiting data…); String input = in.readLine(); 5.Close the connection link.close()

Set up input and output streams Once a socket has connected you send data to the server via an output stream. You receive data from the server via an input stream. Methods getInputStream and getOutputStream of class Socket: BufferedReader in = new BufferedReader( new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter(link.getOutputStream(),true);

TCP Sockets CLIENT: 1.Establish a connection to the server Socket link = new Socket(inetAddress.getLocalHost(),1234) ; 2.Set up input and output streams 3.Send and receive data 4.Close the connection

“Hello World” Client/Server a.import java.net.*; b.import java.io.*; c.public class Client { d. public static void main(String args[]) throws Exception { e. Socket s = new Socket("localhost", 888 ); // Connect f. BufferedReader in = new BufferedReader( g. new InputStreamReader( s.getInputStream( ) ) ); h. System.out.println( in.readLine() ); i. } j.} A.import java.net.*; B.import java.io.*; C.public class Server { D. public static void main(String args[]) throws Exception { E. ServerSocket connection = new ServerSocket( 888 ); F. Socket s = connection.accept(); // connection wait G. PrintStream out = new PrintStream(s.getOutputStream( ) ); H. out.println("Hello World"); I. } J.} 1. (F) Server listens for connection on port (H) Server writes out “Hello World” to connection. 2. (e) Client connects on port (h) Client reads in “Hello World” from connection. Server Client

“Hello World” Client/Server a.import java.net.*; b.import java.io.*; c.public class Client { d. public static void main(String args[]) throws Exception { e. Socket s = new Socket("localhost", 888 ); // connect f. BufferedReader in = new BufferedReader( g. new InputStreamReader( s.getInputStream( ) ) ); h. System.out.println( in.readLine() ); i. } j.} A.import java.net.*; B.import java.io.*; C.public class Server { D. public static void main(String args[]) throws Exception { E. ServerSocket connection = new ServerSocket( 888 ); F. while(true) { G. Socket s = connection.accept(); // connection wait H. PrintStream out=new PrintStream(s.getOutputStream()); I. out.println("Hello World"); J. } K. } L.} 1. (G) Server listens for connection on port (I) Server writes out “Hello World” to connection. 5. Server closes connection, go to (e) Client connects on port (h) Client reads in “Hello World” from connection. Server Client