According to OSI reference model layers communicate with lower layer to transmit information on to other network. So they add header and tail for data and pass it on to lower layer and finally passed to other computer over wire or wireless. Switch is for communication between link layers and router for network layer. What is the use of them when 2 networking layers can't communicate directly and need to pass to lower layer.
Each layer of the OSI model serves a unique purpose with the end goal of host to host communication.
Layer 1 is responsible for moving bits (
0s) across a network.
Layer 2 is responsible for communication within networks. L2 uses addressing known as MAC addresses for this purpose. A MAC address is essentially the address of a particular NIC.
Layer 3 is responsible for communication between networks. L3 uses addressing known as IP addresses for this purpose.
Fundamentally, your question is not "what is the purpose of Routers", but "what is the purpose of networks", if you understand why the Internet is divided up into multiple networks, then you will understand why you need a device that facilitates communication between those networks (aka, the router).
Consider your mailing address. It will look something like this:
432 Park Avenue
Imagine a postal employee having to deliver a letter to your address. It would be impossible for the local postal employee to know every residential unit in the city. BUT, its far more feasible for the postal employee to know every street in the city. The postal employee will find the street you live on, and from there count out the residences until they get to 432. That is how they will find your specific house.
In this analogy, the street is like a network (Park Avenue), it is a grouping of houses that a single postal employee can know the location of. One street can exist next to many other streets, and as long as each street is uniquely named, they can be found by your postal employee.
The building number (432) is like a MAC address, it is the specific house/building within a street. Other streets may have the same building number, but within a particular street there can only be unique house numbers.
Your mailman doesn't need to know every house on every street, they just need to know every street. Once the street is found, they can track down the exact location based upon the house number.
The internet is the same way. Instead of forcing a Router to know every location of every host in the whole world, Router's are only concerned with knowing the location of every network. Once the packet is routed to the proper Router (aka, the proper street), the packet delivery process can look up the appropriate MAC address (aka, the proper house) to deliver the packet to.
So in the end, a Router exists to know the location of every network, and to move packets from Router to Router until the final Router in the path is found. When that happens, that Router will look up the specific MAC address to get the packet to its intended destination.
For more information on how a Router does its job (and therefore, the job the router serves) check out this article that discusses how a Router moves packets through a network.
The OP asked this question in comments:
only bits come out of the physical layer for transmission. So does Router convert them back to packets upon receiving bits as input?
The answer is "eventually yes". Each layer adds to the original data what it needs to add in order to accomplish its purpose:
- Layer 3 (the Router) will add an IP header which would include a Source and Destination IP address
- Layer 2 (the NIC on the Router) would add an Ethernet header which would include a Source and Destination MAC address
Layer 2 would then convert the resulting frame into
0s and send them to Layer 1 to be sent across the wire.
The process can be seen in this animation:
Layers communicate with their corresponding layer logically. That means the transport layer adds information that the receiver's transport layer understands.
Your statement "Switch is for communication between link layers and router for network layer." is a mischaracterization of their functions. See if this question helps you.