CHAPTER-3 NETWORKING PROGRAMMING. Network represents interconnection of computers & devices either by cable or satellite link where no cable is needed.

Slides:



Advertisements
Similar presentations
Network Programming Chapter 11 Lecture 6. Networks.
Advertisements

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.
1 Java Networking – Part I CS , Spring 2008/9.
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.
Networking Theory (part 2). Internet Architecture The Internet is a worldwide collection of smaller networks that share a common suite of communication.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Process-to-Process Delivery:
2: Application Layer 1 Socket Programming TCP and UDP.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Computer Networks  Network - A system of computers interconnected in order to share information.  Data transmission - consists of sending and receiving.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
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.
 TCP/IP is the communication protocol for the Internet  TCP/IP defines how electronic devices should be connected to the Internet, and how data should.
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.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Socket Programming Lee, Sooyong
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket 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.
TCP/IP (Transmission Control Protocol / Internet Protocol)
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 Networking A network represents interconnection of computers that is capable.
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.
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.
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.
The OSI Model. Understanding the OSI Model In early 1980s, manufacturers began to standardize networking so that networks from different manufacturers.
Network Programming. These days almost all devices.
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:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
SOCKET PROGRAMMING.
Networking Based Applications
Computer Networks.
MCA – 405 Elective –I (A) Java Programming & Technology
Networking for Home and Small Businesses – Chapter 6
Lecture 6: TCP/IP Networking By: Adal Alashban
Web Development & Design Chapter 1, Sections 4, 5 & 6
Networking for Home and Small Businesses – Chapter 6
Transport Layer Our goals:
„Networking”.
Topic 5: Communication and the Internet
I. Basic Network Concepts
UNIT-6.
Chapter 2: outline 2.1 principles of network applications
Basic Protocols 24-Nov-18.
Socket programming - Java
Process-to-Process Delivery:
Lecture 6: TCP/IP Networking 1nd semester By: Adal ALashban.
Socket Programming.
Networking Theory (part 2)
CSCD 330 Network Programming
Basic Protocols 19-Feb-19.
Socket Programming 2: Application Layer.
Networking for Home and Small Businesses – Chapter 6
Chapter 2: Application layer
Process-to-Process Delivery: UDP, TCP
Networking Theory (part 2)
Networking Theory (part 2)
Review Communication via paired sockets, one local and one remote
Presentation transcript:

CHAPTER-3 NETWORKING PROGRAMMING

Network represents interconnection of computers & devices either by cable or satellite link where no cable is needed. Client- receives service. Server- provides service. Reuirement for network:  Hardware  Software  protocol

Networking Basics TCP/IP suite UDP Port & socket URL

The TCP/IP protocol suite ( Internet Protocol Suite) is a collection of protocols that collectively provides the data transport services used on the Internet. They provide a robust and efficient mechanism for moving data between machines across computer networks. The suite is split into five layers TCP/IP Five Layer Model Application Layer - interfaces between application processes and transport layer services on host computer.

Transport Layer - determines how to use the network layer to provide a virtual point-to-point connection between source and destination. TCP segments Network/Internet Layer - layer by which data packets are routed from source to destination. Datagrams Data Link Layer - provides data transfer control across the physical layer Physical Layer - the actual physical medium used for the transfer (e.g. cables, Infra red and microwave)

IP address It is unique identification number assigned to each computer. Contains four integr numbers in range 0 to 255. Separated by dot. Address mapped to easy remember site name. Like, www.yahoo.com Mapping service is called DNS(Domain Name Service.)

Protocols HTTP (Hyper Text Transfer Protocol) – transfer web pages from one computer to another on internet. FTP (File Transfer Protocol)- download & upload files from & to the server. SMTP (Simple Mail Transfer Protocol)- sending mails on network. POP (Post Office Protocol)- receives mails into mailboxes.

UDP Protocol that transfers data in connection less & unreliable manner on network. Does not maintain record of no. Of bits delivered or received on network. During transmission data may be lost. Faster. Utilizes for images, video,audio files transfer over network.

Port & Socket Port is 16 bit number to identify a process on machine. Socket is logical connecting point between client & server that allows communication between two hosts over network. Socket contains a port number & IP address. IP address identifies machine & port number identifies process on that machine. A distinct port number is used between 0 to

80- HTTP 109-POP 21-FTp 25-SMTp 23-Telnet for remote login. Java.net package

URL Represents address to access some information or resource on interent. Example: Protocol:http IP adress or sever name: Port number:80 File resource: index.html

TCPUDP Reliability: TCP is connection-oriented protocol. When a file or message send it will get delivered unless connections fails. If connection lost, the server will request the lost part. There is no corruption while transferring a message. Reliability: UDP is connectionless protocol. When you a send a data or message, you don't know if it'll get there, it could get lost on the way. There may be corruption while transferring a message. Ordered: If you send two messages along a connection, one after the other, you know the first message will get there first. You don't have to worry about data arriving in the wrong order. Ordered: If you send two messages out, you don't know what order they'll arrive in i.e. no ordered

TCP VS. UDP TCPUDP eavyweight: - when the low level parts of the TCP "stream" arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together. Lightweight: No ordering of messages, no tracking connections, etc. It's just fire and forget! This means it's a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets. Streaming: Data is read as a "stream," with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call. Datagrams: Packets are sent individually and are guaranteed to be whole if they arrive. One packet per one read call.

Networking Programming import java.io.*; import java.net.*; class Address { public static void main(String args[ ]) throws IOException { //accept name of website from keyboard BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a website name: "); String site = br.readLine(); try{ //getByName() method accepts site name and returns its IP //Address InetAddress ip = InetAddress.getByName(site); System.out.println("The IP Address is: "+ ip); }catch(UnknownHostException ue) { System.out.println("Website not found"); }

URL class import java.net.*; class MyURL { public static void main(String args[ ]) throws Exception { URL obj = new URL(" System.out.println("Protocol: "+ obj.getProtocol()); System.out.println("Host: "+ obj.getHost()); System.out.println("File: "+ obj.getFile()); System.out.println("Port: "+ obj.getPort()); System.out.println("Path: "+ obj.getPath()); System.out.println("External form: "+ obj.toExternalForm()); }

import java.io.*; import java.net.*; import java.util.*; class Details { public static void main(String args[ ]) throws Exception { //pass the site url to URL object URL obj = new URL(" //open a connection with the site on Internet URLConnection conn = obj.openConnection(); //display the date System.out.println("Date: "+ new Date(conn.getDate())); //display the content type whether text or html

System.out.println("Content-type: "+ conn.getContentType()); //display expiry date System.out.println("Expiry: "+ conn.getExpiration()); //display last modified date System.out.println("Last modified: "+ new Date(conn.getLastModified())); //display how many bytes the index.html page has

int l = conn.getContentLength(); System.out.println("Length of content: "+ l); if(l == 0) { System.out.println("Content not available"); return; } else { int ch; InputStream in = conn.getInputStream(); //display the content of the index.html page while((ch = in.read())!= -1) System.out.print((char)ch); }

Output Date: Wed Aug 07 11:00:14 IST 2013 Content-type: text/html;charset=utf-8 Expiry: 0 Last modified: Thu Jan 01 05:30:00 IST 1970 Length of content: -1

Yahoo! India //Roundtrip

Creating Network Application Creates server that delivers information. Creates client that receives information. Execute client & server program. Remember: Sockets are communication channels, which facilitate inter-process communication. A socket is one end of a two-way communications link between two programs running on the network. When a computer program needs to connect to a local or wide area network such as the Internet,

It uses a software component called a socket. The socket opens the network connection for the program, allowing data to be read and written over the network. It is important to note that these sockets are software, not hardware.

Steps for ServerSocket 1. Create a ServerSocket object with some port number at server side. ServerSocket ss = new ServerSocket(777); 2. Make server to wait till client accept the connection. Socket s = ss.accept(); 3. Attach output stream to server socket using its getOutputStream() method. It is used by server socket to send data to client. OutputStream obj = s.getOutputStream();

4. Take another stream like PrintStream to send data to client. PrintStream ps = new PrintStream(obj); 5. Send data to client using println() method of PritnStream object. ps.println(str); 6. Close the connection ps.close(); ss.close(); s.close();

Steps for Client socket 1. Create Socket at client side using Socket class. Socket s = new Socket(“IPaddress", 777); 2. Add InputStream to socket to receive data. InputStream obj = s.getInputStream(); 3. Create BufferedReader object to read data. BufferedReader br = new BufferedReader(new InputStreamReader(obj)); 4. Read data using read() or readLine() method. read() can read single character at a time, while readLine() can read string.

str = br.readLine(); 5. Close the connection by closing all streams & sockets. br.close(); s.close();

Server1.java import java.io.*; import java.net.*; class Server1 { public static void main(String args[ ]) throws Exception { //Create a server socket with some port number ServerSocket ss = new ServerSocket(777); //let the server wait till a client accepts connection Socket s = ss.accept(); System.out.println("Connection established"); //attach output stream to the server socket

OutputStream obj = s.getOutputStream(); //attach print stream to send data to the socket PrintStream ps = new PrintStream(obj); //send 2 strings to the client String str = "Hello client"; ps.println(str); ps.println("Bye"); //close connection by closing the streams and sockets ps.close(); ss.close(); s.close(); }

Client1.java mport java.io.*; import java.net.*; class Client1 { public static void main(String args[ ]) throws Exception { //create client socket with same port number Socket s = new Socket("localhost", 777); //to read data coming from server, attach InputStream to the socket InputStream obj = s.getInputStream(); //to read data from the socket into the client, use BufferedReader

BufferedReader br = new BufferedReader(new InputStreamReader(obj)); //receive strings String str; while((str = br.readLine()) != null) System.out.println("From server: "+str); //close connection by closing the streams and sockets br.close(); s.close(); }

Two way communication import java.io.*; import java.net.*; class Server2 { public static void main(String args[ ]) throws Exception { //Create server socket ServerSocket ss = new ServerSocket(888); //connect it to client socket Socket s = ss.accept(); System.out.println("Connection established"); //to send data to the client PrintStream ps = new PrintStream(s.getOutputStream()); //to read data coming from the client BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //to read data from the key board

BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); while(true) //server executes continuously { String str,str1; //repeat as long as client does not send null string while((str = br.readLine()) != null) //read from client { System.out.println(str); str1 = kb.readLine(); ps.println(str1); //send to client } //close connection

ps.close(); br.close(); kb.close(); ss.close(); s.close(); System.exit(0); //terminate application } //end of while }

Client2.java import java.io.*; import java.net.*; class Client2 { public static void main(String args[ ]) throws Exception { //Create client socket Socket s = new Socket("localhost", 888); //to send data to the server DataOutputStream dos = new DataOutputStream(s.getOutputStream()); //to read data coming from the server BufferedReader br = new BufferedReader(new

InputStreamReader(s.getInputStream())); //to read data from the key board BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); String str,str1; //repeat as long as exit is not typed at client while(!(str = kb.readLine()).equals("exit")) { dos.writeBytes(str+"\n"); //send to server str1 = br.readLine(); //receive from server System.out.println(str1); }

//close connection. dos.close(); br.close(); kb.close(); s.close(); }

Retrieve file import java.io.*; import java.net.*; class FileServer { public static void main(String args[ ]) throws Exception { //create server socket ServerSocket ss = new ServerSocket(8888); //make the server wait till a client accepts connection Socket s = ss.accept(); System.out.println("Connection established"); //to accept file name from client BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); //to send file contents to client }

DataOutputStream out = new DataOutputStream(s.getOutputStream()); //read the filename from the client String fname = in.readLine(); FileReader fr = null; BufferedReader file = null; boolean flag; //create File class object with filename File f = new File(fname); //test if file exists or not if(f.exists()) flag = true; else flag = false; //if file exists, send "Yes" to client, else send "No" if(flag == true) out.writeBytes("Yes"+"\n"); else out.writeBytes("No"+"\n");

if(flag == true) { //attach file to the FileReader to read data fr = new FileReader(fname); //attach FileReader to BufferedReader file = new BufferedReader(fr); String str; //read from BufferedReader and write to DataOutputStream while((str = file.readLine()) != null) { out.writeBytes(str+"\n"); }

file.close(); out.close(); in.close(); fr.close(); s.close(); ss.close(); } }

myfile.txt

UDP UDP transfers datagrams from a process running in source host to other process running in destination host. Each datagram needs destination address. Datagram communication through java.net package classes:  DatagramPacket  DatagramSocket

DatagramPacket is used for creating packets or datagrams. Different datagrams travel through different route, may arrive in any order and their delivery is not guaranteed in UDP. DatagramSocket is used for sending & receiving datagram packets over network. A datagram socket provides an endpoint & methods to send and receive datagram packets over a network.

//A server that sends a messages to the client import java.net.*; class UDPServerEx { public static DatagramSocket mySocket; public static byte myBuffer[]=new byte[2000]; public static void serverMethod() throws Exception { int position=0; while(true) { int charData=System.in.read();

UDP server switch(charData) { case -1: System.out.println("The execution of the server has been terminated"); return; case '\r':break; case '\n':mySocket.send(new DatagramPacket(myBuffer,position,InetAddress.getLocalHost(),777)); position=0; break; default: myBuffer[position++]=(byte) charData; }

UDP client //UDPClient - receives and displays messages sent from the server import java.net.*; class UDPClient { public static DatagramSocket mySocket; public static byte myBuffer[]=new byte[2000]; public static void clientMethod() throws Exception { while(true) { DatagramPacket dataPacket=new DatagramPacket(myBuffer,myBuffer.length);

public static void main(String args[]) throws Exception { System.out.println("Please enter some text here"); mySocket=new DatagramSocket(888); serverMethod(); }

mySocket.receive(dataPacket); System.out.println("Message Recieved :"); System.out.println(new String(dataPacket.getData(),0,dataPacket.getLength())); } public static void main(String args[]) throws Exception { System.out.println("You need to press CTRL+C in order to quit"); mySocket=new DatagramSocket(777); clientMethod(); }

Content Handler & Protocol Handler Content handler is java object for parsing the content of web page and showing it in web browser window, at client side. Every new web page type, such as Portable Network Graphics (PNG) format, jpg, doc, audio, is associated with content handler. All content handler extends java.net.Content Handler class. Content hadler is invoked through getContent() method of URLConnection class.

The job of content handler to read data provided by protocol handler. Protocol handlers are responsible for how data arrives at host. Suppose data delivered from network in compressed form, it is responsibility of protocol handler to unpack data before providing it to content handler.

try{ URL u= new URL( Image i= (Image)u.getContent(); } Catch(Exception e) { e.printStackTrace(); }

In code from Url string firsat we find protocol used. Here it is HTTP, so HTTP protocol handler is responsible for opening the connection with host & transmitting requested data. After connection establishing, protocol handler finds requested file Multipurpose Internet Mail Extension (MIME) type. Protocol handler considers file extension which is.jpg here.

Then content handler is responsible for MIME type invocation & then required object I constucted. Here it returns Image type object, which is returned by getContent() method.