Introduction to Socket Programming Functions
What is Socket?
A socket is one endpoint of a two-way
communication link between two programs running on the network. The application
creates a socket. Socket is an interface between application layer and
transport layer.
It is an interface (a “door”) into which an application process can both send and receive message to/from another application process (remote/local application process). Socket is also referred as the application programmer’s interface (API) between the application and the network.
What is Socket Programming?
Socket programming is a way of connecting two nodes on a network to communicate with each other.
Which functions are used in socket
programming?
The main functions in <sys/socket.h>
are:
socket ()
bind ()
listen ()
connect ()
accept ()
send ()/recv ()/read ()/write ()/sendto ()/recvfrom
()
close ()
Socket () – A Connection Endpoint
Purpose: It creates socket. This function is used by client and server.
Syntax:
int socket (int family, int type, int protocol)
Example:
int socket (AF_INET, SOCK_STREAM, 0);
Bind () – Attaching to an IP and Port
Purpose: Attach itself to a specific port and IP address. This function is used
by server.
Syntax:
int bind (int sockfd, struct sockaddr *serv_addr, int addrlen)
sockfd = socket
descriptor returned by socket ()
serv_addr = It contains
server IP address and port.
addrlen = length of the
address in bytes.
Example:
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
int bind (sockfd, (struct sockaddr *)
&serv_addr, sizeof(serv_addr));
Connect () – connect to a service
Purpose: Connect to a server port. This function is used by client.
Syntax:
int connect (int sockfd, struct sockaddr
*serv_addr, int addrlen)
sockfd: socket
descriptor returned by socket ()
serv_addr: filled with all
the remote server details
addrlen: size of the
server_addr struct
Example:
int connect (sockfd, (struct sockaddr *)
&serv_addr, sizeof (serv_addr));
Listen () – Wait for a connection
Purpose: The server process calls listen to tell the kernel to initialize a wait
queue of connections for this socket. This function is used by server.
Syntax:
int listen (int sockfd, int backlog)
sockfd = socket
descriptor returned by socket ()
backlog = Maximum length
of the pending connections queue
Example:
int listen (sockfd, 10);
This will allow a maximum of 10 connections
to be in pending state.
Accept () – A new connection!
Purpose: Accept new connections from new clients. This function is used by server.
Syntax:
int accept (int sockfd, struct sockaddr
*cli_addr, int addrlen)
sockfd = socket
descriptor returned by socket ()
cli_addr = will hold the
new client’s information when accept returns
addrlen= pointer to size
of the client structure
Example:
struct sockaddr_in cli_addr;
int len = sizeof(client);
int accept (sock, (struct sockaddr *)
&cli_addr, &len);
Send () / Recv () – Stream Socket
Purpose: Send, Recv functions are used to send and receive data over stream
socket. This function is used by client and server.
Syntax:
int send (int sockfd, void *msg, int len, int flags)
int recv (int sockfd, void *msg, int len, int flags)
sockfd = socket
descriptor returned by socket ()
msg = It is the
pointer to a data you want to send/recv.
len = It is the
length of data; you want to send/recv.
flags = It is set to
0.
Example:
char send_msg [1024], recv_msg [1024];
int sent_bytes, recv_bytes;
sent_bytes = int send (sockfd, send_msg,
1024, 0);
recv_bytes= int recv (sockfd, recv_msg, 1024,
0);
Sendto () / Recvfrom () – Datagram Socket
Purpose: Sendto and Recvfrom functions are used to send and receive data over
datagram socket. This function is used by client and server.
Syntax:
int sendto (int sockfd, void *msg, int len, int flags, struct sockaddr *to,
int tolen);
int recvfrom (int sockfd, void *msg, int len, int flags, struct sockaddr
*from,
int fromlen);
sockfd = socket
descriptor returned by socket ()
msg = It is the
pointer to a data you want to send/recv.
len = It is the
length of data; you want to send/recv.
flags = It is set to
0.
to
= socket address for the host where data has to be sent.
tolen = size of socket
address (where data to be sent)
from
= socket address for the host where data has to be recv.
fromlen = size of sock
address (where data to be received)
Close () – Bye...Bye!
Purpose: Close signals the end of communication between a server-client pair.
This effectively closes the socket. This function is used by client and server.
Syntax:
int close (int sockfd)
sockfd = socket
descriptor returned by socket ()
Example:
int close(sockfd);
To learn more about Socket Programming Functions, Click here
Watch more videos click here.
Nicely explained
ReplyDeleteThank u so much.... Please follow my blog...
DeleteSubscribe my channel: Chirag Bhalodia