Pages

Saturday, August 17, 2013

Network Programming -- TCP Program

Socket function:

#include <sys/socket.h>
int socket (int family, int type, int protocol);



The family specifies the protocol family

Family Description
AF_INET IPV4 protocol
AF_INET6 IPV6 protocol
AF_LOCAL unix domain protocol
AF_ROUTE routing sockets
AF_KEY key socket

Type Description
SOCK_STREAM Stream description
SOCK_DGRAM Datagram socket
SOCK_RAW Raw socket

Connect function:
The connect function is used by a TCP client to establish a connection
with a TCP server.
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

Bind function: 
The bind function assigns a local protocol address to a socket.
int bind(int sockfd, const struct sockaddr *myaddr, s ocklen_t addrlen);

Close function:
The normal UNIX close function is also used to close a socket and terminate a TCP
connection.
#include<unistd.h>
int close(int sockfd);
Return 0 if ok, -1 on error.

Listen function: 
The second argument to this function specifies the maximum number of
connection that the kernel should queue for this socket.
int listen(int sockfd, int backlog);

Accept function: 
The cliaddr and addrlen argument are used to ret urn the protocol address of the
connected peer processes (client)
int accept(int sockfd, struct sockaddr *cliaaddr, socklen_t *addrlen);

IPv4 Socket Address Structure:
An IPv4 socket address structure, commonly called an “ Internet socket address
structure, “ is named sockaddr_in and defined by including the <netinet/in.h> header.








No comments:

Post a Comment