SlideShare a Scribd company logo
1 of 24
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;
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
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
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
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
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!!!

More Related Content

What's hot

Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
elliando dias
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
elliando dias
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
elliando dias
 

What's hot (20)

Sockets
SocketsSockets
Sockets
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Sockets
SocketsSockets
Sockets
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
Lecture10
Lecture10Lecture10
Lecture10
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Sockets
Sockets Sockets
Sockets
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
 
Np unit2
Np unit2Np unit2
Np unit2
 
socket programming
socket programming socket programming
socket programming
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 

Viewers also liked (9)

Slow Down Online Guessing Attacks with Device Cookies
Slow Down Online Guessing Attacks with Device CookiesSlow Down Online Guessing Attacks with Device Cookies
Slow Down Online Guessing Attacks with Device Cookies
 
Actividad no. 8 carlos h. muñoz.
Actividad no. 8 carlos h. muñoz.Actividad no. 8 carlos h. muñoz.
Actividad no. 8 carlos h. muñoz.
 
3Com 3C17512
3Com 3C175123Com 3C17512
3Com 3C17512
 
Proyecto jelitza moreira
Proyecto jelitza moreiraProyecto jelitza moreira
Proyecto jelitza moreira
 
5.05 eng
5.05 eng5.05 eng
5.05 eng
 
3Com JD008A
3Com JD008A3Com JD008A
3Com JD008A
 
Ericsson SXK 109 1293/1
Ericsson SXK 109 1293/1Ericsson SXK 109 1293/1
Ericsson SXK 109 1293/1
 
3Com JE015A
3Com JE015A3Com JE015A
3Com JE015A
 
3Com DSA-15P-US
3Com DSA-15P-US3Com DSA-15P-US
3Com DSA-15P-US
 

Similar to Socket programming in c

TCP IP
TCP IPTCP IP
TCP IP
hivasu
 

Similar to Socket programming in c (20)

Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
sockets
socketssockets
sockets
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptx
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
123
123123
123
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.ppt
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
Os 2
Os 2Os 2
Os 2
 
Network sockets
Network socketsNetwork sockets
Network sockets
 
TCP IP
TCP IPTCP IP
TCP IP
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
Sockets
Sockets Sockets
Sockets
 
Udp socket programming(Florian)
Udp socket programming(Florian)Udp socket programming(Florian)
Udp socket programming(Florian)
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Networking chapter VI
Networking chapter VINetworking chapter VI
Networking chapter VI
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Socket programming in c

  • 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
  • 4. Socket (Contd.) Types:- Stream - uses TCP(connection oriented) SOCK_STREAM Datagram - uses UDP(connectionless) SOCK_DGRAM
  • 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; };
  • 9. Structs(Contd.) -- struct sockaddr{ unsigned short sa_familty; //address family - AF_INET, AF_INET6 char sa_data[14]; //14 bytes of protocol address };
  • 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
  • 15. Functions(Contd.) -- listen(int sockfd, int backlog //incoming connection queue_element limit ); -- accept(int sockfd, struct sockaddr *addr, sock_t *addrlen );
  • 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