Socket Programmingin C
Socket Programmingin C
connecting processes
Dipak Kumar Swain
MCA 4th Semester
Regd No-1105227024
OverviewOverview
• 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 SocketsIntroduction 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 ApplicationClient 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 ApplicationClient 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
Client192.168.0.3
1343
5/11
Types of SocketTypes 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 APIsSocket APIs
Some Essential System calls used by TCP & UDP Socket
• socket ( ):
• bind( ):
• listen( ):
• connect( ):
• accept( ):
• send( ):
• recv( ):
• sendto( ):
• recvfrom( ):
• close( ):
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 descriptor
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 descriptor
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 success
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 success
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 success
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 success
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
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
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 success
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 success
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
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
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 error
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 error
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
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
Recvfrom System Call – Receive datagram packet from another UDP socketint 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 error
Recvfrom System Call – Receive datagram packet from another UDP socketint 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 error
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.
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 UDPSocket Programming With UDP
socket socket
recvfrom
sendto
sendto
recvfrom
bind
close
ServerClient
Request
Response
8/11
Socket Programming With TCPSocket Programming With TCP
Client Server
socket
connect
send/recv
close
accept
listen
bind
socket
recv/send
close
Connect
3-way handshake
9/11
ConclusionConclusion
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 ModelThread ModelWorker Pool Model
ReferencesReferences
• 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