CH No 4 Networking Basics13124
CH No 4 Networking Basics13124
BASICS
10 MARKS
COURSE OUTCOME
Develop a java programs using networking
concepts
JAVA NETWORKING
Java Networking is a concept of connecting
two or more computing devices together so
that we can share resources.
Java socket programming provides facility to
sharing resources
centralize software management
JAVA NETWORKING TERMINOLOGY
Server
A Server is a machine that has some
resources that can be shared. For example, a
computing server, which provides computing
capability; a print server which provides
network printing facility, a disk server which
provide networked disk space; and web server
which stores web pages.
Client
A Client is simply any machine that wants to
use the services of a particular server. The
server is a permanently available resource,
while the client is free to disconnect after its
job is done.
Client server communication.
IP Address
IP address is a unique number assigned to
a node of a network e.g. 192.168.0.1 . It is
composed of octets that range from 0 to 255.
It is a logical address that can be changed.
Protocol
In networking, a protocol is a set of rules for
TCP
FTP
Telnet
SMTP
POP etc.
PORT NUMBER
oriented or connection-less.
Socket and ServerSocket classes are used for
ServerSocket, Datagrampacket,
DatagramSocket
Important Interface
SocketOption, SocketImplFactory,
CookiePolicy
URL CLASS
Java URL Class present in java.net package,
deals with URL (Uniform Resource Locator)
which uniquely identify or locate resources
on internet.
Ex
A URL contains many information:
Protocol: In this case, http is the protocol.
write http//www.studyto
night.com:80/index.html/ , 80 is the port
number. If port number is not mentioned in
the URL, it returns -1.
File Name or directory name: In this case,
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}catch(Exception e)
{
System.out.println(e);}
}
}
JAVA URLCONNECTION CLASS
The Java URLConnection class represents a
communication link between the URL and the
application.
It can be used to read and write data to the
addresses.
Inet Address class has no visible constructor. To
wnHostException
static InetAddress getByName (String hostname)
throws UnknownHostException
static InetAddress[ ] getAllByName (String
isMulticastAddress()
public String toString()
import java.net.*;
import java.util.*;
InetAddress ia = InetAddress.getByName(host);
System.out.println("IP address: " + ia.getHostAddress());
System.out.println("Host name : " + ia.getHostName());
System.out.println("Host name and IP address: " + ia.toString());
}
}
JAVA SOCKET PROGRAMMING
oriented or connection-less.
Socket and ServerSocket classes are used for
server.
Constructor
Socket (String hostname, int port)
Socket(InetAddress ipaddr, int port)
METHODS OF THE SOCKET CLASS
InetAddress getInetAddress() -- Returns the InetAddress that is
associated with the socket object.
ServerSocket(int port)
ServerSocket(int port, int maxQ)
ServerSocket(int port, int maxQ, InetAddress
address)
METHODS
Public socket accept() -- Accepts an incoming
connection.
Public int getLocalPort() -- Returns the port
socket.
public void bind(SocketAddress host)
DatagramSocket() throws
SocketException: it creates a datagram
socket and binds it with the available Port
Number on the localhost machine.
DatagramSocket(int port) throws
socket.
public InetAddress getLocalAddress():Gets