SlideShare a Scribd company logo
3
Most read
5
Most read
14
Most read
Introduction to Sockets
Why do we need sockets?
Provides an abstraction for
interprocess communication
• The services provided (often by the operating
system) that provide the interface between
application and protocol software.
Application
Network API
Protocol A Protocol B Protocol C
Definition
Functions
–Define an “end- point” for
communication
–Initiate and accept a connection
–Send and receive data
–Terminate a connection gracefully
Examples
File transfer apps (FTP), Web browsers
(HTTP), Email (SMTP/ POP3), etc…
Types of Sockets
• Two different types of sockets :
– stream vs. datagram
• Stream socket :( a. k. a. connection- oriented socket)
– It provides reliable, connected networking service
– Error free; no out- of- order packets (uses TCP)
– applications: telnet/ ssh, http, …
• Datagram socket :( a. k. a. connectionless socket)
– It provides unreliable, best- effort networking service
– Packets may be lost; may arrive out of order (uses UDP)
– applications: streaming audio/ video (realplayer), …
Addressing
Client Server
Addresses, Ports and Sockets
• Like apartments and mailboxes
– You are the application
– Your apartment building address is the address
– Your mailbox is the port
– The post-office is the network
– The socket is the key that gives you access to the
right mailbox
Client – high level view
Create a socket
Setup the server address
Connect to the server
Read/write data
Shutdown connection
int connect_ socket( char *hostname, int port) {
int sock;
struct sockaddr_in sin;
struct hostent *host;
sock = socket( AF_ INET, SOCK_ STREAM, 0);
if (sock == -1)
return sock;
host = gethostbyname( hostname);
if (host == NULL) {
close( sock);
return -1;
}
memset (& sin, 0, sizeof( sin));
sin. sin_ family = AF_ INET;
sin. sin_ port = htons( port);
sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0];
if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) {
close (sock);
return -1;
}
return sock;
}
Resolve the host
struct hostent *gethostbyname( const char *hostname);
/*Return nonnull pointer if OK, NULL on error */
Setup up the struct
unit16_t htons(unit16_t host16bitvaule)
/*Change the port number from host byte order to
network byte order */
Connect
connect(int socketfd, const struct sockaddr * servaddr,
socket_t addrlen)
/*Perform the TCP three way handshaking*/
Hostent structure
struct hostent{
char * h_name /*official name of host*/
char ** h_aliases; /* pointer ot array of
pointers to alias name*/
int h_addrtype /* host address type*/
int h_length /* length of address */
char ** h_addr_list /*prt to array of ptrs with 
IPv4 or IPv6 address*/
}
Ipv4 socket address structure
struct socketaddr_in{
uint8_t sin_len; /*length of the structure (16)*/
sa_falimily_t sin_family /* AF_INT*/
in_port_t sin_port /* 16 bit TCP or UDP port number*/
struct in_addr sin_addr /* 32 bit Ipv4 address */
char sin_zero(8)/* unused*/
}
Make the socket
Socket(int family , int type, in t protocol);
return nonnegative value for OK, -1 for error
Server – high level view
Create a socket
Bind the socket
Listen for connections
Accept new client connections
Read/write to client connections
Shutdown connection
Listening on a port (TCP)
int make_ listen_ socket( int port) {
struct sockaddr_ in sin;
int sock;
sock = socket( AF_ INET, SOCK_ STREAM, 0);
if (sock < 0)
return -1;
memset(& sin, 0, sizeof( sin));
sin. sin_ family = AF_ INET;
sin. sin_ addr. s_ addr = htonl( INADDR_ ANY);
sin. sin_ port = htons( port);
if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0)
return -1;
return sock;
}
Make the socket
Setup up the struct
Bind
bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen);
/* return 0 if OK, -1 on error
assigns a local protocol adress to a socket*/
accepting a client connection (TCP)
int get_ client_ socket( int listen_ socket) {
struct sockaddr_ in sin;
int sock;
int sin_ len;
memset(& sin, 0, sizeof( sin));
sin_ len = sizeof( sin);
sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_ len);
return sock;
}
Setup up the struct
Accept the client connection
accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen)
/* return nonnegative descriptor if OK, -1 on error
return the next completed connection from the front of the
completed connection queue.
if the queue is empty,
the process is put to sleep(assuming blocking socket)*/
Sending / Receiving Data
• With a connection (SOCK_STREAM):
– int count = send(sock, &buf, len, flags);
• count: # bytes transmitted (-1 if error)
• buf: char[], buffer to be transmitted
• len: integer, length of buffer (in bytes) to transmit
• flags: integer, special options, usually just 0
– int count = recv(sock, &buf, len, flags);
• count: # bytes received (-1 if error)
• buf: void[], stores received bytes
• len: # bytes received
• flags: integer, special options, usually just 0
– Calls are blocking [returns only after data is sent (to
socket buf) / received]
socket()
bind()
listen()
accept()
read()
write()
read()
close()
Socket()
connect()
write()
read()
close()
TCP Client
TCP Server
Well-known port
blocks until connection from client
process request
Connection establishment
Dealing with blocking calls
• Many functions block
– accept(), connect(),
– All recv()
• For simple programs this is fine
• What about complex connection routines
– Multiple connections
– Simultaneous sends and receives
– Simultaneously doing non-networking processing
Dealing with blocking (cont..)
• Options
– Create multi-process or multi-threaded code
– Turn off blocking feature (fcntl() system call)
– Use the select() function
• What does select() do?
– Can be permanent blocking, time-limited blocking or non-
blocking
– Input: a set of file descriptors
– Output: info on the file-descriptors’ status
– Therefore, can identify sockets that are “ready for use”:
calls involving that socket will return immediately
select function call
• int status = select()
– Status: # of ready objects, -1 if error
– nfds: 1 +largest file descriptor to check
– readfds: list of descriptors to check if read-ready
– writefds: list of descriptors to check if write-ready
– exceptfds: list of descriptors to check if an
exception is registered
– Timeout: time after which select returns

More Related Content

PPTX
HyperText Transfer Protocol (HTTP)
PPT
Lecture 6 -_presentation_layer
PPTX
Address resolution protocol (ARP)
PPTX
switching techniques in data communication and networking
PPTX
PPTX
IP addressing and Subnetting PPT
PPT
Packet switching
PPTX
System and network administration network services
HyperText Transfer Protocol (HTTP)
Lecture 6 -_presentation_layer
Address resolution protocol (ARP)
switching techniques in data communication and networking
IP addressing and Subnetting PPT
Packet switching
System and network administration network services

What's hot (20)

PPTX
Dns server
PPT
Paging.ppt
PPTX
IP addressing seminar ppt
PDF
TCP over wireless slides
PPT
Advanced Operating System- Introduction
PPTX
Presentation on samba server
PDF
operating system structure
PPT
Guided Transmission Media
PPT
System Calls.ppt
PPTX
Hypertext transfer protocol (http)
PPT
Coda file system
PDF
IEEE 802.11 Architecture and Services
PPT
Inter-Process communication in Operating System.ppt
PPTX
Application Layer
PPT
Disk scheduling
PPT
Domain name system
PPTX
GOOGLE FILE SYSTEM
PPTX
Visual Programming
PPTX
Ipv4 and Ipv6
PPT
File Allocation Methods.ppt
Dns server
Paging.ppt
IP addressing seminar ppt
TCP over wireless slides
Advanced Operating System- Introduction
Presentation on samba server
operating system structure
Guided Transmission Media
System Calls.ppt
Hypertext transfer protocol (http)
Coda file system
IEEE 802.11 Architecture and Services
Inter-Process communication in Operating System.ppt
Application Layer
Disk scheduling
Domain name system
GOOGLE FILE SYSTEM
Visual Programming
Ipv4 and Ipv6
File Allocation Methods.ppt
Ad

Similar to INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt (20)

PPT
sockets_intro.ppt
PPT
Sockets intro
PPT
Sockets
PPT
Introduction to sockets tcp ip protocol.ppt
PPT
03-socketprogramming for college students.ppt
PPT
03-socketprogrsamming forcoleeger students.ppt
PPTX
L5-Sockets.pptx
PPTX
Basics of sockets
PDF
lab04.pdf
PPTX
Socket programming
PDF
Network Programming Assignment Help
PDF
socketProgramming-TCP-and UDP-overview.pdf
PDF
PPT
Socket System Calls
PPT
Sockets in unix
PPT
Basic socket programming
PDF
PPT
Socket programming in C
PDF
Socket programming using C
PPT
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
sockets_intro.ppt
Sockets intro
Sockets
Introduction to sockets tcp ip protocol.ppt
03-socketprogramming for college students.ppt
03-socketprogrsamming forcoleeger students.ppt
L5-Sockets.pptx
Basics of sockets
lab04.pdf
Socket programming
Network Programming Assignment Help
socketProgramming-TCP-and UDP-overview.pdf
Socket System Calls
Sockets in unix
Basic socket programming
Socket programming in C
Socket programming using C
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
Ad

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Sustainable Sites - Green Building Construction
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Well-logging-methods_new................
PDF
Digital Logic Computer Design lecture notes
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
composite construction of structures.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Construction Project Organization Group 2.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Sustainable Sites - Green Building Construction
Automation-in-Manufacturing-Chapter-Introduction.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Well-logging-methods_new................
Digital Logic Computer Design lecture notes
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Internet of Things (IOT) - A guide to understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT 4 Total Quality Management .pptx
OOP with Java - Java Introduction (Basics)
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
composite construction of structures.pdf
R24 SURVEYING LAB MANUAL for civil enggi
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Operating System & Kernel Study Guide-1 - converted.pdf
Construction Project Organization Group 2.pptx

INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt

  • 2. Why do we need sockets? Provides an abstraction for interprocess communication
  • 3. • The services provided (often by the operating system) that provide the interface between application and protocol software. Application Network API Protocol A Protocol B Protocol C Definition
  • 4. Functions –Define an “end- point” for communication –Initiate and accept a connection –Send and receive data –Terminate a connection gracefully Examples File transfer apps (FTP), Web browsers (HTTP), Email (SMTP/ POP3), etc…
  • 5. Types of Sockets • Two different types of sockets : – stream vs. datagram • Stream socket :( a. k. a. connection- oriented socket) – It provides reliable, connected networking service – Error free; no out- of- order packets (uses TCP) – applications: telnet/ ssh, http, … • Datagram socket :( a. k. a. connectionless socket) – It provides unreliable, best- effort networking service – Packets may be lost; may arrive out of order (uses UDP) – applications: streaming audio/ video (realplayer), …
  • 7. Addresses, Ports and Sockets • Like apartments and mailboxes – You are the application – Your apartment building address is the address – Your mailbox is the port – The post-office is the network – The socket is the key that gives you access to the right mailbox
  • 8. Client – high level view Create a socket Setup the server address Connect to the server Read/write data Shutdown connection
  • 9. int connect_ socket( char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock == -1) return sock; host = gethostbyname( hostname); if (host == NULL) { close( sock); return -1; } memset (& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ port = htons( port); sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) { close (sock); return -1; } return sock; } Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup up the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ Hostent structure struct hostent{ char * h_name /*official name of host*/ char ** h_aliases; /* pointer ot array of pointers to alias name*/ int h_addrtype /* host address type*/ int h_length /* length of address */ char ** h_addr_list /*prt to array of ptrs with IPv4 or IPv6 address*/ } Ipv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr /* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ } Make the socket Socket(int family , int type, in t protocol); return nonnegative value for OK, -1 for error
  • 10. Server – high level view Create a socket Bind the socket Listen for connections Accept new client connections Read/write to client connections Shutdown connection
  • 11. Listening on a port (TCP) int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) return -1; memset(& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ addr. s_ addr = htonl( INADDR_ ANY); sin. sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) return -1; return sock; } Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/
  • 12. accepting a client connection (TCP) int get_ client_ socket( int listen_ socket) { struct sockaddr_ in sin; int sock; int sin_ len; memset(& sin, 0, sizeof( sin)); sin_ len = sizeof( sin); sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_ len); return sock; } Setup up the struct Accept the client connection accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen) /* return nonnegative descriptor if OK, -1 on error return the next completed connection from the front of the completed connection queue. if the queue is empty, the process is put to sleep(assuming blocking socket)*/
  • 13. Sending / Receiving Data • With a connection (SOCK_STREAM): – int count = send(sock, &buf, len, flags); • count: # bytes transmitted (-1 if error) • buf: char[], buffer to be transmitted • len: integer, length of buffer (in bytes) to transmit • flags: integer, special options, usually just 0 – int count = recv(sock, &buf, len, flags); • count: # bytes received (-1 if error) • buf: void[], stores received bytes • len: # bytes received • flags: integer, special options, usually just 0 – Calls are blocking [returns only after data is sent (to socket buf) / received]
  • 15. Dealing with blocking calls • Many functions block – accept(), connect(), – All recv() • For simple programs this is fine • What about complex connection routines – Multiple connections – Simultaneous sends and receives – Simultaneously doing non-networking processing
  • 16. Dealing with blocking (cont..) • Options – Create multi-process or multi-threaded code – Turn off blocking feature (fcntl() system call) – Use the select() function • What does select() do? – Can be permanent blocking, time-limited blocking or non- blocking – Input: a set of file descriptors – Output: info on the file-descriptors’ status – Therefore, can identify sockets that are “ready for use”: calls involving that socket will return immediately
  • 17. select function call • int status = select() – Status: # of ready objects, -1 if error – nfds: 1 +largest file descriptor to check – readfds: list of descriptors to check if read-ready – writefds: list of descriptors to check if write-ready – exceptfds: list of descriptors to check if an exception is registered – Timeout: time after which select returns