Socket programming in C

deepakswain902 1,155 views 11 slides Oct 15, 2013
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

No description available for this slideshow.


Slide Content

Socket Programming
in C
connecting
processes
Dipak Kumar Swain
MCA 4
th
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
Server
Client
Client
192.168.0.1
192.168.0.2
192.168.0.2
80
1343
5488
Client
192.168.0.3
1343
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
Some Essential System calls used by TCP & UDP Socket
•socket ( ):
•bind( ):
•listen( ):
•connect( ):
•accept( ):
•send( ):
•recv( ):
•sendto( ):
•recvfrom( ):
•close( ): onetig citg o no etig io gpo ipro
sp oei ootoepeg p
perioei
igconeti ig coneti gig g pr gig
psogoo p
i tpogpoppirop iitoegiro
ei oriets t
Socket System Call – create an end
point socket for communication
#include <sys/socket.h>
int socket(int domain, int type, int
protocol);
Returns an unsigned integer called
socket descriptorgcitg onprogpo o grrtio
gproost op ito! ogoei
perioei
igs gig oc gcinigg
coneg rseccs gigg eccsri p
i tpo"opoeei
Bind System Call –Bind an IP address
and port number with a socket
#include <sys/socket.h>
int bind( int sockfd, struct
sockaddr *serv_addr, int addrlen);
Returns 0 on successcitg citg r o #iro o $it%ito
teio po &'o to sg%io !g po
toeppie p
perioei
igcit gig oc gigsneo p
i tpo"opoeei
Listen System Call:– Used by Server
Process in TCP for passively waiting
for connection
#include <sys/socket.h>
int listen( int sockfd, int backlog);
Returns 0 on successotnigcitg onop g iogo
eppie popo&'o$ei
perioei
ignotni gig oc gcinig
coneg eccstigeccsri p
i tpo"opoeei
Connect System Call – Initiates a
connection on TCP Socket.
#include <sys/socket.h>
int connect( int sockfd, struct
sockaddr *addr, int addrlen);
Returns 0 on success!nntigcitgr no(eeis ogo
eppie potooeip oei
perioei
ignnti gig oc gcinig
coneg eccstigeccsri p
i tpogopp)pig %ioriets topo
eei
Accept System Call:– Accept a
connection from client socket.
#include <sys/socket.h>
int accept( int sockfd, struct
sockaddr *addr, int *addrlen);
Returns a non-negative descriptor on
successtgcitg ono$iprorg go o
gp ito&'oei
perioei
igct gig oc gogc tig
trtige
i tpop itooegtge itoip opo
eei
Send System Call – Send data to
another TCP socket.
#include <sys/socket.h>
int send( int sockfd, void *msg, int
size, int flag);
Returns number of characters sent on
success"tngcitg onoiei%iorg goto
gp ito&'oei
perioei
igtn gig oc gogc tig
trtige
i tpop itooegtge itoip opo
eeioto)*opoittt
Recv System Call – Receive data from
another TCP socket.
#include <sys/socket.h>
int recv( int sockfd, void *msg, int
size, int flag);
Returns number of characters sent on
success or -1 on errortiogcitg ono$iprorg gtgo
o iosieiro#+oei
igctio gig oc gogc tig
io grtigegsgoeccs
gotigo t i#
i tpop itoo io!t ipo o
ei opoeeioto)*opoittt
Sendto System Call – Send datagram
to the specified UDP socket.
int sendto( int sockfd, void *msg, int
noOfbytes, int flags, struct sockaddr
*to, int *sock_length);
Returns number of bytes written to
socket on success or -1 on error"tnogcitg onoiei%io
rg gtgosgei otogp ito#+o
ei
igtno gig oc gogc tig
io grtigegsgoeccsson
tigo t i#
i tpop itoo iotigroto
ei opoeeioto)*opoittt
Recvfrom System Call – Receive
datagram packet from another UDP
socket
int recvfrom( int sockfd, void *msg, int
noOfbytes, int flags, struct sockaddr *from,
int *sock_length);
Returns number of bytes read from
socket on success or -1 on erroroctgcitg ono'io io
epeg poegppio i !iipo
eip o,oit%it
ignoct igconep
i tpo"opoeeio,oeroo io
ei oriets to!eo!o io
eiro
Close System Call – Closes the
communication channel between
client & server.
int close(int sockfd);
Return 0 on success & sockfd is the
socket descriptor which will be
closed.
7/11

Socket Programming With UDP
socket socket
recvfrom
sendto
sendto
recvfrom
bind
close
ServerClient
Request
Response
8/11

Socket Programming With TCP
Client Server
socket
connect
send/recv
close
accept
listen
bind
socket
recv/send
close
Connect
3-way handshake
9/11

Conclusion
10/11
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

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
Tags