Socket Introduction
School of Information and Communication Technology,
Hanoi University of Science and Technology
References
[1] W.Richard Stevens, Unix Network Programming Vol.1, 3rd Ed., Prentice
Hall.
[2] Keir Davis, John W. Turner, and Nathan Yocom, The Definitive Guide to
Linux Network Programming, Apress.
[3] Michael Donahoo, Kenneth Calvert, TCP/IP Sockets in C: Practical Guide
for Programmers, Elsevier.
2
Content
• Socket
• Stream Socket
• Datagram Socket
• APIs for managing names and IP addresses
3
Socket
• Sockets (in plural) are
• Application programming interface (API) at transport
layer in TCP/IP stack
• Application may send and receive data through socket
4
Socket
• Setup socket
• Where is the remote machine (IP address, hostname)
• What service gets the data (port)
• Send and Receive
• Designed just like any other I/O in Unix
• send – write
• recv – read
• Close the socket
5
Socket
• The main types of sockets in TCP/IP are
• Stream sockets
• Use TCP as the end-to-end protocol (with IP underneath) and
thus provide a reliable byte-stream service
• Datagram sockets
• Use UDP (again, with IP underneath) and thus provide a best-
effort datagram service
• Socket Address :
• Combination of host name/Adress IP + transport port
6
Stream Sockets (TCP)
• TCP provides connections between clients and
servers
• TCP also provides reliability :
• Acknowledgment
• Error control
• Flow control
• Congestion control
• TCP connection is full-duplex
• Send and receive data over single connection.
7
Stream Sockets (TCP)
• Working flow
8
Stream Sockets (TCP)
9
Stream Sockets (TCP)
• C/C++ Programming APIs
• socket()
• Creates a socket of a given domain, type, protocol (buy a phone)
• Returns a file descriptor (called a socket ID)
• bind()
• Assigns a name to the socket (get a telephone number)
• Associate a socket with an IP address and port number (Eg :
192.168.1.1:80)
• accept()
• server accepts a connection request from a client (answer phone)
• There are basically three styles of using accept:
• Iterating server: Only one socket is opened at a time.
• Forking server: After an accept, a child process is forked off to handle
the connection.
• Concurrent single server: use select to simultaneously wait on all
open socketIds, and waking up the process only when new data
arrives
10
Stream Sockets (TCP)
• C/C++ Programming APIs
• connect()
• Client requests a connection request to a server
• This is the first of the client calls
• send()
• Write data to connection to the parterner (speak)
• recv()
• Read data from connection from the parterner (listen)
• close()
• Close a socket descriptor (end the call)
11
Datagram Socket (UDP)
• UDP is a simple transport-layer protocol
• If a datagram is errored or lost, it won’t be
automatically retransmitted (can process in
application)
• UDP provides a connectionless service, as there
need not be any long-term relationship between a
UDP client and server
12
Datagram Socket (UDP)
13
C/C++ Headers
• Can be found under /usr/include
• E.g.: /usr/include/i386-linux-gnu
• <stdio.h>
• input and output of basic C programs.
• <sys/types.h>
• Contains definitions of data types used in system calls.
These types are used in the next two include files.
• <sys/socket.h>
• Includes definitions of structures needed for sockets.
• <netinet/in.h>
• contains constants and structures needed for internet
domain addresses.
14
Socket Address Structures
• Most socket functions require a pointer to a socket
address structure as an argument.
• Each supported protocol suite defines its own socket
address structure.
• A Socket Address Structure is a structure which has
information of a socket to create or connect with it
• Types of socket address structures
• IPv4
• IPv6
15
IPv4 Socket Address Structure
16
IPv6 Socket Address Structure
17
Names and IP Addresses APIs
• Auxiliary APIs:
• All binary values are network byte ordered
• htons, htonl, ntohs, ntohl: byte ordering
• inet_ntoa(), inet_aton(), inet_addr : Convert IP addresses
from a dots-and-number string (eg : 192.168.1.1) to a struct
in_addr and back
• inet pton, inet ntop: conversion of IP numbers between
presentation and strings
18
Names and IP Addresses APIs
#include <sys/socket.h>
•getprotobyname()
• Retrieve the protocol name and number corresponding to a protocol name.
•getprotobynumber()
• Retrieve the protocol name and number corresponding to a protocol
number.
List of IP protocol numbers – Wikipedia
•getservbyname()
• Retrieve the service name and port corresponding to a service name.
•getservbyport()
• Retrieve the service name and port corresponding to a port.
19
Names and IP Addresses APIs
• struct hostent* gethostbyname (const char* hostname);
• Translate DNS host name to IP address (uses DNS)
• struct hostent* gethostbyaddr (const char* addr, size_t len, int family);
• Translate IP address to DNS host name (not secure)
• int gethostname (char* name, size_t namelen);
• Read host’s name (use with gethostbyname to find local IP)
• getprotobyname()
• Retrieve the protocol name and number corresponding to a protocol name.
• getprotobynumber()
• Retrieve the protocol name and number corresponding to a protocol
number.
• getservbyname()
• Retrieve the service name and port corresponding to a service name.
• getservbyport()
• Retrieve the service name and port corresponding to a port.
20
Names and IP Addresses APIs
• getservbyname(): Get service information corresponding to
a service name and protocol.
• Servname: A pointer to a service name.
• protoname
• An optional pointer to a protocol name.
• If this is NULL, getservbyname() returns the first service entry for which the name
matches the s_name or one of the s_aliases.
• Otherwise getservbyname() matches both the name and the proto.
• Returns
• non-null pointer if OK
• NULL on error
struct servent *sptr = NULL;
sptr = getservbyname(“ftp”, “tcp");
21
Names and IP Addresses APIs
struct servent {
char *s_name; /* official service name */
char **s_aliases; /* alias list */
int s_port; /* port number, network-byte order */
char *s_proto; /* protocol to use */
};
• s_name
• Official name of the service.
• s_aliases
• A NULL-terminated array of alternate names.
• s_port
• The port number at which the service may be contacted. Port numbers are returned in
network byte order.
• s_proto
• The name of the protocol to use when contacting the service.
22
Names and IP Addresses APIs
• Getservbyport(): Get service information corresponding to a
service name and protocol.
• servname
• A pointer to a service name.
• protoname
• An optional pointer to a protocol name.
• If this is NULL, getservbyname() returns the first service entry for which the name matches the
s_name or one of the s_aliases.
• Otherwise getservbyname() matches both the name and the proto.
• Returns
• Non-null pointer if OK
• NULL on error
struct servent *sptr;
sptr = getservbyname(“ftp”, “tcp");
23
Names and IP Addresses APIs
struct hostent {
char *h_name; /* official (canonical) name of host */
char **h_aliases; /* pointer to array of pointers to alias names */
int h_addrtype; /* host address type: AF_INET */
int h_length; /* length of address: 4 */
char **h_addr_list; /* ptr to array of ptrs with IPv4 addrs */
};
24
Names and IP Addresses APIs
• Byte Ordering
• Little-endian byte order (Host)
• Big-endian byte order (Network)
25
Names and IP Addresses APIs
• Byte Ordering
• There is no standard between these two-byte orderings
• The Internet protocols use big-endian byte ordering
• Host order can be big- or little-endian
• x86, ARM: little-endian
• SPARC: big-endian
• Conversion
• htons(), htonl(): host to network short/long
• ntohs(), ntohl(): network order to host short/long
26
Names and IP Addresses APIs
• htons(), htonl(), ntohs(), ntohl()
• Convert multi-byte integer types from host byte order to
network byte order
#include <netinet/in.h>
uint32_t htonl(u_long hostlong); // host to network long
uint16_t htons(u_short hostshort);// host to network short
uint32_t ntohl(u_long netlong); // network to host long
uint16_t ntohs(u_short netshort); // network to host short
• Each function returns the converted value
27
Names and IP Addresses APIs
• char* inet_ntoa (struct in_addr inaddr);
• Translate IP address to ASCII dotted-decimal notation
(e.g., “128.32.36.37”); not thread-safe
• in_addr_t inet_addr (const char* strptr);
• Translate dotted-decimal notation to IP address; returns
-1 on failure, thus cannot handle broadcast value
“255.255.255.255”
• int inet_aton (const char* strptr, struct in_addr
inaddr);
• Translate dotted-decimal notation to IP address; returns
1 on success, 0 on failure
28
Names and IP Addresses APIs
• inet_aton(): Convert IP addresses from a dots-and-
number string to a struct in_addr
• Return:
• The value non-zero if the address is valid
• The value 0 if the address is invalid
struct in_addr someAddr;
if(inet_aton(“10.0.0.1”, &someAddr))
printf(“The address is valid”);
else
printf (“The address is invalid”);
29
Names and IP Addresses APIs
• inet_ntoa(): Convert IP addresses from a struct
in_addr to a dots-and-number string; return: the
dots-and-numbers string
struct in_addr someAddr;
if(inet_aton(“10.0.0.1”, &someAddr))
printf(“The address is
valid”);
else
printf (“The address is
invalid”);
char *addrStr = NULL;
addrStr = inet_ntoa(someAddr);
30
IPv4
• Developed in APRANET (1960s)
• 32-bit number
• Divided into classes that describe the portion of the
address assigned to the network (netID) and the
portion assigned to endpoints (hosten)
• A : netID – 8 bit
• B : netID – 16 bit
• C : netID – 24 bit
• D : use for multicast
• E : use for experiments
31
IPv6
• IPv6 address is 128 bits
• To subdivide the available addresses into a hierarchy of
routing domains that reflect the Internet's topology
• IPv6 address is typically expressed in 16-bit
chunks displayed as hexadecimal numbers
separated by colons
Example : 21DA:00D3:0000:2F3B:02AA:00FF:FE28:9C5A
or : 21DA:D3:0:2F3B:2AA:FF:FE28:9C5A
32
New APIs for IPv6
• Those APIs only supports IPv4 but IPv6 will be
replace IPv4 in the future, so we need APIs
supporting IPv6
• They are
• getaddrinfo
• getnameinfo
• These APIs have replaced the IPv4 specific
routines
33
Q&A
34