Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/avinandanbose/javanetworking
This is all about Network Collection of Java . NET
https://github.com/avinandanbose/javanetworking
java network networking
Last synced: about 2 months ago
JSON representation
This is all about Network Collection of Java . NET
- Host: GitHub
- URL: https://github.com/avinandanbose/javanetworking
- Owner: AvinandanBose
- License: mit
- Created: 2022-12-09T10:38:53.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-30T19:06:19.000Z (about 2 years ago)
- Last Synced: 2023-03-05T19:23:28.334Z (almost 2 years ago)
- Topics: java, network, networking
- Language: Java
- Homepage:
- Size: 154 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JavaNetworking
This is all about Java Networking.JavaNetworking
The systems connected over a network through the internet connect to each other with the help of Transmission Control Protocol(TCP) or User Datagram Protocol(UDP). In Java, programming is done on the application layer that uses the concept of the classes in the java.net package , which provide networking functions to users.
InetAddress[Internet Addressing]
GetByName
GetAllByName
GetAddress
GetHostName
GetHostAddress
CanonicalHostName
GetLocalHost
GetLoopbackAddress
HashCode
isAnyLocalAddress
isLinkLocalAddress
isLinkLocalAddress
isMCGlobal
isMCLinkLocal
isMCNodeLocal
isMCOrgLocal
isMCSiteLocal
isMulticastAddress
isReachable
isSiteLocalAddress
toString
hashCode
```Syntax
Determines the IP address of a host, given the host's name.
```
```Syntax
Given the name of a host, returns an array of its IP addresses,
based on the configured name service on the system.
```
```Syntax
Returns the raw IP address of InetAddress object.
As it returns byte value hence we have to do AND
Operation with 0xff(Hex)= 255 (1111 1111), to get actual value.
```
```Syntax
Gets the host name for the IP address.
```
```Syntax
Returns the IP address string in textual presentation.
```
```Syntax
Gets the fully qualified domain name for the IP address.
```
```Syntax
Returns the address of the local host.
This is achieved by retrieving the name of the host from the system,
then resolving that name into an InetAddress.
```
```Syntax
Returns the loopback address.
The InetAddress returned will represent the IPv4 loopback address, 127.0.0.1,
or the IPv6 loopback address, ::1.
Note: The IP address 127.0. 0.1 is called a loopback address.
Packets sent to this address never reach the network ,
but are looped through the network interface card only.
This can be used for diagnostic purposes to verify,
that the internal path through the TCP/IP protocols is working.
```
```Syntax
Returns a hashcode for this IP address.
```
```Syntax
Utility routine to check if the InetAddress is a wildcard address.
Wild Card Address is a special local IP address.
i.e.
For Net Mask : 255.255.255.255 → Wild Card Mask : 0.0.0.0
For Net Mask : 255.255.255.254 → Wild Card Mask : 0.0.0.1
For Net Mask : 255.255.255.252 → Wild Card Mask : 0.0.0.3
For Net Mask : 255.255.255.248 → Wild Card Mask : 0.0.0.7
.....
For Net Mask : 0.0.0.0 → Wild Card Mask : 255.255.255.255
Note :
Formula =
Starting Value is Always : 255.255.255.255
(Substract)
Subnet Mask = Wild Card Mask
i.e.
255.255.255.255 - Subnet Mask = Wild Card Mask
A wildcard mask is a mask of bits that indicates ,
which parts of an IP address are available for examination.
A wildcard mask can be thought of as an inverted subnet mask.
i.e.
(Subnet Mask)255.255.255.0 → 0.0.0.255(Wild Card Mask)
A wild card mask is a matching rule in which:
0 means that the equivalent bit must match(Care)and
1 means that the equivalent bit does not matter(Don't Care).
```
```Syntax
Utility routine to check if the InetAddress is an link local address.
Linkink Local Address: A link-local address is an automated selected,
IPv6 unicast( one-to-one transmission ) network address ,
that is valid only for communications within the subnetwork that the host is connected to.
A link-local address is required on each physical interface.
Link-local addresses are designed to be used for addressing on a single link,
for purposes such as automatic address configuration, neighbor discovery,
or in the absence of routers.
It also may be used to communicate with other nodes on the same link.
A link-local address is automatically assigned.
A link-local address is the IP address that is to be used for communication ,
within a logical division of the network or in the broadcast domain ,
to which the host is connected.
Range of Link Local Ip Address : 169.254.x.x
where x ranges from : 0 - 255
i.e. The Range is between : 169.254.0.0 -
169.254.255.255
Each computer randomly select an IP address in a given Range.
And then communicate to other Computer searching for the same ,
IP address through ARP(Address Resolution Protocol (ARP)),
if not found then the automated selected IP belongs to the
Computer.
Note: A link-local address is an IPv6 unicast address ,
that can be automatically configured on any interface .
```
```Syntax
Utility routine to check if the InetAddress is a loopback address.
```
```Syntax
Utility routine to check if the multicast address has global scope.
Multicast: In a Multicast Transmission,
All stations recieves the frame, the stations that
are members of the group keeps and handles the frame.
The protocol that is used
Internet Group Management Protocol .
It uses Class D of IP address , where
1110 - these 4 bits are fixed in Octet
and other 28 bits can be used in maximum
of 2^28 bits . Therefore 1st Octet will
range from : 1110 0000 - 1110 1111
that is 224-239. And least significant
bit of the of the first byte in a destination
address is always 1 in Multicasting,
where as in Unicast it is 0.
```
```Syntax
Utility routine to check if the multicast address has link scope.
```
```Syntax
Utility routine to check if the multicast address has node scope.
```
```Syntax
Utility routine to check if the multicast address has organization scope.
```
```Syntax
Utility routine to check if the multicast address has site scope.
```
```Syntax
Utility routine to check if the InetAddress is an IP multicast address.
```
```Syntax
Test whether that address is reachable.
Best effort is made by the implementation to try to reach the host,
but firewalls and server configuration may block requests resulting,
in a unreachable status while some specific ports may be accessible.
A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained,
otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
The timeout value, in milliseconds, indicates the maximum amount of time the try should take.
If the operation times out before getting an answer, the host is deemed unreachable.
A negative value will result in an IllegalArgumentException being thrown.
```
```Syntax
Utility routine to check if the InetAddress is a site local address.
```
```Syntax
Converts this IP address to a String. The string returned is of the form: hostname / literal IP address.
```
```Syntax
Returns a hashcode for the given IP address.
```
Q 1) Lets Look at a program which prompts the user for a hostname and then it looks up the IP address for the hostname and displays the result. Once done , it asks the user if he or she wants to look up another host:
Q 2)Printing address of Host of Local Device(local host ) and Host of an website.
Socket Class
Creation of Unconnected Socket-1
Creation of Unconnected Socket -2
Connecting InetAddress Through a Port-Constructor1
Connecting InetAddress Through a Port-Constructor2
Connecting InetAddress Through a Port-Constructor3
Connecting String Address Through a Port-Constructor4
Connecting String Address Through a Port-Constructor5
Connecting String Address Through a Port-Constructor6
Connecting Socket Through a Proxy and Input String Address -Constructor7
Connecting Socket Through a Proxy and Input Inet Address -Constructor8
Connecting InetAddress Through a Port-Other Types-1
Connecting InetAddress Through a Port-Other Types-2
Void Bind(SocketAddress bindpoint)
Void Close()
Void Connect(Socket endpoint)
Void Connect(Socket endpoint, int timeout)
SocketChannel getChannel
InetAddress getInetAddress
InputStream getInputStream
SetKeepAlive and GetKeepAlive
InetAddress getLocalAddress
int getLocalPort()
SocketAddress getLocalSocketAddress
OutputStream getOutputStream
boolean getOOBInline
int getPort()
int getRecieveBufferSize()
SocketAddress getRemoteSocketAddress()
boolean getReuseAddress()
int getSendBufferSize
int getSoLinger()
int getSoTimeout()
boolean getTcpNoDelay()
int getTrafficClass()
boolean isBound()
boolean isClosed()
boolean isConnected()
1. Constructors of Socket Class
Socket() constructor {Creating UnConnected Socket}
```Syntax
import java.net.Socket → Socket Package
Socket s = new Socket(); → Creating an instance of Socket
```
```Syntax
import java.net.Socket → Socket Package
new Socket(); → Anonymously Creating an instance of Socket
```
Using Inet in Constructor
```Syntax
import java.net.Socket → Socket Package
import java.net.InetAddress;
InetAddress address = InetAddress.getByName(host);
Socket s = new Socket(address.getHostAddress(), int port);
Or
Socket s = new Socket(address.getHostName(), int port);
```
```Syntax
import java.net.Socket → Socket Package
import java.net.InetAddress;
static Socket s;
InetAddress address = InetAddress.getByName(host);
InetAddress localAdress = InetAddress.getLocalHost();
s = new Socket(address.getHostAddress(), int port, localAdress, localPort);
Or
s = new Socket(address.getHostName(), int port, localAdress, localPort);
```
```Syntax
import java.net.Socket → Socket Package
import java.net.InetAddress;
static Socket s;
InetAddress address = InetAddress.getByName(host);
InetAddress localAdress = InetAddress.getLocalHost();
s = new Socket(address.getHostAddress(), int port, boolean);
Or
s = new Socket(address.getHostName(), int port, localAdress, boolean);
```
Using String in Constructor
```Syntax
import java.net.Socket → Socket Package
static Socket s;
String address = hostname;
socket = new Socket(address, 80);
// socket = new Socket(hostname, int port);
```
```Syntax
import java.net.Socket → Socket Package
import java.net.InetAddress;
static Socket s;
String address = hostname;
InetAddress localAdress = InetAddress.getLocalHost();
socket = new Socket(address, int port, localAdress, int localPort);
// socket = new Socket( hostname, int port,localAdress, int localPort);
```
```Syntax
import java.net.Socket → Socket Package
static Socket s;
String address = hostname;
socket = new Socket(address, int port, boolean);
// socket = new Socket( hostname, int port, boolean);
```
Using Proxy in Constructor
```Syntax
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
SocketAddress address = new InetSocketAddress(hostname, int port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
Socket s = new Socket(proxy);
Proxy socksProxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(hostname, int port));
Socket s1 = new Socket(socksProxy);
```
```Syntax
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetAddress;
InetAddress address = InetAddress.getByName(hostname);
SocketAddress address = new InetSocketAddress(address, int port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
Socket s = new Socket(proxy);
Proxy socksProxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(hostname, intport));
Socket s1 = new Socket(socksProxy);
```
In Details
Constructors
Does this
Socket()
It creates an unconnected socket with the system-default of SocketImpl
Socket(InetAddress address, int port)
It is creates a stream socket and connects it to the specified port number at the specified IP address.
Socket(InetAddress address, int port, boolean stream)
It is depreciated and it uses DatagramSocket instead of UDP transport.
Socket(InetAddress address, int port, InetAddress localAddr, int localport)
It creates a socket and connects it to the specified remote address of specified remote port.
Socket(Proxy proxy)
It creates an unconnected socket, specifying the type of proxy that should be used regardless of any other settings.
Socket(String address, int port)
It is creates a stream socket and connects it to the specified port number on the named host.
Socket(String address, int port, boolean stream)
It is depreciated and it uses DatagramSocket instead of UDP transport.
Socket(InetAddress address, int port, InetAddress localAddr, int localport)
It creates a socket and connects it to the specified remote host of specified remote port.
Other types of Inet And Socket connections:
```Syntax
import java.net.Socket → Socket Package
import java.net.InetAddress;
InetAddress address = InetAddress.getByName(host);
Socket s = new Socket();
s.connect( new java.net.InetSocketAddress(address.getHostAddress(), int port));
Or
s.connect( new java.net.InetSocketAddress(address.getHostName(), int port));
```
```Syntax
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetAddress;
static Socket socket = new Socket();
InetAddress address = InetAddress.getByName(host);
SocketAddress addr = new InetSocketAddress(address.getHostAddress(), int port);
Or
SocketAddress addr = new InetSocketAddress(address.getHostName(), int port);
And then:
socket.connect(addr);
```
Note : Same thing can be attained by String .
2. Methods of Socket Class
```Syntax
It binds socket to a local address and port number.
```
```Syntax
It closes the socket.
```
```Syntax
It connects this socket to the server.
```
```Syntax
It connects this socket to the server with a specified timeout value.
```
```Syntax
It returns the unique SocketChannel object associated with the socket if any.
:Note:
The Java NIO SocketChannel is used for connecting a channel with a TCP
(Transmission Control Protocol) network socket.
It is equivalent to Java Networking Sockets used in network programming.
It is provided by: java.nio package .
NIO represents New Input/Output , where as,
IO represents Input/Output of java.io package.
Java.NIO package have Channels provided by
java.nio.channels interface and one of the Channels is:
SocketChannel which is a Class.
```
```Syntax
It returns the address to which the socket is connected.
```
```Syntax
It returns an input stream for this socket.
```
```Syntax
SetKeepAlive: If Set to True, then SO_KEEPALIVE is enabled
GetKeepALive: If SetKeepAlive is true , then GetKeepAlive returns True else false.
```
```Syntax
It gets the local address to which the socket is bound.
```
```Syntax
It returns the local port to which the current socket is bound .
```
```Syntax
It returns the address of the endpoint to which the socket is bound,
or null if it is not bound yet.
```
```Syntax
It returns the outputstream for the connected socket.
```
```Syntax
The setOOBInline() method of Java Socket class enables
or disables the SO_OOBInline. By default, the SO_OOBInline option is disabled.
```
```Syntax
It returns the remote port to which the socket is connected to.
```
```Syntax
It gets the value of the SO_RCVBUF option for this socket, that is,
the buffer size used by the platform for the input on this Socket.
```
```Syntax
Returns the address of the endpoint this socket is connected to,
or null if it is unconnected.
If the socket was connected prior to being closed,
then this method will continue to return the connected address,
after the socket is closed.
```
```Syntax
Tests if SO_REUSEADDR is enabled.
```
```Syntax
Get value of the SO_SNDBUF option for this Socket,
that is the buffer size used by the platform for output on this Socket.
```
```Syntax
Returns setting for SO_LINGER. -1 returns implies that the option is disabled.
The setting only affects socket close.
```
```Syntax
Returns setting for SO_TIMEOUT. 0 returns implies that the option is disabled
(i.e., timeout of infinity).
```
```Syntax
Tests if TCP_NODELAY is enabled.
```
```Syntax
Gets traffic class or type-of-service in the IP header for packets sent from this Socket
```
```Syntax
Returns the binding state of the socket.
Returns true if the socket was successfuly bound to an address.
```
```Syntax
Returns the closed state of the socket.
Returns true if the socket has been closed.
```
```Syntax
Returns the connection state of the socket.
Note: Closing a socket doesn't clear its connection state,
which means this method will return true for a closed socket (see isClosed())
if it was successfuly connected prior to being closed.
```