320 likes | 369 Views
A. M. JAIN COLLEGE. SHIFT – II DEPARTMENT OF BCA SECOND YEAR ‘A’ SECTION. Java Programming. TOPICS on “ NETWORK BASICS IN JAVA ”. Presented by M. Ganesh Raja Assistant Professor. What is network? Java Networking is a concept of connecting two or more computing devices.
E N D
A. M. JAIN COLLEGE SHIFT – II DEPARTMENT OF BCA SECOND YEAR ‘A’ SECTION
TOPICS on “NETWORK BASICS IN JAVA” Presented by M. Ganesh Raja Assistant Professor
What is network?Java Networking is a concept of connecting two or more computing devices. • Internet is a network of networks. • Advantage of Java Networking • sharing resources • centralize software management
Network Layers • Application Level • (FTP, Telnet, etc.) • Network Layer • (Low-level Protocol -- IP, datagrams, etc.) • Hardware Layer • (Ethernet, TokenRing, X.25, etc.)
All the Java networking classes and interfaces use java.net package.The java.net package provides the functionality for two common protocols:1. TCP (Transmission Control Protocol)2. UDP (User Datagram Protocol)
TCP-connection oriented • TCP is a connection based protocol that provides a reliable flow of data between two devices. UDP-connection less • UDP protocol sends independent packets of data, called datagram from one computer to another. Transport Layer • (TCP, UDP, sockets, etc.)
Networking is the concept of connecting multiple remote or local devices together. Java program communicates over the network at application layer.
Request and ResponseWhen an input data is sent to an application via network, it is called request.The output data coming out from the application back to the client program is called response. Networking Terminology
Protocol A protocol is basically a set of rules and guidelines which provides the instructions to send request and receive response over the network. For example: TCP, UDP, SMTP, FTP etc.
IP Address IP Address stands for Internet protocol address. It is an identification number that is assigned to a node of a computer in the network.For example: 192.168.2.01Range of the IP Address0.0.0.0 to 255.255.255.255
Port Number The port number is an identification number of server software. The port number is unique for different applications. It is a 32-bit positive integer number having between ranges 0 to 65535.
Java Socket ProgrammingSocket provides an endpoint of two way communication link using TCP protocol.
java Socket programming is used for communication between the applications running on different JRE. • Java Socket programming can be connection-oriented or connection-less.Socket and ServerSocket classes are used for connection-oriented socket programming. • DatagramSocket and DatagramPacket classes are used for connection-less socket programming.The client in socket programming support for two types.1. IP Address of Server2. Port number.
Socket Class Thejava.net.Socket class is used to create a socket so that both the client and the server can communicate with each other easily.Asocket is an endpoint for communication between two computers.
InetAddress Class • The Inetaddress class provides you with a limited interface to DNS for doing both forward and reverse internet address lookups • An InetAddress class method corresponds to a DNS request
InetAddress Class • No public constructor • Three static methods: • InetAddress getByName(String) • Static method used to retrieve the address for the host name passed as the parameter. • InetAddress [ ] getAllByName(String) • Static method used to retrieve all the addresses for the host name passed as a parameter. • InetAddress getLocalHost( ) • Static method used to retrieve the address for the current, or local, host.
InetAddress Class • Three additional “getter” methods • String getHostName( ) • Returns the host name. • byte[ ] getAddress( ) • Returns the IP address. • String getHostAddress( ) • Returns the IP address as a string.
DatagramPacket Class • DatagramPacket(byte[] buf, int len, SocketAddress addr) • Constructs a datagram packet for sending packets of length len to the specified port number on the specified host. • Getter methods • getAddress( ) • getData( ) • getLength( ) • getOffset( ) • getPort( ) • getSocketAddress( ) • Setter methods • setAddress(InetAddressiaddr) • setData(byte[ ] buf) • setData(byte[] buf, int offset, int length) • setLength(intlen) • setPort(int port) • setSocketAddress(SocketAddresssaddr)
URL Class URL class represents aUniform Resource Locator, a pointer to a "resource" on the World Wide Web. • different services use slightly different formats • file://ftp.yoyodyne.com/pub/files/foobar.txt • http://www.yahoo.com/index.html • ftp://useracct@someftpserver.com • news:rec.gardening • gopher://gopher.banzai.edu:1234/
URL Class - Constructors • URL(String spec) • Creates a URL object from the String representation. • URL(String protocol, String host, int port, String file) • Creates a URL object from the specified protocol, host, port number, and file. • URL(String protocol, String host, int port, String file, URLStreamHandler handler) • Creates a URL object from the specified protocol, host, port number, file, and handler. • URL(String protocol, String host, String file) • Creates a URL from the specified protocol name, host name, and file name. • URL(URL context, String spec) • Creates a URL by parsing the given spec within a specified context • URL(URL context, String spec, URLStreamHandler handler) • Creates a URL by parsing the given spec with the specified handler within a specified context.
URL Class - Methods Setters set(String protocol, String host, int port, String file, String ref) set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
ServerSocket • Used as the main connection point for some service you wish to provide. • Once created, it listens for connection requests then queues the request for disposition • On Unix/Linux you must be root to use
ServerSocket – methods • accept( ) – accepts a connection request and creates a socket to the remote user • close( ) – close the server socket and wait for next connection request