Ajp Notes Chap4
Ajp Notes Chap4
Networking(10M)
1. sharing resources
2. centralize software management
Networking Terminology
The widely used java networking terminologies are given below:
1. IP Address
2. Protocol
3. Port Number
4. MAC Address
5. Connection-oriented and connection-less protocol
6. Socket
1) IP Address
2) Protocol
● TCP/UDP
● FTP
● Telnet
● SMTP
● POP etc.
3) Port Number
●
● TCP/IP reserves the lower 1,024 ports for specific protocols.
21 is for FTP,
23 is for Telnet,
25 is for e-mail,
79 is for finger,
80 is for HTTP,
119 is for netnews
4) MAC Address
6) Proxy Server
Proxy server is an intermediary server between client and the internet. Proxy
servers offers the following basic functionalities:
Proxy servers allow to hide, conceal and make your network id anonymous by
hiding your IP address.
7) Domain Naming Service /system(DNS).
The internet world is completely based on IP (Internet Protocol) address.To access
any website you need to know its IP address which is a long numeric code and
is not possible to learn.
Now, here comes the role of DNS. A DNS is an internet service that translates a
domain name into corresponding IP address.
Domain name used here is alphabetic and can be easily remembered.
For example, www.example.com is a domain name of a site. And with the help
of DNS it will get translate into its IP address 198.105.232.4.
8) Socket
● Authenticator
● CacheRequest
● CacheResponse
● ContentHandler
● CookieHandler
● CookieManager
● DatagramPacket
● DatagramSocket
● DatagramSocketImpl
● InterfaceAddress
● JarURLConnection
● MulticastSocket
● InetSocketAddress
● InetAddress
● Inet4Address
● Inet6Address
● IDN
● HttpURLConnection
● HttpCookie
● NetPermission
● NetworkInterface
● PasswordAuthentication
● Proxy
● ProxySelector
● ResponseCache
● SecureCacheResponse
● ServerSocket
● Socket
● SocketAddress
● SocketImpl
● SocketPermission
● StandardSocketOptions
● URI
● URL
● URLClassLoader
● URLConnection
● URLDecoder
● URLEncoder
● URLStreamHandler
InetAddress
Whether you are making a phone call, sending mail, or establishing a connection
across the Internet, addresses are fundamental. The InetAddress class is used to
encapsulate both the numerical IP address and the domain name for that address.
domain name
InetAddress
IP address
Factory Methods
The InetAddress class has no visible constructors. To create an
InetAddress object,you have to use one of the available factory methods
getLocalHost( )
getByName(String hostName)
getAllByName(String hostName)
If these methods are unable to resolve the host name, they throw an
UnknownHostException.
The getByName( ) method returns an InetAddress for a host name passed to it.
import java.net.*;
class InetAddressTest
{
InetAddress address = InetAddress.getLocalHost(); Acer-PC/103.31.147.90
System.out.println(address);
yahoo.com/206.190.36.45
address = InetAddress.getByName("yahoo.com");
System.out.println(address);
System.out.println(SW[i]);
www.gmail.com/74.125.236.117
www.gmail.com/74.125.236.118
www.gmail.com/2404:6800:4009:800:0:0:0:1015
}
Acer-PC/103.31.147.90
yahoo.com/206.190.36.45
www.gmail.com/74.125.236.117
www.gmail.com/74.125.236.118
www.gmail.com/2404:6800:4009:800:0:0:0:1015
*/
● There are two kinds of TCP sockets in Java. One is for servers,
and the other is for clients.
● ServerSocket has a method called accept( ),which is a blocking call that will wait for
a client to initiate communications,
//TCPIP Server client Communication
Server Program
import java.net.*;
import java.io.*;
ServerSocket ss=null;
try
ss=new ServerSocket(132);
catch(IOException E)
while (true)
try
Socket s=ss.accept();
OutputStream os=s.getOutputStream();
bw.close();
s.close();
catch(IOException e)
Client Program
import java.net.*;
import java.io.*;
try
InputStream is=s1.getInputStream();
br.close();
s1.close();
catch(Exception e)
{}
Server Window
Client Window
Here are two constructors used to create client sockets:
ServerSocket(int port)
ServerSocket(int port, int maxQueue)
1)Server program
import java.net.*;
import java.io.*;
public class chatserver
Socket sk=ss.accept();
String s;
while ( true )
s=cin.readLine();
if (s.equalsIgnoreCase("END"))
cout.println("BYE");
break;
System.out.print("Client :"+s+"\n");
System.out.print("Server: ");
s=stdin.readLine();
cout.println(s);
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
2) Client Program
import java.net.*;
import java.io.*;
public class chatclient
while ( true )
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"\n");
if ( s.equalsIgnoreCase("BYE") )
break;
sk.close();
sin.close();
sout.close();
stdin.close();
Whois
URL
For example:
1. https://www.google.com/index.html
A URL contains many information(components of URL):
// Demonstrate URL.
import java.net.*;
class URLDemo
System.out.println("Ext:" + hp.toExternalForm());
output:
Protocol: http
Port: -1
Host: www.google.com
File: /downloads
Ext:http://www.google.com/downloads
● The next two forms of the constructor allow you to break up the
URL into its component parts:
URL(String protocolName, String hostName, int port, String path)
URL(String protocolName, String hostName, String path)
URL(URL urlObj, String urlSpecifier)
● URL class has several constructors, and each can throw
a MalformedURLException
URLConnection
URLConnection is a general-purpose class for accessing the attributes of a remote
resource.
import java.net.*;
import java.io.*;
import java.util.Date;
class UCDemo
int c;
// get date
long d = hpCon.getDate();
if(d==0)
System.out.println("No date information.");
else
d = hpCon.getExpiration();
if(d==0)
d = hpCon.getLastModified();
if(d==0)
if(len == -1)
/*
E:\imp\co6c\chapter2>java UCDemo
No last-modified information.
Content-Length: 352
*/
Datagrams(UDP)
//
Demonstrate Datagrams.
import java.net.*;
class WriteServer
int pos=0;
while (true)
int c = System.in.read();
switch (c)
quit"); return;
case '\n':
ds.send(new DatagramPacket(buffer,pos,
InetAddress.getLocalHost(),clientPort));
pos=0;
break;
default:
buffer[pos++] = (byte) c;
}
public static void theClient() throws Exception
while(true)
if(args.length == 1)
ds = new DatagramSocket(serverPort);
theServer();
else {
ds = new DatagramSocket(clientPort);
theClient();
}