Lab 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

CSE4202 -001

Inha University

Computer Networks - Lab


Socket Programming in C

3. Representing internet addresses in Socket


Programming
Review (Lab-2):
Socket creation

1. socket(internet, website, address)


2. socket(domain, type, protocol)
3. socket(potatoes, coke, tacos)
From the 5 fundamental parts of client-server
communication, about address:

* First socket descriptor is created


* Then server socket is “bound to a port”
* Then client socket is “connected to an address+port”
Server Socket

• “Server” Socket workflow: Socket()

Bind()

Listen()

Accept()
Client Socket

• “client” Socket workflow: Socket()

connect()

recv()
Internet (Logical) Address + Port => Socket
1. Internet (Logical) Address
Internet (Logical) Address

• What is “Internet (Logical) Address”?


• Internet address across networks is like a home address (with person’s name alongside it)
• You’ll get a package delivered to your home (network id), now your home will deliver the package to you

directly as it has a direct access to you (destination host id)

• Formally: An Internet Protocol address (IP address or logical address) is a numerical label


assigned to each device connected to a computer network that uses the Internet Protocol
for communication.
Internet (Logical) Address

• Classful vs Classless Internet Addresses


2. Port number
Port number

• Port number is used to deliver a packet to a specific process (program). And to be even more
precise, it is delivered to the process which created the socket that specified the particular port.

• Formally: A port number is the logical address of each application or process that uses a


network or the Internet to communicate. A port number uniquely identifies a network-based
application on a computer.
Internet Address representation in C
Internet Address representation in C

• Goal:
• We need to specify a IPv4 address (as it is used the most)
• We need to set the port address in both ends of the socket (meaning that sending and
receiving sockets must have the same port number)

• struct sockaddr_in => socket address internet (and sa_family = socket address family)

• sin_xxxxx -> socket internet xxxxx (“xxxxx” can be family, port, address, zero=useless)

IP Address + Port IP Address


Internet Address representation in C

• Members of sockaddr_in
• sin_family: address family (almost always AF_INET)
• sin_port: port number
• sin_addr: IPv4 address
• sin_zero: no special meaning => not used
sin_zero means zero padding, and is used for making the code bug free on various
architectures by utilizing it as a buffer because the length of useful data in sockaddr_in is
shorter than sockaddr (a slight hack, more details)
Internet Address representation in C

• Where sockaddr_in is used the socket programming code (server socket example)
Internet Address representation in C

• Depending on CPU can interpret the internet address in decimal using


either Little or Big Endian (order of bytes)
Internet Address representation in C

• Because of Little/Big endian choice, we convert the port number (short) and
IPv4 address (long) using these methods (for correcting byte sequence):

htons -> h host


htons -> n network
htons -> s short
htonl -> l long
Internet Address representation in C
Internet Address representation in C

• Allowing any client to make connection requests


• INADDR_ANY: is, as clear by its name, basically saying that ip address is
not set, and it can be any internet address. It is used
mainly for server socket to accept any client’s request.
Internet Address representation in C

• Remember what commands we used to start client/server communication


in the first two labs?

./server 1234 = listen to INADDR_ANY address, on port 1234

./hclient 127.0.0.1 1234 = connect to IPv4 address “127.0.0.1”, on port 1234


Let’s compile and play with source files !

You might also like