SlideShare uma empresa Scribd logo
1 de 11
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Semelhante a 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
 

Último

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

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