NETWORK PROGRAMMING.

Slides:



Advertisements
Similar presentations
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r Sockets are explicitly created, used, released by applications.
Advertisements

Network Programming and Java Sockets
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Application Layer – Lecture.
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.
9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003.
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.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
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.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
1 Review of Previous Lecture r Electronic Mail r DNS r P2P file sharing.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
Networking Overview February 2, /2/2004 Assignments Due – Homework 0 Due – Reading and Warmup questions Work on Homework 1.
2: Application Layer1 Chapter 2 (continued) Application Layer – part 2 Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Socket programming with UDP and TCP. Socket Programming with TCP Connection oriented – Handshaking procedure Reliable byte-stream.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
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,
1 Network Layers Application Transport Network Data-Link Physical bits.
Protocols Rules for communicating between two entities (e.g., a client and a server) “A protocol definition specifies how distributed system elements interact.
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,
JAVA Socket Programming Joonbok Lee KAIST.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
ECE5650: Network Programming
1 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.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 2 nd edition. Jim Kurose, Keith Ross.
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.
Department of Computer and IT Engineering University of Kurdistan Computer Networks II Socket programming By: Dr. Alireza Abdollahpouri.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5 th edition. Jim Kurose, Keith Ross Addison-Wesley, April A note on the use.
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.
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.
Java Socket programming. Socket programming with TCP.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
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.
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.
Chapter 2 Application Layer Application 2-1. Chapter 2: Application layer 2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic.
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.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
1 All rights reserved to Chun-Chuan Yang Upon completion you will be able to: The OSI Model and the TCP/IP Protocol Suite Understand the architecture of.
Topic: Network programming
Socket Programming Socket Programming Overview
Transport layer (last part) Application layer
DNS: Domain Name System
Socket programming with TCP
Chapter 2: outline 2.1 principles of network applications
URL in Java C343 Lab (Week 13).
Socket programming - Java
CSCD 330 Network Programming
Socket Programming Socket Programming Overview
Socket Programming.
CSCD 330 Network Programming
Java Socket Programming
Socket Programming 2: Application Layer.
DNS: Domain Name System
CPSC 441 UDP Socket Programming
Chapter 2: Application layer
DNS: Domain Name System
DNS: Domain Name System
Socket Programming with UDP
Presentation transcript:

NETWORK PROGRAMMING

Introduction The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.

Introduction The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details.

Introduction The java.net package provides support for the two common network protocols: TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

Introduction The java.net package provides support for the two common network protocols: TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

URL Processing Socket Programm

Url Processing

Url Processing URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory. This section shows you how to write Java programs that communicate with a URL. A URL can be broken down into parts, as follows: protocol://host:port/path?query#ref

Url Processing The following is a URL to a Web page whose protocol is HTTP: http://www.xyz.com/index.htm?language=en#j2se Notice that this URL does not specify a port, in which case the default port for the protocol is used. With HTTP, the default port is 80.

URL Class Methods: The java.net.URL class represents a URL and has complete set of methods to manipulate URL in Java. The URL class has several constructors for creating URLs, including the following: (1) public URL(String protocol, String host, int port, String file) throws MalformedURLException. Creates a URL by putting together the given parts.

URL Class Methods: (2)public URL(String protocol, String host, String file) throws MalformedURLException Identical to the previous constructor, except that the default port for the given protocol is used. (3) public URL(String url) throws MalformedURLException Creates a URL from the given String

URL Class Methods: (4)public URL(URL context, String url) throws MalformedURLException Creates a URL by parsing the together the URL and String arguments

URL Class Methods: The URL class contains many methods for accessing the various parts of the URL being represented. Some of the methods in the URL class include the following: (1) public String getPath() --Returns the path of the URL. (2) public String getQuery()--Returns the query part of the URL. (3) public String getAuthority()--Returns the authority of the URL.

URL Class Methods: (4)public int getPort() --Returns the port of the URL. (5)public int getDefaultPort() --Returns the default port for the protocol of the URL. (6)public String getProtocol() --Returns the protocol of the URL. (7)public String getHost() --Returns the host of the URL. (8) public String getFile()--Returns the filename of the URL. (9)public String getRef()--Returns the reference part of the URL. (10) public URLConnection openConnection() throws IOException --Opens a connection to the URL, allowing a client to communicate with the resource.

Example The following URLDemo program demonstrates the various parts of a URL. A URL is entered on the command line, and the URLDemo program outputs each part of the given URL. See URLDemo in netbeans

URLConnections Class Methods The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections. For example: If you connect to a URL whose protocol is HTTP, the openConnection() method returns an HttpURLConnection object. If you connect to a URL that represents a JAR file, the openConnection() method returns a JarURLConnection object. etc...

URLConnections Class Methods The URLConnection class has many methods for setting or determining information about the connection, including the following: (1)Object getContent() --Retrieves the contents of this URL connection. (2)Object getContent(Class[] classes) --Retrieves the contents of this URL connection. (3)String getContentEncoding() --Returns the value of the content- encoding header field. (4)int getContentLength() --Returns the value of the content-length header field.

URLConnections Class Methods (5)String getContentType() --Returns the value of the content-type header field. (6)int getLastModified() --Returns the value of the last-modified header field. (7)long getExpiration() --Returns the value of the expires header field. (8 )long getIfModifiedSince() --Returns the value of this object's ifModifiedSince field.

URLConnections Class Methods (5)String getContentType() --Returns the value of the content-type header field. (6)int getLastModified() --Returns the value of the last-modified header field. (7)long getExpiration() --Returns the value of the expires header field. (8 )long getIfModifiedSince() --Returns the value of this object's ifModifiedSince field. (9)public URL getURL() --Returns the URL that this URLConnection object is connected to

Example The following URLConnectionDemo program connects to a URL entered from the command line. If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read one line at a time. See URLConnDemo in netbeans

Java Socket Programming

Socket Programming Socket-The combination of an IP address and a port number. A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and Server Socket--that implement the client side of the connection and the server side of the connection, respectively.

Java Sockets Programming The package java.net provides support for sockets programming (and more). Typically you import everything defined in this package with: import java.net.*;

Classes InetAddress Socket ServerSocket DatagramSocket DatagramPacket

Server Listening to the port, Establishing connections Server side program: Listening to the port, Establishing connections Reading from and writing to the socket.

Server ServerSocket ssoc=new ServerSocket(1111); A socket is an end point for communication between two machines Server ServerSocket ssoc=new ServerSocket(1111); This ServerSocket object is used to listen on a port

Port is an address that identifies the particular application in destination machine 192.256.17.25 Port: 1111

Client Server Establishing connections Socket csoc=ssoc.accept(); Accepting the connection Client Socket csoc=ssoc.accept(); Client socket object which is bound to same local port(1111) The communication is done through the new socket object

In Server program: BufferedReader fromc= new BufferedReader(new InputStreamReader(csoc.getInputStream())) csoc.getInputStream: It reads input from socket object PrintStream toc=new PrintStream(csoc.getOutputStream()); csoc.getOutputStream: It writes stream of data to socket object

In client program: PrintStream tos=new PrintStream(soc.getOutputStream()); It writes stream of data to socket object BufferedReader froms=new BufferedReader(new InputStreamReader(soc.getInputStream())); It reads input from socket object BufferedReader fromkb=new BufferedReader(new InputStreamReader(System.in)); Takes the input from keyboard

TCPClient.java import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception {         String sentence;         String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789);         DataOutputStream outToServer =         new DataOutputStream(clientSocket.getOutputStream());

TCPClient.java BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));         sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence);        clientSocket.close();                    } }

TCPServer.java import java.io.*; import java.net.*; class TCPServer {   public static void main(String argv[]) throws Exception     {       String clientSentence;       String capitalizedSentence;   ServerSocket welcomeSocket = new ServerSocket(6789);   while(true) { Socket connectionSocket = welcomeSocket.accept();            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

TCPServer.java DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());            clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence);         }

UDP Sockets UDP Socket Programming with UDP Connectionless and unreliable service. There isn’t an initial handshaking phase. Doesn’t have a pipe. transmitted data may be received out of order, or lost Socket Programming with UDP No need for a welcoming socket. No streams are attached to the sockets. the sending hosts creates “packets” by attaching the IP destination address and port number to each batch of bytes. The receiving process must unravel to received packet to obtain the packet’s information bytes.

UDP Sockets DatagramSocket class DatagramPacket class needed to specify the payload incoming or outgoing

UDPClient.java import java.io.*; import java.net.*; class UDPClient {     public static void main(String args[]) throws Exception     {         BufferedReader inFromUser =         new BufferedReader(new InputStreamReader(System.in));         DatagramSocket clientSocket = new DatagramSocket();         InetAddress IPAddress = InetAddress.getByName("hostname");         byte[] sendData = new byte[1024];       byte[] receiveData = new byte[1024];         String sentence = inFromUser.readLine();         sendData = sentence.getBytes();

Java Socket Programming UDPClient.java       DatagramPacket sendPacket =          new DatagramPacket(sendData, sendData.length, IPAddress, 9876);    clientSocket.send(sendPacket);    DatagramPacket receivePacket =          new DatagramPacket(receiveData, receiveData.length);    clientSocket.receive(receivePacket);    String modifiedSentence =          new String(receivePacket.getData());    System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close();       } } Java Socket Programming

UDPServer.java import java.io.*; import java.net.*; class UDPServer {   public static void main(String args[]) throws Exception     {         DatagramSocket serverSocket = new DatagramSocket(9876);         byte[] receiveData = new byte[1024];       byte[] sendData  = new byte[1024];         while(true)         {             DatagramPacket receivePacket =              new DatagramPacket(receiveData, receiveData.length);             serverSocket.receive(receivePacket);             String sentence = new String(receivePacket.getData());

UDPServer.java      InetAddress IPAddress = receivePacket.getAddress();      int port = receivePacket.getPort();     String capitalizedSentence = sentence.toUpperCase();         sendData = capitalizedSentence.getBytes();      DatagramPacket sendPacket =        new DatagramPacket(sendData, sendData.length, IPAddress, port);       serverSocket.send(sendPacket);       } }

(Email id-jatinit2010@gmail.com/jatin.patel@gperi.ac.in) CONTACT ME Prof. Jatin Patel (Email id-jatinit2010@gmail.com/jatin.patel@gperi.ac.in) thank you! 4x3 16x9 Prof. Jatin Patel