Chapter 4 - Javanetworking
Chapter 4 - Javanetworking
University
Chapter 4
4. MAC Address
MAC (Media Access Control) Address is a unique
identifier of NIC (Network Interface Controller).
A network node can have multiple NIC but each
with unique MAC
5. Connection-oriented and connection-less
protocol
In connection-oriented protocol, acknowledgement is
sent by the receiver. So it is reliable but slow.
The example of connection-oriented protocol is TCP.
But, in connection-less protocol, acknowledgement is
not sent by the receiver. So it is not reliable but fast.
The example of connection-less protocol is UDP.
6. Socket
A socket is an endpoint between two way
communications.
A socket in Java is one endpoint of a two-way
communication link between two programs running on
the network.
InetAddress Class
Inet Address is used to encapsulate both the
numerical IP address and the domain name for that
address.
It can handle both IPv4 and Ipv6 addresses.
An InetAddress class method corresponds to a
DNS request
No public constructor
Three static methods:
InetAddress getByName(String)
o Static method used to retrieve the address for
the host name passed as the parameter.
InetAddress [ ] getAllByName(String)
o Static method used to retrieve all the addresses
for the host name passed as a parameter.
InetAddress getLocalHost( )
o Static method used to retrieve the address for
the current, or local host.
Cont…
import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.javatpoint.com"
);
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
} catch(Exception e){
System.out.println(e);
} }
}
Output Host Name:
www.javatpoint.com
IP Address: 206.51.231.148
Cont…
try
{ InetAddress fullname =
netAddress.getByName(“bigyellowcat.cs.binghamton
;
InetAddress alias =
InetAddress.getByName(“bigyellowcat"); InetAddre
octets = InetAddress.getByName(“128.226.121.44");
if (fullname.equals(alias) && fullname.equals(octe
// All is right with the world!
}
catch (UnknownHostException e)
{ // Exception handling here. }
TCP Sockets
Once a TCP socket connection is made, a virtual stream is
in place. Java’s IO model is that of a stream, therefore
the models are consistent; all you need to do connect a
TCP socket to a stream and read and write the streams as
normal
Sockets can be configured to act as a server and listen for
incoming messages, or connect to other applications as
a client. After both ends of a TCP/IP socket are connected,
communication is bi-directional.
Socket
is one endpoint of a two-way communication link
between two programs running on the network.
is bound to a port number so that the TCP layer can
identify the application that data is destined to be sent
is simply an endpoint for communications between the
machines.
Socket Class - TCP Client sockets
Socket(String ip, int port)
◦ Creates a streaming socket and binds it to the host and
port specified as parameters.
Socket(String ip, int port, boolean TCP or UDP)
◦ Creates a socket and binds it to the host and port
specified as parameters. The last parameter is used to
indicate whether the socket should be a stream or
datagram socket.
Socket(InetAddress ia, int port)
◦ Creates a streaming socket connected to the specified
host and port.
Socket(InetAddress ia, int port, boolean TCPorUDP)
◦ Creates a socket connected to the specified host and
port. The last parameter specifies whether the socket
should be a stream or datagram socket.
Client Sockets
Client sockets send messages to
server sockets by specifying the server endpoint
in a call to connect() and then sending data using
send() or write() .
When the client calls connect() , a connection is
pushed onto the server-side queue where it sits
until the server accepts the connection
Client Sockets methods
InetAddress getInetAddress( )
◦ Returns an InetAddress object representing the host for
this socket.
Int getPort( )
◦ Returns the port number on the local host for this socket.
InputStream getInputStream( ) or need Read()
Setter Methods:
void setBroadcast( boolean on)
static void setDatagramSocketImplFactory
(DatagramSocketImplFactory fac)
void serReceiveBufferSize(int size)
void setReuseAddress(boolean on)
void setSevdBufferSize(int size)
void setSoTimeout(int timeout)
void setTrafficClass(int tc)
Java URL class
A Class URL represents a Uniform Resource Locator,
which is a pointer to a “resource” on the World Wide Web.
For example:
https://www.javatpoint.com/java-tutorial
int hashCode( )
URLConnection openConnection( )
InputStream openStream( )
boolean sameFile(URL other)
String toExternalForm( )
String toString( )
boolean equals(Object obj)
Example of Java URL class
// File Name: URLDemo
import java.net.*;
public class URLDemo{
public static void main(String[] args){
try{
URL url=new URL("http://www.javatpoint.com/java-
tutorial");
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);}
}
}
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