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

What's hot

Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
elliando dias
 
Sockets
SocketsSockets
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
Kristian Arjianto
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
elliando dias
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
Jignesh Patel
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
Mostak Ahmed
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
Amandeep Kaur
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
Peter R. Egli
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
phanleson
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
Avinash Varma Kalidindi
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
elliando dias
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
sureshkarthick37
 
Lecture10
Lecture10Lecture10
Lecture10
vantinhkhuc
 
Socket programing
Socket programingSocket programing
Socket programing
Pankil Agrawal
 
Sockets
SocketsSockets
Sockets
sivindia
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
babak danyal
 
Sockets
SocketsSockets
Sockets
Rajesh Kumar
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
elliando dias
 
socket programming
socket programming socket programming
socket programming
prashantzagade
 

What's hot (20)

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

Similar to Socket programming in C

Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
ssuserec53e73
 
Sockets
Sockets Sockets
Sockets
Gopaiah Sanaka
 
Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
ssuserec53e73
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
Lecture set 7
Lecture set 7Lecture set 7
Lecture set 7
Gopi Saiteja
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8
shanmuga priya
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
ycelgemici1
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
EloAcubaOgardo
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
TienTran779192
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
Mohammed Abdalla Youssif
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
RockyBhai46825
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
EloOgardo
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
 
Socket programming
Socket programmingSocket programming
Socket programming
Divya Sharma
 
Socket Programming_theory.ppt
Socket Programming_theory.pptSocket Programming_theory.ppt
Socket Programming_theory.ppt
mdrobinhossain4
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Os 2
Os 2Os 2
java networking
 java networking java networking
java networking
Waheed Warraich
 
Chapter 4--converted.pptx
Chapter 4--converted.pptxChapter 4--converted.pptx
Chapter 4--converted.pptx
WijdenBenothmen1
 
Socket programming
Socket programmingSocket programming
Socket programming
chandramouligunnemeda
 

Similar to Socket programming in C (20)

Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
 
Sockets
Sockets Sockets
Sockets
 
Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Lecture set 7
Lecture set 7Lecture set 7
Lecture set 7
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket Programming_theory.ppt
Socket Programming_theory.pptSocket Programming_theory.ppt
Socket Programming_theory.ppt
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Os 2
Os 2Os 2
Os 2
 
java networking
 java networking java networking
java networking
 
Chapter 4--converted.pptx
Chapter 4--converted.pptxChapter 4--converted.pptx
Chapter 4--converted.pptx
 
Socket programming
Socket programmingSocket programming
Socket programming
 

Recently uploaded

TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 

Recently uploaded (20)

TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 

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