Topic: 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.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
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.
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.
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.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
NETWORK PROGRAMMING.
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.
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 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.
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.
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.
Socket Programming Socket Programming Overview
Transport layer (last part) Application layer
DNS: Domain Name System
Socket programming with TCP
CSCD 330 Network Programming
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
CSCD 330 Network Programming
Socket Programming Socket Programming Overview
Socket Programming.
CSCD 330 Network 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:

Topic: Network programming & Java Sockets Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Associate Professor, Dept. of Information Technology prabhjot.kaur@msit.in 1

Hosts Devices connected to the Internet are called hosts Most hosts are computers, but hosts also include routers, printers, fax machines, PDAs, Tablets, etc.

Internet addresses Every host on the Internet is identified by a unique, four-byte Internet Protocol (IP) address. This is written in dotted quad format like 199.1.32.90 where each byte is an unsigned integer between 0 and 255. There are about four billion unique IP addresses, but they aren’t very efficiently allocated

Domain Name System (DNS) Numeric addresses are mapped to names like "www.blackstar.com" or "star.blackstar.com" by DNS. Each site runs domain name server software that translates names to IP addresses and vice versa DNS is a distributed system

The InetAddress Class The java.net.InetAddress class represents an IP address. It converts numeric addresses to host names and host names to numeric addresses. It is used by other network classes like Socket and ServerSocket to identify hosts

IP Addresses and Java Java has a class java.net.InetAddress which abstracts network addresses Serves three main purposes: Encapsulates an address and domain name Performs name lookup (converting a host name into an IP address) Performs reverse lookup (converting the address into a host name)

java.net.InetAddress Static construction using a factory method InetAddress getByName(String hostName) hostName can be “host.domain.com.au”, or hostName can be “130.95.72.134” InetAddress getLocalHost() Some useful methods: String getHostName() Gives you the host name (for example “www.sun.com”) String getHostAddress() Gives you the address (for example “192.18.97.241”) InetAddress[] getAllByName(String hostName) Factory methods: a convention whereby static methods in a class return an instance of a class.

Creating InetAddresses There are no public InetAddress() constructors. Arbitrary addresses may not be created. All addresses that are created must be checked with DNS

Getter Methods public boolean isMulticastAddress() public String getHostName() public byte[] getAddress() public String getHostAddress()

Utility Methods public int hashCode() public boolean equals(Object o) public String toString()

Ports In general a host has only one Internet address This address is subdivided into 65,536 ports Ports are logical abstractions that allow one host to communicate simultaneously with many other hosts Many services run on well-known ports. For example, http tends to run on port 80

URLs A URL, short for "Uniform Resource Locator", is a way to unambiguously identify the location of a resource on the Internet.

Example URLs http://java.sun.com/ file:///Macintosh%20HD/Java/Docs/JDK%201.1.1%20docs/api/java.net.InetAddress.html#_top_ http://www.macintouch.com:80/newsrecent.shtml ftp://ftp.info.apple.com/pub/ mailto:elharo@metalab.unc.edu telnet://utopia.poly.edu ftp://mp3:mp3@138.247.121.61:21000/c%3a/stuff/mp3/ http://elharo@java.oreilly.com/ http://metalab.unc.edu/nywc/comps.phtml?category=Choral+Works

The Pieces of a URL the protocol, aka scheme the authority user info user name password host name or address port the path, aka file the ref, aka section or anchor the query string Most URLs can be broken into about five pieces, not all of which are necessarily present in any given URL. These are: There may also be a query string part which is used for CGI data. We’ll talk about that when we discuss CGIs.

The java.net.URL class A URL object represents a URL. The URL class contains methods to create new URLs parse the different parts of a URL get an input stream from a URL so you can read data from a server get content from the server as a Java object

Example try { URL u = new URL("http://www.poly.edu/fall97/grad.html#cs "); System.out.println("The protocol is " + u.getProtocol()); System.out.println("The host is " + u.getHost()); System.out.println("The port is " + u.getPort()); System.out.println("The file is " + u.getFile()); System.out.println("The anchor is " + u.getRef()); } catch (MalformedURLException e) { }

Elements of C-S Computing a client, a server, and network Request Client Server Network Result Client machine Server machine

Networking Basics Applications Layer Transport Layer Network Layer Standard apps HTTP FTP Telnet User apps Transport Layer TCP UDP Programming Interface: Sockets Network Layer IP Link Layer Device drivers TCP/IP Stack Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

Networking Basics TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers. Example applications: HTTP FTP Telnet TCP/IP Stack Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

Networking Basics UDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. Example applications: Clock server Ping TCP/IP Stack Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

Understanding Ports The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer. server P o r t TCP Client app app app app port port port port TCP or UDP Packet Data port# data

Understanding Ports Port is represented by a positive (16-bit) integer value Some ports have been reserved to support common/well known services: ftp 21/tcp telnet 23/tcp smtp 25/tcp login 513/tcp User level process/services generally use port number value >= 1024

Sockets Sockets provide an interface for programming networks at the transport layer. Network communication using Sockets is very much similar to performing file I/O In fact, socket handle is treated like file handle. The streams used in file I/O operation are also applicable to socket-based I/O Socket-based communication is programming language independent. That means, a socket program written in Java language can also communicate to a program written in Java or non-Java socket program.

Socket Communication A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request. server Connection request port Client

Socket Communication If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bounds to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client. server port port Client port Connection

Sockets and Java Socket Classes A socket is an endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent. Java’s .net package provides two classes: Socket – for implementing a client ServerSocket – for implementing a server

Client/server socket interaction: TCP Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP connection setup close connectionSocket read reply from clientSocket create socket, connect to hostid, port=x clientSocket = Socket() wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to

JAVA TCP Sockets In Package java.net java.net.Socket Implements client sockets (also called just “sockets”). An endpoint for communication between two machines. Constructor and Methods Socket(String host, int port): Creates a stream socket and connects it to the specified port number on the named host. InputStream getInputStream() OutputStream getOutputStream() close() java.net.ServerSocket Implements server sockets. Waits for requests to come in over the network. Performs some operation based on the request. ServerSocket(int port) Socket Accept(): Listens for a connection to be made to this socket and accepts it. This method blocks until a connection is made.

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);         }

Socket Exceptions try { Socket client = new Socket(host, port); handleConnection(client); } catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace(); catch(IOException ioe) { System.out.println("IOException: " + ioe); ioe.printStackTrace();

Client/server socket interaction: UDP Server (running on hostid) create socket, clientSocket = DatagramSocket() Client Create, address (hostid, port=x, send datagram request using clientSocket create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket close clientSocket read reply from clientSocket write reply to serverSocket specifying client host address, port umber

JAVA UDP Sockets In Package java.net java.net.DatagramSocket A socket for sending and receiving datagram packets. Constructor and Methods DatagramSocket(int port): Constructs a datagram socket and binds it to the specified port on the local host machine. void receive( DatagramPacket p) void send( DatagramPacket p) void close()

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();

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();       } }

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);       } }

Thank You