Socket programming allows two programs to communicate over a network. It uses file descriptors and structs like sockaddr to define endpoints. There are two main socket types - stream sockets for TCP and datagram sockets for UDP. Key functions include socket(), bind(), listen(), accept(), send(), recv(), sendto(), and receivefrom(). A TCP server will create a socket, bind to a port, listen for connections, accept requests, and send/receive data. A TCP client connects and sends/receives similarly. A UDP server and client use sendto()/receivefrom() instead of connect().
3. Socket
Socket:-
one endpoint in a communication flow between two programs running over a network
uses file descriptor to communicate
File Descriptor:-
an integer associated with an opened file
OS creates an entry to represent an opened file and to keep information of the file
5. Prerequisition
Network Addresses:-
IPv4 - 32 bits (loopback :- 127.0.0.1)
Class A : N.H.H.H (first octet starts with 0)
Class B : N.N.H.H (first octet starts with 10)
Class C : N.N.N.H (first octet starts with 110)
IPv6 - 128 bits (loopback :- ::1)
Representation of an IPv4 address (e.g: 192.168.8.82) in IPv6 address is simply - ::ffff:192.168.8.82
6. Prerequisition
Port Number - 16 bits
local address for communication- can’t assign below 1024 as registered
used to differentiate between services
e.g: HTTP - 80, Telnet - 23, SMTP - 25, SSH - 22, TFTP - 69…
Service Name and Transport Protocol Port Number Registry (last updated:- 2016-06-02)
7. Prerequisition
Byte Order :-
each memory stores a single byte
but a word (for instance) 4 bytes
word 1234ABCD can be stored in 2 ways
Big Endian - Little Endian -
Address Byte
1000 12
1001 34
1002 AB
1003 CD
Address Byte
1000 CD
1001 AB
1002 34
1003 12
8. Structs
-- struct addrinfo{
int ai_flags; //eg: AI_PASSIVE
int ai_family; //AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; //SOCK_STREAM, SOCK_DGRAM
size_t ai_protocol; //0 for any
struct sockaddr *ai_addr; //struct sockaddr_in or in6
char *ai_canonname; //full canonical hostname
Struct addrinfo *ai_next;
};
10. Structs(Contd.)
-- struct sockaddr_in{ //parallel structure
of sockaddr but
// requires
casting before connect() ‘ing
short int sin_family;
unsigned short int sin_port; //port number
struct in_addr sin_addr; //Internet address
unsigned char sin_zero[8];
};
11. Structs(Contd.)
-- struct in_addr{
Uint32_t s_addr; //4 bytes
};
-- struct sockaddr_storage{
sa_family_t ss_family; //address family, can be checked to
see whether it
//is AF_INET or
AF_INET6
char __ss_pad1[__SS_PAD1SIZE];
int64_t __ss_align;
12. Functions
-- getaddrinfo(const char* IP, //returns 0
in success
const char* port,
const struct addrinfo *hint, //points to a addrinfo
structure
//filled with relevant info
struct addrinfo **res); //pointer to the
linked list result
13. Functions(Contd.)
-- socket(int domain, //family → PF_INET,
PF_INET6
int type,
//type → SOCK_DGRAM, SOCK_STREAM
int protocol
//protocol → can be set AF_UNSPEC or 0,
//AF_INET or 2, AF_INET6 or 10
); //returns
int filedescriptor
-- bind (int sockfd, struct sockaddr *addr, int addrlen); //binding
14. Functions(Contd.)
-- connect(int sockfd, //used in
TCP mainly, can be used in
//UDP as well
struct sockaddr *serv_addr, //to specifically inform that
//receivefrom() or sendto()
int addrlen //with a particular
endpoint and
//reject packets coming from other
16. Functions(Contd.)
-- send(int sockfd, //returns number of
bytes actually sent
const void *msg, //if return value less than
len remaining
//message bytes needs
resending
int len,
int flag);
-- recv(int sockfd, //returns number of bytes
actually read
void *buf, //and ‘0’ if other end closes
connection
17. Functions(Contd.)
-- sendto(int sockfd,
const void *msg, //differentiates from
send() in a manner
//that it
needs to specify the
//the
destination IP address and port no.
int len,
unsigned int flags,
const struct sockaddr *addr_to,
socklen_t tolen);
18. Functions(Contd.)
-- receivefrom(int sockfd,
const void *msg, //differentiates from
recv() in a manner
//that it needs to specify
int len, //the sender’s IP
address and port number
unsigned int flags,
const struct sockaddr *addr_from,
socklen_t fromlen);
19. -- close(int sockfd);
-- shutdown(int sockfd, //how = 0 →
further receives are blocked
int how //how = 1 → further
sending blocked
); //how = 2 →
further sending and
//receiving
blocked
Functions(Contd.)
20. TCP Server
Steps:
socket : create the socket
bind : bind the socket with port number
listen : wait for connection and specify queue number
accept : accept request
send/receive : send or read data from other point
shutdown : shutdown further sending or receiving