Lab 3
Lab 3
Inha University
Bind()
Listen()
Accept()
Client Socket
connect()
recv()
Internet (Logical) Address + Port => Socket
1. Internet (Logical) Address
Internet (Logical) Address
• 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.
• 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)
• 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
• Because of Little/Big endian choice, we convert the port number (short) and
IPv4 address (long) using these methods (for correcting byte sequence):