Search code examples
c#network-programminghttpwebrequestip-addresswebrequest

Backend process while creating Webrequest in c#


I want to know the complete procedure which is going to be performed while I am doing webRequest from C#. I am using dongle to connect to internet. Actually what is going in backend which prepares headers for me? How my .net application know my public IP address which is provided by my internet provider(Which will be shown in whatismyip)? Is my public IP stored anywhere in my system? I just want no know the complete process like packet transfer or socket related thing/DNS. How they are built and how they communicate?

I am new to network programming. Good tutorial link are also welcome. Kindly help me to understand all this things.


Solution

  • "just a tutorial" won't help you here.

    Core of your problem: networking and to be more specific TCP/IP.

    Indeed your IP-address is stored on your system and is bound to your networkinterface. But that's just part of the story.

    If you send data across a tcp network, your data is packed in a packet that has

    1. a header containing the ip address of the server (destination)
    2. and your address (source).
    3. couple of other things

    So indeed server know your ip address. It has to otherwise it won't be able to send a reply to you.

    If you are behind a router that does NAT (address translation) your local ip (example 192.168.1.1) is replaced by it. Making it a 5 step process

    1. request to server is received by router
    2. router replaces your ip with public ip.
    3. request is forwarded to server.
    4. reply is send from server to router ip.
    5. router forwards reply to you local ip

    On a generic router translation is an option the should be configured explicit. However on (consumer) routers provided by ISP's where there is almost always only 1 public address per subscriber, NAT is standard since private addresses (192.168/16, 10/8) are not allowed and as such not routed over the internet.

    That's tcp in a nutshell with respect to your case. How the packets flow over the network from router to server and back is story on it's own ;-)

    Here is a primer on tcp/ip

    How does your pc knows the ip of the server. Here comes DNS into play a giant distributed lookup system to translate a hostname to an ip-address. Comparable to a phonebook. Standard lecture on this DNS and BIND

    Making a webrequest add a layer of complexity. Protocol involved is HTTP(S). Here is a lot involved but a starting point can be A HTTP beginner's guide

    HTTP is agnostic of the transport. Your computer decides on the transport to communicate with the server. There are a couple of more networkprotocols but tcp-ip is (almost) the default.