Socket programming in c

MGHossainHimu 253 views 24 slides Mar 02, 2017
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Skimming through this short presentation may help recall socket creation and usage flow in C.


Slide Content

Socket Programming In C

Contents Socket Prerequisition Structs Functions TCP server and client flow Udp server and client flow

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

Socket (Contd.) Types:- Stream - uses TCP(connection oriented) SOCK_STREAM Datagram - uses UDP(connectionless) SOCK_DGRAM

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

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 )

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

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; };

Structs(Contd.) -- struct sockaddr{ unsigned short sa_familty; //address family - AF_INET, AF_INET6 char sa_data[14]; //14 bytes of protocol address };

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]; };

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; char __ss_pad2[__SS_PAD2SIZE];};

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

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 is a must //before listening

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 //ends ); after connect()’ing a datagram socket, send() and recv() can also be used

Functions(Contd.) -- listen(int sockfd, int backlog //incoming connection queue_element limit ); -- accept(int sockfd, struct sockaddr *addr, sock_t *addrlen );

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 int len, int flag);

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);

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);

-- 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.)

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 close : close the server (not expected in live server)

TCP Client Steps: socket connect send/receive shutdown Close

UDP Server Steps : socket bind sendto/receivefrom close

UDP Client Steps : socket sendto/recievefrom Close

PHEW!!!