SlideShare a Scribd company logo
Socket Programming
in C
connecting
processes

Dipak Kumar Swain
MCA 4th Semester
Regd No-1105227024
Overview
• Introduction to Sockets
• A generic Client-Server application
• Types of Socket
• Socket APIs
• Socket Programming with TCP
• Socket Programming with UDP

• Conclusion
• References
2/11
Introduction to Sockets

What is a Sockets?
• Socket is an interface between application and network
which is used for communication between processes
• Data can be sent to or received through a socket from
another process running on the same machine or a
different machine
• Socket internally represents the three things:
– Protocol
– IP address
– Port number

3/11
Client Server Application
Most interprocess communication uses client-server model
•
•
•

Server waits for client to request a connection and Client contacts
server to establish a connection.
Client sends request and Server sends reply.
Client and/or server terminate connection.

4/11
Client Server Application

•

What makes a connection?
{Source<IP address, Port #> , Destination <IP address, Port #>}
i.e. source socket – destination socket pair uniquely identifies a connection.

•

Example

1343

Client

192.168.0.2

Server

80

1343

Client

192.168.0.3

192.168.0.1
5488

Client

192.168.0.2

5/11
Types of Socket
Two essential types of sockets :
– STREAM Socket – Treat communications as a continuous stream
of characters.
– DATAGRAM Socket– Read entire messages at once.
Stream Socket(SOCK_TCP)

Datagram
Socket(SOCK_DGRAM)

TCP

UDP

Connection Oriented

Connection Less

Reliable Delivery

Unreliable Delivery

In-Order Guaranteed

No-Order Guaranteed

Bidirectional

Can Send Or Receive

6/11
Socket APIs
Socket System Call – create an end
Socket System Call UDP Socketend
Some Essential System calls used byCall –Bind createaddress
TCP & – an IP an
Bind Systemfor communication
Bind System Call –Bind an IP address
point socket for communication
point socket
Listen System Call:– Used by Server
Listen System Call:– Used by
• socket ( ):
and port number with a socket Server
and port<sys/socket.h> socket
number with a
#includein TCP for passively waiting
#includeSystem for passively waiting
<sys/socket.h>
Connect in TCP Call – Initiates a
Connect
Process System Call – Initiates a
Process
#include <sys/socket.h>
#include <sys/socket.h>
• bind( ):
Accept SystemTCPSocket. a
Accept SystemCall:– Accept a
connection on TCP Socket. type, int
connection on domain, int
for connection domain, int type, int
for connection Call:– Accept
int socket(intCall– Send data to
int socket(int
Send Systemsockfd, Send data to
Send SystemCallclient
connection from client socket.
connection from – struct
int bind(<sys/socket.h>socket.
int
int bind(<sys/socket.h>
• listen( ):
#includeTCP socket. Receive data from
#includeTCP sockfd, struct
protocol);intsocket.Receive data from
protocol); Call –
Recv System Call –
Recv System
another <sys/socket.h> addrlen);
another *serv_addr, int
sockaddr<sys/socket.h> addrlen);
#includeSystemCall – Send datagram
sockaddr *serv_addr,Send
int
#include
Sendto System Call integer datagram
Sendto TCPunsigned –intstruct
another an unsigned integercalled
another <sys/socket.h> backlog);
int listen( intintsockfd, struct
• connect( ):
intconnect( int sockfd,
listen( intsockfd,
sockfd,
Returns TCPsocket. intbacklog);
connect(socket.
Returns <sys/socket.h>
#include anSystemCallstructcalled
#include System Call– Receive
Recvfromonintsockfd, struct
Recvfrom*addr,
intthe specified UDP socket.
to the specified sockfd, – Receive
intaccept(*addr, intsocket.
to accept(int UDP addrlen);
#include0<sys/socket.h>
Returnsdescriptorint addrlen);
#include on Call
sockaddr<sys/socket.h> the
Returns 0on success
socketSystemsockfd,Closes*msg, int
sockaddr int success void
socketdescriptor – void the
Returns 0 int success *addrlen); int
Close Systemsockfd,Closes*msg,
• accept( ):
int send( packet from*addrlen); int
Returns 0 packet from void *msg,
Close
int send( on success void *msg,
datagram *addr, int another UDP int
datagram *addr, int
sockaddrintintCall – void *msg, int
sockaddr sockfd,
int sendto( intsockfd, another UDP
int sendto(
sockfd,
int recv( intsockfd, void *msg, int
int recv( on success
communication channel between
size, int 0 on flags, between
communication
Returns flag);success struct sockaddr
size, int 0 int
Returns
socket flag); flags, struct sockaddr
socket a non-negative descriptor
• send( ):
noOfbytes, int channeldescriptoron
noOfbytes,
Returns server.
size, int flag);
Returns server.
size, int flag);
client & numbersockfd,void *msg,senton
client & a non-negative *msg,int on
Returns*sock_length);
Returns number of characters int
int recvfrom( int sockfd, void
int int
*to,recvfrom( int of characters sent on
*to, int
success *sock_length);
success
• recv( ):
Returns number of characters sent on
Returns int flags,struct sockaddr *from,
noOfbytes,number of characters sent on
int close(intflags,of bytes written*from,
noOfbytes,
int close(intsockfd);
sockfd);
success number of bytes writtento
success number struct
Returnsorint on error sockaddr to
Returnsor-1 on error
success
int *sock_length);
success -1
int *sock_length);
• sendto( ):
Returnonon success & sockfd is the
Returnonsuccess or & on error
socket 0 on success -1 sockfd is the
socket 0 success or -1 on error
Returnsdescriptorofbyteswill befrom
Returnsnumber which read from
socket descriptorofwhich will be
socket number bytes read
• recvfrom( ):
socket on success or -1 on error
socket
closed. on success or -1 on error
closed.

• close( ):

7/11
Socket Programming With UDP
Client

Server

socket

socket

bind

sendto

recvfrom

Request

Response

recvfrom

sendto

close

8/11
Socket Programming With TCP
Client

Server

socket

socket
bind
listen

connect

Connect
3-way handshake

accept

send/recv

recv/send

close

close

9/11
Conclusion
Not only message can send but also a file can send
through Socket.
To handle multiple client simultaneously , we can use
the following models at server side:
Process Model
Thread Model
Worker Pool Model

10/11
References

• Cystem Programming at the Roots… By Susant K Rout
and T Prabakaran
• UNIX Network Programming, by Richard Stevens.
• LINUX man page
Accessible through following command
– man 2 <system_call_name>
– E.g. man 2 socket

11/11

More Related Content

PPTX
Socket programming in c
PDF
Socket programming
PDF
Socket programming using C
PPTX
Socket programming
PPTX
Socket programming
PPTX
Socket Programming
PPTX
Socket programming
PPTX
Elementary TCP Sockets
Socket programming in c
Socket programming
Socket programming using C
Socket programming
Socket programming
Socket Programming
Socket programming
Elementary TCP Sockets

What's hot (20)

PPT
Socket programming
PPT
Application Layer and Socket Programming
PDF
PPT
Basic socket programming
PDF
Programming TCP/IP with Sockets
PPT
Socket Programming Tutorial
PPT
Socket Programming
PPT
Ppt of socket
PDF
Network Sockets
PPT
Socket Programming it-slideshares.blogspot.com
PPT
Socket System Calls
PDF
Socket Programming
PPT
Socket programming-tutorial-sk
PDF
Lecture10
PPTX
Socket programing
PPT
Sockets
PPT
Tcp sockets
PDF
Sockets
PDF
Advanced Sockets Programming
DOC
socket programming
Socket programming
Application Layer and Socket Programming
Basic socket programming
Programming TCP/IP with Sockets
Socket Programming Tutorial
Socket Programming
Ppt of socket
Network Sockets
Socket Programming it-slideshares.blogspot.com
Socket System Calls
Socket Programming
Socket programming-tutorial-sk
Lecture10
Socket programing
Sockets
Tcp sockets
Sockets
Advanced Sockets Programming
socket programming
Ad

Similar to Socket programming in C (20)

PPT
Sockets
PPTX
Basics of Socket Programming using python
PDF
JavaSockets-Session10 New York university.pdf
PPT
Socket Programming
PPT
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
PDF
Lecture set 7
PPT
Socket Programming Tutorial 1227317798640739 8
PPTX
L5-Sockets.pptx
PPT
lecture03for socket programming college.ppt
PPT
lecture03 on socket programming000000.ppt
PPT
Net Programming.ppt
PPTX
EN-04 (1).pptx
PPTX
Java socket programming
PPTX
Byte Ordering - Unit 2.pptx
PPT
Network Prog.ppt
PPT
Network programming-Network for engineering
PPT
Lan chat system
PPT
LECTURE-17(Socket Programming) Detailed.
PPT
Socket programming
PPTX
Networking in Python2025 (programs allll)
Sockets
Basics of Socket Programming using python
JavaSockets-Session10 New York university.pdf
Socket Programming
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
Lecture set 7
Socket Programming Tutorial 1227317798640739 8
L5-Sockets.pptx
lecture03for socket programming college.ppt
lecture03 on socket programming000000.ppt
Net Programming.ppt
EN-04 (1).pptx
Java socket programming
Byte Ordering - Unit 2.pptx
Network Prog.ppt
Network programming-Network for engineering
Lan chat system
LECTURE-17(Socket Programming) Detailed.
Socket programming
Networking in Python2025 (programs allll)
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
A Presentation on Artificial Intelligence
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
Spectral efficient network and resource selection model in 5G networks
A Presentation on Artificial Intelligence
NewMind AI Weekly Chronicles - August'25 Week I
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Monthly Chronicles - July 2025
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology

Socket programming in C

  • 1. Socket Programming in C connecting processes Dipak Kumar Swain MCA 4th Semester Regd No-1105227024
  • 2. Overview • Introduction to Sockets • A generic Client-Server application • Types of Socket • Socket APIs • Socket Programming with TCP • Socket Programming with UDP • Conclusion • References 2/11
  • 3. Introduction to Sockets What is a Sockets? • Socket is an interface between application and network which is used for communication between processes • Data can be sent to or received through a socket from another process running on the same machine or a different machine • Socket internally represents the three things: – Protocol – IP address – Port number 3/11
  • 4. Client Server Application Most interprocess communication uses client-server model • • • Server waits for client to request a connection and Client contacts server to establish a connection. Client sends request and Server sends reply. Client and/or server terminate connection. 4/11
  • 5. Client Server Application • What makes a connection? {Source<IP address, Port #> , Destination <IP address, Port #>} i.e. source socket – destination socket pair uniquely identifies a connection. • Example 1343 Client 192.168.0.2 Server 80 1343 Client 192.168.0.3 192.168.0.1 5488 Client 192.168.0.2 5/11
  • 6. Types of Socket Two essential types of sockets : – STREAM Socket – Treat communications as a continuous stream of characters. – DATAGRAM Socket– Read entire messages at once. Stream Socket(SOCK_TCP) Datagram Socket(SOCK_DGRAM) TCP UDP Connection Oriented Connection Less Reliable Delivery Unreliable Delivery In-Order Guaranteed No-Order Guaranteed Bidirectional Can Send Or Receive 6/11
  • 7. Socket APIs Socket System Call – create an end Socket System Call UDP Socketend Some Essential System calls used byCall –Bind createaddress TCP & – an IP an Bind Systemfor communication Bind System Call –Bind an IP address point socket for communication point socket Listen System Call:– Used by Server Listen System Call:– Used by • socket ( ): and port number with a socket Server and port<sys/socket.h> socket number with a #includein TCP for passively waiting #includeSystem for passively waiting <sys/socket.h> Connect in TCP Call – Initiates a Connect Process System Call – Initiates a Process #include <sys/socket.h> #include <sys/socket.h> • bind( ): Accept SystemTCPSocket. a Accept SystemCall:– Accept a connection on TCP Socket. type, int connection on domain, int for connection domain, int type, int for connection Call:– Accept int socket(intCall– Send data to int socket(int Send Systemsockfd, Send data to Send SystemCallclient connection from client socket. connection from – struct int bind(<sys/socket.h>socket. int int bind(<sys/socket.h> • listen( ): #includeTCP socket. Receive data from #includeTCP sockfd, struct protocol);intsocket.Receive data from protocol); Call – Recv System Call – Recv System another <sys/socket.h> addrlen); another *serv_addr, int sockaddr<sys/socket.h> addrlen); #includeSystemCall – Send datagram sockaddr *serv_addr,Send int #include Sendto System Call integer datagram Sendto TCPunsigned –intstruct another an unsigned integercalled another <sys/socket.h> backlog); int listen( intintsockfd, struct • connect( ): intconnect( int sockfd, listen( intsockfd, sockfd, Returns TCPsocket. intbacklog); connect(socket. Returns <sys/socket.h> #include anSystemCallstructcalled #include System Call– Receive Recvfromonintsockfd, struct Recvfrom*addr, intthe specified UDP socket. to the specified sockfd, – Receive intaccept(*addr, intsocket. to accept(int UDP addrlen); #include0<sys/socket.h> Returnsdescriptorint addrlen); #include on Call sockaddr<sys/socket.h> the Returns 0on success socketSystemsockfd,Closes*msg, int sockaddr int success void socketdescriptor – void the Returns 0 int success *addrlen); int Close Systemsockfd,Closes*msg, • accept( ): int send( packet from*addrlen); int Returns 0 packet from void *msg, Close int send( on success void *msg, datagram *addr, int another UDP int datagram *addr, int sockaddrintintCall – void *msg, int sockaddr sockfd, int sendto( intsockfd, another UDP int sendto( sockfd, int recv( intsockfd, void *msg, int int recv( on success communication channel between size, int 0 on flags, between communication Returns flag);success struct sockaddr size, int 0 int Returns socket flag); flags, struct sockaddr socket a non-negative descriptor • send( ): noOfbytes, int channeldescriptoron noOfbytes, Returns server. size, int flag); Returns server. size, int flag); client & numbersockfd,void *msg,senton client & a non-negative *msg,int on Returns*sock_length); Returns number of characters int int recvfrom( int sockfd, void int int *to,recvfrom( int of characters sent on *to, int success *sock_length); success • recv( ): Returns number of characters sent on Returns int flags,struct sockaddr *from, noOfbytes,number of characters sent on int close(intflags,of bytes written*from, noOfbytes, int close(intsockfd); sockfd); success number of bytes writtento success number struct Returnsorint on error sockaddr to Returnsor-1 on error success int *sock_length); success -1 int *sock_length); • sendto( ): Returnonon success & sockfd is the Returnonsuccess or & on error socket 0 on success -1 sockfd is the socket 0 success or -1 on error Returnsdescriptorofbyteswill befrom Returnsnumber which read from socket descriptorofwhich will be socket number bytes read • recvfrom( ): socket on success or -1 on error socket closed. on success or -1 on error closed. • close( ): 7/11
  • 8. Socket Programming With UDP Client Server socket socket bind sendto recvfrom Request Response recvfrom sendto close 8/11
  • 9. Socket Programming With TCP Client Server socket socket bind listen connect Connect 3-way handshake accept send/recv recv/send close close 9/11
  • 10. Conclusion Not only message can send but also a file can send through Socket. To handle multiple client simultaneously , we can use the following models at server side: Process Model Thread Model Worker Pool Model 10/11
  • 11. References • Cystem Programming at the Roots… By Susant K Rout and T Prabakaran • UNIX Network Programming, by Richard Stevens. • LINUX man page Accessible through following command – man 2 <system_call_name> – E.g. man 2 socket 11/11