Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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

  • ```Syntax
    Determines the IP address of a host, given the host's name.
    ```

  • GetAllByName

  • ```Syntax
    Given the name of a host, returns an array of its IP addresses,
    based on the configured name service on the system.
    ```

  • GetAddress

  • ```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.
    ```

  • GetHostName

  • ```Syntax
    Gets the host name for the IP address.
    ```

  • GetHostAddress

  • ```Syntax
    Returns the IP address string in textual presentation.
    ```

  • CanonicalHostName

  • ```Syntax
    Gets the fully qualified domain name for the IP address.
    ```

  • GetLocalHost

  • ```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.
    ```

  • GetLoopbackAddress

  • ```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.
    ```

  • HashCode

  • ```Syntax
    Returns a hashcode for this IP address.
    ```

  • isAnyLocalAddress

  • ```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).
    ```

  • isLinkLocalAddress

  • ```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 .

    ```

  • isLinkLocalAddress

  • ```Syntax

    Utility routine to check if the InetAddress is a loopback address.

    ```

  • isMCGlobal

  • ```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.

    ```

  • isMCLinkLocal

  • ```Syntax

    Utility routine to check if the multicast address has link scope.

    ```

  • isMCNodeLocal

  • ```Syntax

    Utility routine to check if the multicast address has node scope.

    ```

  • isMCOrgLocal

  • ```Syntax

    Utility routine to check if the multicast address has organization scope.

    ```

  • isMCSiteLocal

  • ```Syntax

    Utility routine to check if the multicast address has site scope.

    ```

  • isMulticastAddress

  • ```Syntax

    Utility routine to check if the InetAddress is an IP multicast address.

    ```

  • isReachable

  • ```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.

    ```

  • isSiteLocalAddress

  • ```Syntax

    Utility routine to check if the InetAddress is a site local address.

    ```

  • toString

  • ```Syntax

    Converts this IP address to a String. The string returned is of the form: hostname / literal IP address.

    ```

  • hashCode

  • ```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

    1. Constructors of Socket Class


      Socket() constructor {Creating UnConnected Socket}


    • Creation of Unconnected Socket-1



    • ```Syntax

      import java.net.Socket → Socket Package

      Socket s = new Socket(); → Creating an instance of Socket
      ```

    • Creation of Unconnected Socket -2




    • ```Syntax

      import java.net.Socket → Socket Package

      new Socket(); → Anonymously Creating an instance of Socket
      ```

      Using Inet in Constructor



    • Connecting InetAddress Through a Port-Constructor1



    • ```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);
      ```



    • Connecting InetAddress Through a Port-Constructor2



    • ```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);

      ```

    • Connecting InetAddress Through a Port-Constructor3



    • ```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



    • Connecting String Address Through a Port-Constructor4



    • ```Syntax

      import java.net.Socket → Socket Package

      static Socket s;

      String address = hostname;
      socket = new Socket(address, 80);

      // socket = new Socket(hostname, int port);

      ```

    • Connecting String Address Through a Port-Constructor5



    • ```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);

      ```

    • Connecting String Address Through a Port-Constructor6



    • ```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



    • Connecting Socket Through a Proxy and Input String Address -Constructor7





    • ```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);

      ```

    • Connecting Socket Through a Proxy and Input Inet Address -Constructor8



    • ```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:

    • Connecting InetAddress Through a Port-Other Types-1



    • ```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));
      ```

    • Connecting InetAddress Through a Port-Other Types-2





    • ```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



    • Void Bind(SocketAddress bindpoint)



    • ```Syntax

      It binds socket to a local address and port number.

      ```

    • Void Close()

    • ```Syntax

      It closes the socket.

      ```

    • Void Connect(Socket endpoint)

    • ```Syntax

      It connects this socket to the server.

      ```

    • Void Connect(Socket endpoint, int timeout)

    • ```Syntax

      It connects this socket to the server with a specified timeout value.

      ```

    • SocketChannel getChannel

    • ```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.

      ```

    • InetAddress getInetAddress

    • ```Syntax

      It returns the address to which the socket is connected.

      ```

    • InputStream getInputStream

    • ```Syntax

      It returns an input stream for this socket.

      ```

    • SetKeepAlive and GetKeepAlive

    • ```Syntax

      SetKeepAlive: If Set to True, then SO_KEEPALIVE is enabled

      GetKeepALive: If SetKeepAlive is true , then GetKeepAlive returns True else false.

      ```

    • InetAddress getLocalAddress

    • ```Syntax

      It gets the local address to which the socket is bound.

      ```

    • int getLocalPort()

    • ```Syntax

      It returns the local port to which the current socket is bound .

      ```

    • SocketAddress getLocalSocketAddress

    • ```Syntax

      It returns the address of the endpoint to which the socket is bound,
      or null if it is not bound yet.

      ```

    • OutputStream getOutputStream

    • ```Syntax

      It returns the outputstream for the connected socket.

      ```

    • boolean getOOBInline

    • ```Syntax

      The setOOBInline() method of Java Socket class enables
      or disables the SO_OOBInline. By default, the SO_OOBInline option is disabled.

      ```

    • int getPort()

    • ```Syntax

      It returns the remote port to which the socket is connected to.

      ```

    • int getRecieveBufferSize()

    • ```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.

      ```

    • SocketAddress getRemoteSocketAddress()

    • ```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.

      ```

    • boolean getReuseAddress()

    • ```Syntax

      Tests if SO_REUSEADDR is enabled.

      ```

    • int getSendBufferSize

    • ```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.

      ```

    • int getSoLinger()

    • ```Syntax

      Returns setting for SO_LINGER. -1 returns implies that the option is disabled.
      The setting only affects socket close.

      ```

    • int getSoTimeout()

    • ```Syntax

      Returns setting for SO_TIMEOUT. 0 returns implies that the option is disabled
      (i.e., timeout of infinity).

      ```

    • boolean getTcpNoDelay()

    • ```Syntax

      Tests if TCP_NODELAY is enabled.

      ```

    • int getTrafficClass()

    • ```Syntax

      Gets traffic class or type-of-service in the IP header for packets sent from this Socket

      ```

    • boolean isBound()

    • ```Syntax

      Returns the binding state of the socket.
      Returns true if the socket was successfuly bound to an address.
      ```

    • boolean isClosed()

    • ```Syntax

      Returns the closed state of the socket.
      Returns true if the socket has been closed.

      ```

    • boolean isConnected()

    • ```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.

      ```