2

I know that switches are better than hubs because they can forward an Ethernet frame exactly to the host that has that specific destination MAC address.

However, when two computers communicate, they use the IP address to send packets.

If I make a C program (a server and a client) and I run the server on one PC and the client on another host on the same network and start communicating (suppose I know the IP of both of them), how exactly will the NIC of the first computer will know the NIC of the computer with the destination IP?

The frame must have a destination MAC, otherwise all destination MACs would be broadcast MACs.

Without the computer to know the destination MAC address the switch is useless (from what I understand) and I do not understand how do hosts know each others MACs if they actually communicate using IP adresses.

5
  • 1
    You need to read about ARP (Address Resolution Protocol). There is a nice tutorial here Address Resolution Protocol Tutorial, How ARP work, ARP Message Format
    – DavidPostill
    Mar 15, 2016 at 13:06
  • 1
    Eventually, your line of questioning will lead into how routing works, because there is no real requirement that your destination is on your local network. If outside the local net, your packet needs to go to the MAC of your network's gateway host
    – infixed
    Mar 15, 2016 at 13:10
  • I know more about how routing works. Basically (? if the computer does not have the ip address in the ARP cache and no host on the network responds to the arp request package) than the host that wants to send the data to some IP will forward the packet to the default gateway (which should be a router) and that router will than check the routing table to see on what port it is connected the NETWORK in which the destination IP is part of. If the network is not in that table then router forwards to its own default route and so on until some router knows the network. Is this correct?
    – yoyo_fun
    Mar 15, 2016 at 13:14
  • The problem is nobody explained me how ARP works.. and prof did not know too much himself either so...
    – yoyo_fun
    Mar 15, 2016 at 13:17
  • 1
    It changes between IPv4 and IPv6. Basically when you try to send an IP to a local net address the machine your host doesn't have a MAC address for, it will broadcast (to everyone on the subnet) an ARP packet that says "I am aa.bb.cc.dd, where are you ww.xx.yy.zz?" The target machine should see the broadcast and send an ARP reply back. This informs your machine of the target's MAC address, and the transmission of the IP packet can proceed. I suspect Wikipedia has a good explanation. en.wikipedia.org/wiki/Address_Resolution_Protocol
    – infixed
    Mar 15, 2016 at 13:29

1 Answer 1

4

If I make a C program (a server and a client) and I run the server on one PC and the client on another host on the same network and start communicating (suppose I know the IP of both of them), how exactly will the NIC of the first computer will know the NIC of the computer with the destination IP?

Lets assume IPv4. For IPv6 the principles are similar but the details are different (neighbour discovery instead of ARP).

First your computer looks up the destiniation IP address in it's IP routing table. This will tell it the "interface" and the "next hop IP address". If the destination is in a local subnet then the next hop IP address will be the same as the destination IP address (if the destination is not in a local subnet then the next hop IP address will be the address of a gateway).

Next your computer looks up the next hop IP address in the ARP table for the interface. If it finds a match with a valid destination MAC address then the packet can be sent out immediately. Otherwise the packet will be queued until a MAC address is available.

To discover the MAC address your computer sends out an ARP request. The ARP request is sent to the broadcast MAC address. If everything is working the next hop (destination in your case) computer will reply to the ARP request, your computer will create an entry in it's arp table and the queued packets will be sent.

If there is no response to the ARP request then your system will likely retry a finite number of times before giving up on sending the packets. Depending on the OS it may or may not generate ICMP destination host unreachable packets when it gives up (linux does, BSD apparently doesn't, not sure about windows).

I know more about how routing works. Basically (? if the computer does not have the ip address in the ARP cache and no host on the network responds to the arp request package) than the host that wants to send the data to some IP will forward the packet to the default gateway (which should be a router) and that router will than check the routing table to see on what port it is connected the NETWORK in which the destination IP is part of. If the network is not in that table then router forwards to its own default route and so on until some router knows the network. Is this correct?

This is wrong, the default gateway is used when there is no other entry in the IP routing table that matches the destination IP. It is not used if ARP fails. Using the default gateway if ARP failed would be very likely to create routing loops.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .