Published on

Notes on core java
sub- introduction-1

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Core Java Debasish Pratihari Networking:  Networking is the linking of two or more computing devices together with a purpose of sharing data and other resources.  Networks are built with a mix of computer hardware and computer software.  In networking the communication language used by computer devices is called protocol.  An IP address helps to uniquely identify a device in the network. Network Layers : Application Layer (HTTP, FTP,Telnet…) Application Layer (HTTP, FTP,Telnet…) Transport Layer ( TCP, UDP ) Transport Layer ( TCP, UDP ) Network Layer (IP,…) Network Layer (IP,…) Link Layer ( Device driver,..) (IP,…) Link Layer ( Device driver,..) (IP,…) Physical Layer – Ethernet, wireless, etc… Note :  The OSI Model is having seven layers.  As a java programmer you are programming Application Layer. IP Address :  An Internet Protocol (IP) address is a numerical identification (logical address) that is assigned to devices participating in a computer network.  It is a 32-bit number(4-octects)  Example :  Note : The IP address in IPv6 is 128 bit. However IPv4 is still in use and will be in use for some more years. When a computer is configured to use the same IP address each time it powers up, this is known as a Static IP address. In contrast, in situations when the computer's IP address is assigned automatically, it is known as a Dynamic IP address. Lecture/core/networking/25 Page #1 feel the Technology…
  2. 2. Core Java Port :  Is a logical connection Network. Debasish Pratihari end-point in the  Transport Layer protocols specify a source and destination port number in their packet header.  A port number is a 16-bit unsigned integer(065,535).  A process associates with a particular port (called binding) to send and receive data.  Port number 0- 1023 are knows as wellknown ports Application Layer:  The Application Layer involves all the user related programs like web browsers and ftp.  The application layer programs use various protocols: HTTP FTP Telnet SMTP Hyper Text Transfer Protocol File Transfer Protocol the telnet protocol provides for console sessions. Simple Mail Transport Protocol email. Lecture/core/networking/25 Page #2 feel the Technology…
  3. 3. Core Java Debasish Pratihari URL:  Browsers obtain resources from the Web by specifying the Web addresses, which are referred to officially as Uniform Resource Locators (URL).  Example :  URL components : o Protocol identifier o Resource Name  A general Format of URLs : http://www.lakshyatraining.org/ Protocol_ID://Host_IP_address:Port/Filename#Target Where Protocol_ID HTTP, FTP, etc Host_IP_address Host name in either numerical IP or hostname format Port default is 80 Filename Target Name of a hypertext web page or other type of file. Default index.htm[l] optional reference address within a Web page Socket:   Sockets provide connections between applications and allow streams of data to flow. Java provides two kinds of sockets:  Socket: provides a connection-oriented protocol that behaves like telnet or ftp. The connection remains active, even with no communications occurring, until explicitly broken.  DatagramSocket: o a connectionless protocol o transfers datagram packets o no fixed connection o does not keep packets in order o no guarantee a packet will arrive at its destination Lecture/core/networking/25 Page #3 Note :  Socket uses TCP/IP and DatagramSocket uses UDP protocol feel the Technology…
  4. 4. Core Java Debasish Pratihari URL Example : 25% import java.net.*; import java.io.*; public class URLDemo { public static void main (String[] args) { if (args.length !=1) { System.out.println ("Error: missing the url argument"); System.exit (0); } } try { URL url = new URL (args[0]); System.out.println ("Protocol = " + url.getProtocol ()); System.out.println ("Host = " + url.getHost ()); System.out.println ("File name = " + url.getFile ()); System.out.println ("Port = " + url.getPort ()); System.out.println ("Target = " + url.getRef ()); } catch (MalformedURLException e) { System.out.println ("Bad URL = " + args[0]); } } MalformedURLException :   Each of the four URL constructors throws a MalformedURLException if the arguments to the constructor refer to a null or unknown protocol. you cna handle this exception by embedding try/catch to your try { URL myURL = new URL(. . .) } catch (MalformedURLException e) { ... // exception handler code here ... } Lecture/core/networking/25 Page #4 feel the Technology…
  5. 5. Core Java Debasish Pratihari Using Socket:  A socket is one 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 is destined to be sent.  Basic Steps :      Open a socket. Open an input stream and output stream to the socket. Read from and write to the stream according to the server's protocol. Close the streams. Close the socket. Java Sockets for connection oriented Communication:  Socket types: o ServerSocket o Socket  To create a ServerSoket: ServerSocket ss= new ServerSocket(port,quelength); Where Port - port number queuelength -Specifies the number of clients that can wait for a connection and pressed by the server. If the queue is full client connection refused.  To accept a connection request from the client: Socket con= ss.accept();  To get the input/output stream: Con.getInputStream(); Con.getOutputStream();  To send and receive data: BufferedWriter br= new BufferedWriter(newInputStreamWriter( con.getOutputStream()); BufferedReader br= new BufferedReader(new InputStreamReader(con.getInputStream()); Lecture/core/networking/25 Page #5 feel the Technology…
  6. 6. Core Java Debasish Pratihari Example : Objective – To get the server date using Stream Socket Server Program FileName-DayServer.java import java.net.*; import java.io.*; import java.util.*; public class DayServer{ private ServerSocket ss; public static void main(String args[]) throws IOException{ DayServer ds = new DayServer(); for(;;) ds.serve(); } public DayServer() throws IOException{ ss= new ServerSocket(13,5); } public void serve() throws IOException{ Socket s = null; s = ss.accept(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); out.write("Day & Time :"+ (new Date()).toString()); out.close(); s.close(); } } Lecture/core/networking/25 Page #6 feel the Technology…
  7. 7. Core Java Debasish Pratihari Client Programs FileName – DayClient.java import java.net.*; import java.io.*; import java.util.*; public class DayClient { String Host; Socket soc; public DayClient(String phost) throws IOException{ this.Host=phost; } public void getDate() throws IOException{ soc= new Socket(Host,13); BufferedReader br = new BufferedReader(new InputStreamReader(soc.getInputStream())); System.out.println(br.readLine()); br.close(); soc.close(); } public static void main(String args[]) throws IOException{ DayClient dc= new DayClient(args[0]); dc.getDate(); } } To Run (in single system):  Compile both the programs.  Run the Server program first.  Open a new command window and run the client as follows:  Java DayClient localhost Lecture/core/networking/25 Page #7 feel the Technology…