Sockets in C++
Sockets are used to create connection between processes.
we can create socket using TCP or UDP. In Connection based (TCP), socket communicate client and server.We done this by using some of socket APIs
In Server Side:
1. socket
2. bind
3. listen
4. accept
5. send
6. recv
7. shutdown
8. close
In Client Side:
1. socket
2. connect
3. send
4. recv
5. shutdown
6. close
1.Socket
Used for creating socket.
Syntax:
int socket ( int domain , int type , int protocol ) ;
Here,
domain - AF UNIX(address format is UNIX pathname) or
AF INET (address format is host and port number)
type - SOCK_STREAM (Establishes a virtual circuit for stream)
SOCK_DGRAM (Establishes a datagram for communication)
SOCK SEQPACKET( Establishes a reliable, connection based,
two way communication with maximum message size.)
protocol - usually zero, so that type defines the connection within domain.
bind:
Give the address of the socket on the server
Syntax:
int bind ( int sid , struct sockaddr *addrPtr , int len )
Here:
sid - socket id
addrPtr - a pointer to the address family dependent address structure
len - size of *addrPtr
listen:
Specifies the maximum number of connection requests that can be pending for this process
Syntax:
int listen ( int sid , int size ) ;
accept:
Establish the connection with a specific client
Syntax:
int accept ( int sid , struct sockaddr *addrPt r , int * lenPt r )
send,recv:
Stream-based equivalents of read and write
Syntax:
int send ( int sid , const char *bufferPtr ,int len , int flag )
int recv ( int sid , const char *bufferPtr ,int len , int flag )
connect:
Client requests a connection request to a server
Syntax:
int connect (int sid , struct sockaddr *addrPt r , int len )
shutdown:
End reading or writing
Syntax:
int shutdown ( int sid , int how)
close:
Release kernel data structures
