NetWorking(URL , SOCKET(TCP/IP),DATAGRAMS)- I
Java supports Networking and its creators have called java as "programming for
the Internet".
Types of Network
Programming
Two general types are:
·
Connection-Oriented Programming
·
Connectionless Programming
Connection-Oriented
Networking
The Client and Server have a communication link that is open and active from the time the application is executed until it is closed.Using Internet Jargon,the Transmission control protocol is a connection oriented protocol.It is reliable connection -packets are guaranteed to arrive in the order they are sent
Connection-less Oriented Networking
The this type each instance that packets are sent ,they are transmitted
individually. No link to the receiver is maintained after the packets arrive.
The Internet equivalent is the user Datagram Protocol(UDP).
Connectionless communication is faster but not reliable. Datagrams are used to
implement a connectionless protocol,suchas UDP.
SocketOverview
A network socket is a lot like an electrical socket.
Internet Protocol(IP) is a Low-Level routing Protocol that breaks data
into small packets and sends them to an address across a network,which does not
guarantee to deliver said packets to the desination.Transmission Control
Protocol(TCP) is a higher-level protocol that manages to robustly string
together these packets,sorting and retransmitting them as necessary to reliably
transmit your data.A third protocol, user Datagram Protocol(UDP),sits next
to TCP and can be used directly to support fast,connectionless,unreliable
transport of packets.
Client/Server
A server is anything that has some resource that can be shared.there are
compute servers,which provide networked disk spaces;and web servers,which store
web page.A client is simply any other entity that wants to gain access to a
particular server.the interaction between client and server is just like
interaction between a lamp and an electrical socket.
Reserved Sockets
Once Connected,a higher-level protocol ensues,which is dependent on which
port you are using.TCP/IP reserves the lower 1024 ports for specific protocols.
Proxy Servers
A proxy server speaks the client side of a protocol to another server.
Internet Addressing
Every computer on the Internet has an address.An Internet Address is a
number that uniquely identifies each computer on the net.Originally,all
Internet address consisted of 32-bit values
Domain Naming Service(DNS)
The Internet wouldn't be a very friendly place to navigate if everyone
had to refer to their address as numbers.Forexample,it is difficult to imagine
seeing "http://192.9.9.1/" at the bottom of an
advertisement.Thankfully,a clearing house exists for a parallel hierarchy of
names to go with all these numbers. It is called the
DomainNamingService(DNS).just as the four numbers of an IP address describe a
network hierarchy from left to right,the name of an Internet address,called its
domain name ,describes a machine's location in a name space,from right to left
For example,www.osborne.com is in the COM domain(reserved for U.S.commercial
sites),it is called Osborne (after the company name), and www is the name of
the specific computer that is Osborne's web server.www corresponds to the
rightmost number in the equivalent IP address.
JAVA AND THE NET
Inet Address Class
The InetAddress class is used to encapsulate both the numerical IP
address and the domain name for that address. you interact with this class by
using the name of an IP host,which is more convenient and understandable than
its IP address. the Inet Address class hides the number inside.
METHODS
static InetAddress getLocalHost()throws unknown HostException.
static InetAddress getByName(StringhostName)throws unknown
HostException.
static InetAddress[]
getAllbynames(StringhostName)throws unknown HostException.
TCP/IPClientSockets
TCP/IP sockets are used to implement
reliable,bidirectional,persistent,point-to-point, stream-based connection
between hosts on the Internet.A sockets can be used to connect java's I/O
system to other program that may reside either on the local machine or on any
other machine on the internet.
Socket(StringhostName,int port)->creates a socket connecting the local
host to the named host and port;can throw an UnknownHostException or an IOException.
Socket(InetAddressipAddress.intport)
->creates a socket using a preexisting InetAddress Object and a
port;can throw an IOException.
A socket can be examined at any times for the address and the port
information associated with it by use of the following methods:
InetAddress getInetAddress()->Returns the InetAddress associated with
the socket object .
intgetPort()->Return the remote port to which this socket object is
connected.
intgetPort()->Returns the local port to which this socket object is
connected.
The serversocket class is used to create serves that listen for either
local or remote client programs to connect to them on Published ports.
serversockets are quit different
from normal sockets.when can you create a serversocket,it will register itself
with the system as having an interest in client connections.
Constructors
serversocket(int port)->creates server socket on the specified port
with a queue length of 50.
serversocket(int port,int maxQueue)
->creates server socket on the specified port with a maximum queue
length of maxQueue.
serversocket(intport,int maxQueue,InetAddresslocalAddress)
->creates server socket on the specified port with a maximum Queue
length of maxQueue.on a multihomed host,localAddress specifies the IP address
to which this socket binds.
INetAddress Class
Program:
File name: Ip.java
import
java.net.*;
import
java.io.*;
public
class Ip
{
public
static void main(String[] args)throws IOException
{
String
hostname="";
try
{
InetAddress
ipaddress=InetAddress.getByName(hostname);
System.out.println("IP
Address:"+ipaddress.getHostAddress());
}
catch(UnknownHostException
e){
System.out.println("Could
not find IP address for:"+hostname);
}
}
}
Output
Constructors
Server Program
File name: Server.java
import
java.net.*;
import
java.lang.*;
import
java.io.*;
public
class Server{
public
static final int PORT=1025;
public
static void main(String args[])
{
ServerSocket
sersock=null;
Socket
sock=null;
System.out.println("
Wait !! ");
try{
sersock=new
ServerSocket(PORT);
int
number;
System.out.println("Server
Started :"+sersock);
sock=sersock.accept();
System.out.println("Client
Connected:"+sock);
DataInputStream
ins=new DataInputStream(sock.getInputStream());
String
clientMsg=new String(ins.readUTF());
System.out.println(clientMsg);
DataOutputStream
dos=new DataOutputStream(sock.getOutputStream());
dos.writeUTF("Hello
from Server");
dos.close();
sock.close();
}
catch(SocketException
se)
{
System.out.println("Server
Socket Problem"+se.getMessage());
}
catch(Exception
e){
System.out.println("Couldn't
start"+e.getMessage());
}
System.out.println("Connection
from:"+sock.getInetAddress());
}
}
Client Program
File name: Client.java
import
java.lang.*;
import
java.io.*;
import
java.net.*;
import
java.net.InetAddress;
class
Client{
public
static void main(String args[]){
Socket
sock=null;
DataInputStream
dis=null;
DataOutputStream
dos=null;
System.out.println("Trying
to Connect");
try{
InetAddress
ip=InetAddress.getByName("localhost");
sock=new
Socket(ip,Server.PORT);
dos=new
DataOutputStream(sock.getOutputStream());
dos.writeUTF("Hi
from Client");
DataInputStream
is=new DataInputStream(sock.getInputStream());
String
serverMsg=new String(is.readUTF());
System.out.println(serverMsg);
}
catch(SocketException
e){
System.out.println("SocketException
"+e);
}
catch(IOException
e){
System.out.println("IOException
"+e);
}
finally{
try{
sock.close();
}
catch(IOException
ie){
System.out.println("Close
Error: "+ie.getMessage());
}
}
}
}
Open Two CMD. Run One Cmd
Server , and another Cmd Client.First Run Server.After Run Client Program.
Output
Before connecting
Client , Server Output
After connecting
Client , Server and Client Output
0 comments:
Post a Comment