Tuesday, April 23, 2019


Socket Programming using TCP Socket

Socket:
Sockets allow communication between two different processes on the same or different machines. A Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. The client in socket programming must know two information:
  • IP Address of Server, and
  • Port number.

Server Side Programming in Java:
Method
Description
1) public Socket accept()
returns the socket and establish a connection between server and client.
2) public synchronized void close()
closes the server socket.

Java Program for server side:
import java.net.*;
import java.io.*;
  
public class Server
{
    //initialize socket and input stream
    private Socket          socket   = null;
    private ServerSocket    server   = null;
    private DataInputStream in       =  null;
  
    // constructor with port
    public Server(int port)
    {
        // starts server and waits for a connection
        try
        {
            server = new ServerSocket(port);
            System.out.println("Server started");
  
            System.out.println("Waiting for a client ...");
  
            socket = server.accept();
            System.out.println("Client accepted");
  
            // takes input from the client socket
            in = new DataInputStream(
                new BufferedInputStream(socket.getInputStream()));
  
            String line = "";
  
            // reads message from client until "Over" is sent
            while (!line.equals("Over"))
            {
                try
                {
                    line = in.readUTF();
                    System.out.println(line);
  
                }
                catch(IOException i)
                {
                    System.out.println(i);
                }
            }
            System.out.println("Closing connection");
  
            // close connection
            socket.close();
            in.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }
  
    public static void main(String args[])
    {
        Server server = new Server(5000);
    }
}

Client side programming in Java:
Method
Description
1) public InputStream getInputStream()
returns the InputStream attached with this socket.
2) public OutputStream getOutputStream()
returns the OutputStream attached with this socket.
3) public synchronized void close()
closes this socket

Java Program for client side:
import java.net.*;
import java.io.*;
  
public class Client
{
    // initialize socket and input output streams
    private Socket socket            = null;
    private DataInputStream  input   = null;
    private DataOutputStream out     = null;
  
    // constructor to put ip address and port
    public Client(String address, int port)
    {
        // establish a connection
        try
        {
            socket = new Socket(address, port);
            System.out.println("Connected");
  
            // takes input from terminal
            input  = new DataInputStream(System.in);
  
            // sends output to the socket
            out    = new DataOutputStream(socket.getOutputStream());
        }
        catch(UnknownHostException u)
        {
            System.out.println(u);
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
  
        // string to read message from input
        String line = "";
  
        // keep reading until "Over" is input
        while (!line.equals("Over"))
        {
            try
            {
                line = input.readLine();
                out.writeUTF(line);
            }
            catch(IOException i)
            {
                System.out.println(i);
            }
        }
  
        // close the connection
        try
        {
            input.close();
            out.close();
            socket.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }
  
    public static void main(String args[])
    {
        Client client = new Client("127.0.0.1", 5000);
    }
}












Snapshots:

                   Client                                                                            Server

Implementation of network address translation in cisco packet tracer


NAT stands for "Network Address Translation." NAT translates the IP addresses of computers in a local network to a single IP address. This address is often used by the router that connects the computers to the Internet. Address translation reduces the need for IPv4 public addresses and hides private network address ranges. 

The Purpose of NAT
NAT serves three main purposes:

·         Provides a type of firewall by hiding internal IP addresses
·         Enables a company to use more internal IP addresses. Since they're used internally only, there's no possibility of conflict with IP addresses used by other companies and organizations.
·         Allows a company to combine multiple ISDN connections into a single Internet connection.

NAT conserves the number of globally valid IP addresses a company needs, and in combination with Classless Inter-Domain Routing (CIDR) has done a lot to extend the useful life of IPv4 as a result.
The NAT mechanism ("natting") is a router feature, and is often part of a corporate firewall. NAT gateways can map IP addresses in several ways:
·         From a local IP address to one global IP address statically;
·         From a local IP address to any of a rotating pool of global IP addresses a company may have;
·         From a local IP address plus a particular TCP port to a global IP address or one in a pool of ports;
·         From a global IP address to any of a pool of local IP addresses on a round-robin basis.

Types of NAT:
Static NAT:
Static NAT (Network Address Translation):
Static NAT (Network Address Translation) is one-to-one mapping of a private IP address to a public IP address. Static NAT (Network Address Translation) is useful when a network device inside a private network needs to be accessible from internet.
Dynamic NAT (Network Address Translation):

Dynamic NAT can be defined as mapping of a private IP address to a public IP address from a group of public IP addresses called as NAT pool. Dynamic NAT establishes a one-to-one mapping between a private IP address to a public IP address. Here the public IP address is taken from the pool of IP addresses configured on the end NAT router. The public to private mapping may vary based on the available public IP address in NAT pool.

 Advantages of NAT:

·         NAT conserves legally registered IP addresses .
·         It provides privacy as the device IP address, sending and receiving the traffic, will be hidden.
·         Eliminates address renumbering when a network evolves.

CONFIGURATION:
Topology:
                      
Create a n/w topology as shown in Figure 7.1. I have taken 2 PCs with IP address as 10.0.0.10, 10.0.0.20. These PCs are connected to a switch which is connected to a router Router1 with gig 0/1. The interface IP address of gig 0/1 is 10.0.0.1. The Router1 is connected to Router2 via a serial ports se 0/1/0 in both the routers.

The interface IP address for Router1 at se 0/1/0 is 100.0.0.1. The interface IP address for Router2 at se 0/1/0 is 100.0.0.2. Router2 is connected to a server whose IP address is 192.168.1.10 via gig 0/0 port whose interface IP address is 192.168.1.1.

Now run the following commands as below to setup static NAT.

ON ROUTER 1:
Router>en
Router#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int gig 0/1
Router(config-if)#ip address 10.0.0.1 255.0.0.0
Router(config-if)#no shut
Router(config-if)#
%LINK-5-CHANGED: Interface GigabitEthernet0/1, changed state to up
Router(config-if)#int se 0/1/0
Router(config-if)#ip address 100.0.0.1 255.0.0.0
Router(config-if)#no shut

%LINK-5-CHANGED: Interface Serial0/1/0, changed state to down
Router(config-if)#
Router(config-if)#
%LINK-5-CHANGED: Interface Serial0/1/0, changed state to up

Router(config-if)#exit
Router(config)#ip nat inside source static 10.0.0.10 50.0.0.10
Router(config)#int gig 0/1
Router(config-if)#ip nat inside
Router(config-if)#exit
Router(config)#int se 0/1/0
Router(config-if)#ip nat outside
Router(config-if)#exit
Router(config)#ip nat inside source static 10.0.0.20 50.0.0.20
Router(config)#ip nat inside source static 10.0.0.30 50.0.0.30
Router(config)#exit
Router(config)#ip route 200.0.0.0 255.255.255.0 100.0.0.2
Router(config)#exit

ON ROUTER 2:

Router>en
Router#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int gig 0/0
Router(config-if)#ip address 192.168.1.1 255.255.255.0
Router(config-if)#no shut

Router(config-if)#
%LINK-5-CHANGED: Interface GigabitEthernet0/0, changed state to up
Router(config-if)#int se 0/1/0
Router(config-if)#ip address 100.0.0.2 255.0.0.0
Router(config-if)#no shut

Router(config-if)#
%LINK-5-CHANGED: Interface Serial0/1/0, changed state to up

Router(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial0/1/0, changed state to up

Router(config-if)#ip nat inside source static 192.168.1.10 200.0.0.10
Router(config)#int gig 0/0
Router(config-if)#ip nat inside
Router(config-if)#exit
Router(config)#int se 0/1/0
Router(config-if)#ip nat outside
Router(config-if)#exit
Router(config)#ip route 50.0.0.0 255.0.0.0 100.0.0.1
Router(config)#exit



 Ping snapshot of NAT:












Socket Programming using TCP Socket Socket: Sockets allow communication between two different processes on the same or different ma...