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