Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

socket

socket - create an endpoint for


communication
• The socket Function
• To perform network I/O, the first thing a
process must do is, call the socket function,
specifying the type of communication protocol
desired and protocol family, etc.
• #include <sys/types.h>
• #include <sys/socket.h>
• int socket (int family, int type, int protocol);
Parameters
• family − It specifies the protocol family and is
one of the constants shown below −
• FamilyDescription AF_INET IPv4 protocols
AF_INET6IPv6 protocols
• Type Description
• SOCK_STREAM Stream socket
SOCK_DGRAM Datagram socket
The connect Function

• The connect function is used by a TCP client to


establish a connection with a TCP server.
• #include <sys/types.h>
• #include <sys/socket.h>
• int connect(int sockfd, struct sockaddr
*serv_addr, int addrlen);
Parameters
• sockfd − It is a socket descriptor returned by
the socket function.
• serv_addr − It is a pointer to struct sock addr
that contains destination IP address and port.
• addrlen − Set it to sizeof(struct sockaddr).
The bind Function
• The bind function assigns a local protocol address
to a socket.
• With the Internet protocols, the protocol address is
the combination of either a 32-bit IPv4 address or a
128-bit IPv6 address, along with a 16-bit TCP or
UDP port number. This function is called by TCP
server only.
• #include <sys/types.h>
• #include <sys/socket.h>
• int bind(int sockfd, struct sockaddr *my_addr,int
addrlen);
Parameters
• sockfd − It is a socket descriptor returned by
the socket function.
• my_addr − It is a pointer to struct sockaddr
that contains the local IP address and port.
• addrlen − Set it to sizeof(struct sockaddr).
The listen Function
• The listen function is called only by a TCP server and it performs two
actions −
• The listen function converts an unconnected socket into a passive
socket, indicating that the kernel should accept incoming connection
requests directed to this socket.
• The second argument to this function specifies the maximum number of
connections the kernel should queue for this socket.
• #include <sys/types.h>
• #include <sys/socket.h>
• int listen(int sockfd,int backlog);
• This call returns 0 on success, otherwise it returns -1 on error.
• Parameters
• sockfd − It is a socket descriptor returned by the socket function.
• backlog − It is the number of allowed connections.
The accept Function

• The accept function is called by a TCP server


to return the next completed connection from
the front of the completed connection queue.
The signature of the call is as follows −
• #include <sys/types.h>
• #include <sys/socket.h>
• int accept (int sockfd, struct sockaddr *cliaddr,
socklen_t *addrlen);
Parameters

• sockfd − It is a socket descriptor returned by


the socket function.
• cliaddr − It is a pointer to struct sockaddr that
contains client IP address and port.
• addrlen − Set it to sizeof(struct sockaddr).
The send Function
• The send function is used to send data over stream
sockets or CONNECTED datagram sockets. If you want to
send data over UNCONNECTED datagram sockets, you
must use sendto() function.
• int send(int sockfd, const void *msg, int len, int flags);
• Parameters
• sockfd − It is a socket descriptor returned by the socket
function.
• msg − It is a pointer to the data you want to send.
• len − It is the length of the data you want to send (in
bytes).
• flags − It is set to 0.
The recv Function
• The recv function is used to receive data over
stream sockets or CONNECTED datagram sockets.
If you want to receive data over UNCONNECTED
datagram sockets you must use recvfrom().
• int recv(int sockfd, void *buf, int len, unsigned int
flags);
• Parameters
• sockfd − It is a socket descriptor returned by the
socket function.
• buf − It is the buffer to read the information into.
• len − It is the maximum length of the buffer.
• flags − It is set to 0.

You might also like