0% found this document useful (0 votes)
154 views8 pages

Introduction To Socket Programming

This document provides an introduction to socket programming. It discusses key concepts like clients, servers, sockets and socket addresses. It also explains important socket functions like socket(), bind(), connect(), listen(), accept(), read(), write(), sendto() and recvfrom(). Socket functions are used to create sockets, bind addresses, establish connections, listen for connections, accept connections, read/write data and close sockets for both connection-oriented TCP and connection-less UDP communication.

Uploaded by

Angayarkanni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
154 views8 pages

Introduction To Socket Programming

This document provides an introduction to socket programming. It discusses key concepts like clients, servers, sockets and socket addresses. It also explains important socket functions like socket(), bind(), connect(), listen(), accept(), read(), write(), sendto() and recvfrom(). Socket functions are used to create sockets, bind addresses, establish connections, listen for connections, accept connections, read/write data and close sockets for both connection-oriented TCP and connection-less UDP communication.

Uploaded by

Angayarkanni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 8

INTRODUCTION TO SOCKET PROGRAMMING

Network Programming Network programming involves writing programs that communicate with other programs across a computer N/W. One program is normally called the client, and the other the server. Common examples in the TCP/IP are web clients (browsers) & Web Servers, FTP clients & server and Telnet clients & servers. To facilitate communication between unrelated processes, and to standardize network programming, an API is needed. There are two such APIs: 1. Sockets, sometimes called Berkeley Sockets 2. XTI (X/open transport interface) Socket In TCP/IP, an addressable point that consists of an IP address and a TCP or UDP port member that provides application with access to TCP/IP protocol is called Socket. A socket is an abstraction that represents an endpoint of communication. The operations that can be performed on a socket include control operations (such as associating a port number with the socket, initiating or accepting a connection on the socket, or destroying the socket), data transfer operations (such as writing data through the socket to some other application, or reading data from some other application through the socket) and status operations (such as finding the IP address associated with the socket). The complete set of operations that can be performed on a socket constitutes the Sockets API (Application Programming Interface). Structures Structures are used in socket programming to hold information about the address. The generic socket address structure is defined below: struct sockaddr { unsigned short sa_family; /* address family */ char sa_data[14]; /* 14 bytes of protocol address*/ }; IPv4 Socket Address Structure This structure is also called as Internet socket address structure. It is defined by including the <netinet/in.h> header. struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address, network byte ordered */

}; struct sockaddr_in{ unit8_t sa_family_t in_port_t

sin_len; sin_family; sin_port;

struct in_addr sin_addr; char sin_zero[8]; };

/* length of structure (16 byte) */ /*AF_INET*/ /* 16-bit TCP or UDP port number */ /* network byte ordered */ /*32-bit Ipv4 address, network byte ordered */ /* unused initialize to all zeroes */

Important functions 1.socket() This function is called by both TCP server and client process to create an empty socket. #include <sys/socket.h> int socket (int family, int type, int protocol); family: specifies the protocol family and is one of the constants below: Family description
AF_INET AF_INET6 AF_LOCAL AF_ROUTE AF_KEY IPv4 protocols IPv6 protocols Unix domain protocols Routing sockets Key sockets

type: indicates communications semantics SOCK_STREAM - stream socket SOCK_DGRAM - datagram socket SOCK_RAW - raw socket protocol: set to 0 except for raw sockets. Returns on success: socket descriptor (a small nonnegative integer), on error: -1 2. bind() The bind function assigns a local protocol address to a socket. The protocol address is the combination of either a 32-bit IPV4 address or a 128-bit IPV6 address, along with a 16-bit TCP or UDP port number. #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); sockfd: a socket descriptor returned by the socket function.

*myaddr: a pointer to a protocol-specific address. addrlen: the size of the socket address structure. Returns on success: 0, on error: -1 3. connect() The connect function is used by a TCP client to establish a connection with a TCP server. #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); sockfd: a socket descriptor returned by the socket function *servaddr: a pointer to a socket address structure addrlen: the size of the socket address structure Returns on success: 0, on error: -1 4. listen() The listen function is called only by a TCP server to converts an unconnected socket into a passive socket, indicating that kernel should accept incoming connection requests directed to its socket. #include<sys/socket.h> int listen (int sockfd, int backlog); sockfd: a socket descriptor returned by the socket function. backlog: maximum number of connections that the kernel should queue for this socket. Returns on success: 0, on error: -1 5. accept() The accept function is called by the TCP server to return the next completed connection from the front of the completed connection queue. #include<sys/socket.h> int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); sockfd: This is the same socket descriptor as in listen call. *cliaddr: used to return the protocol address of the connected peer process *addrlen: length of the address. Returns on success: a new (connected)socket descriptor, on error:-1

6. close()
The close function is used to close a socket and terminate a TCP connection. #include <unistd.h> int close (int sockfd); sockfd: This socket descriptor is no longer useable. Returns on success: 0, on error: -1

7. read()
The read function is used to receive data from the specified socket. #include <unistd.h> ssize_t read(int sockfd, const void * buf, size_t nbytes); sockfd: a socket descriptor returned by the socket function. buf: buffer to store the data. nbytes: size of the buffer Returns: number of bytes read if OK,0 on EOF, -1 on error

8. write()
The write function is used to send the data through the specified socket. #include <unistd.h> ssize_t write(int sockfd, const void * buf, size_t nbytes); sockfd: a socket descriptor returned by the socket function. buf: buffer to store the data. nbytes: size of the buffer Returns: number of bytes written if OK,0 on EOF, -1 on error 9. sendto() This function is similar to the write function, but additional arguments are required. #include<sys/socket.h> ssize_t sendto(int sockfd, const void *buff, size_t nbyte, int flag, const struct sockaddr *to, socklen_t addrlen); sockfd socket descriptor *buff pointer to buffer to write from. nbytes number of bytes to write. to socket address structure containing the protocol address of where the data is to be sent. addrlen size of the socket address structure

Returns: number of bytes read or written if OK,-1 on error 10. recvfrom() This function is similar to the read function, but additional arguments are required. #include<sys/socket.h> ssize_t recvfrom(int sockfd, void *buff, size_t nbyte, int flag, struct sockaddr *from, socklen_t *addrlen); sockfd socket descriptor *buff pointer to buffer to read. nbytes number of bytes to read. addrlen size of the socket address structure from - socket address structure of who sent the datagram. Returns: number of bytes read or written if OK,-1 on error

Socket functions for connection-oriented communication TCP Server socket()

bind()

listen()

TCP Client socket() Connection establishment conect() data ( request) write()

accept() blocks until connection from client

read()

read()

process request data ( reply) write()

close()

EOF notification read()

close()

Socket functions for connection-less communication UDP Server socket()

UDP Client socket()

bind()

recvfrom() Blocks until datagram received from a client Process request Data (reply) recvfrom() sendto()

sendto()

Data (request)

close()

You might also like