SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Tendances (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
 

Similaire à 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
 

Dernier

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Dernier (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

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