Four common functions:
1) socket ()
Header file:
# Include <sys/types.h>
# Include <sys/socket.h>
Function prototype:
Int socket (INT domain, int type, int Protocol)
Domain: protocol type, generally af_inet
Type: Socket Type
Protocol: used to specify the number of the transport protocol used by the socket. It is usually
set to 0.
2) BIND ()
Header file:
# Include <sys/types.h>
# Include <sys/socket.h>
Function prototype:
Int BIND (INT sockfd, struct sockaddr * my_addr, int addrlen)
Sockfd: Socket Descriptor
My_addr: A sockaddr pointer pointing to information including the local IP address and port
number.
Addrlen: usually set to sizeof (struct sockaddr)
3) connect ()
Header file:
# Include <sys/types.h>
# Include <sys/socket.h>
Function prototype:
Int connect (INT sockfd, struct sockaddr * serv_addr, int addrlen)
Sockfd: Socket descriptor of the target server
Serv_addr: pointer containing the IP address and port number of the target machine
Addrlen: sizeof (struct sockaddr)
4 listen ()
Header file:
# Include <sys/socket.h>
Function prototype:
Int listen (INT sockfd, int backlog );
Sockfd: Socket descriptor returned by the socket () system call
Backlog: specify the maximum number of requests in the Request queue. incoming
connection requests will wait for accept () in the queue.
5 accept ()
Header file:
# Include <sys/types.h>
# Inlcude <sys/socket.h>
Function prototype:
Int accept (INT sockfd, void * ADDR, int addrlen)
Sockfd: indicates the socket descriptor to be monitored.
ADDR: Usually a pointer to the sockaddr_in variable, which is used to store the information
of the host requesting the Connection Service.
Addrlen: sizeof (struct sockaddr_in)
6 send ()
Header file:
# Include <sys/socket.h>
Function prototype:
Int send (INT sockfd, const void * MSG, int Len, int flags );
Sockfd: Socket descriptor used to transmit data
MSG: pointer to send data
Flags: 0
7 Recv ()
Header file:
# Include <sys/types.h>
# Include <sys/socket.h>
Function prototype:
Int Recv (INT sockfd, void * Buf, int Len, unsigned int flags)
Sockfd: Socket descriptor for receiving data
Buf: buffer for storing data
Len: Buffer Length
Flags: 0
8 sendto ()
Header file:
# Include <sys/types.h>
# Include <sys/socket.h>
Function prototype:
Int sendto (INT sockfd, const void * MSG, int Len, unsigned int flags, const struct sockaddr *To, int tolen );
9 recvfrom ()
Header file:
# Include <sys/types.h>
# Include <sys/socket.h>
Function prototype:
Int recvfrom (INT sockfd, void * Buf, int Len, unsigned int flags, struct sockaddr * From, int fromlen)
10 read () write ()
Int read (int fd, char * Buf, int Len)
Int write (int fd, char * Buf, int Len)
11 Shutdown ()
Close (sockfd)
Int Shutdown (INT sockfd, int how)
Result: Various Functions related to socket programming were studied. Those are
the part of Socket IO and Basic Functions of Socket Programming.