Sockets Tutorial
Sockets Tutorial
What is a socket?
Socket: An interface between an application process and transport layer
The application process can send/receive messages to/from another application process (local or remote)via a socket
In Unix jargon, a socket is a file descriptor an integer associated with an open file Types of Sockets: Internet Sockets, unix sockets, X.25 sockets etc
Internet sockets characterized by IP Address (4 bytes) and port number (2 bytes)
Socket Description
Background
Two types of Byte ordering
Network Byte Order: High-order byte of the number is stored in memory at the lowest address Host Byte Order: Low-order byte of the number is stored in memory at the lowest address Network stack (TCP/IP) expects Network Byte Order
Client
Conversions:
htons() - Host to Network Short htonl() - Host to Network Long ntohs() - Network to Host Short ntohl() - Network to Host Long
Connectionless Protocol
Server
socket() bind() socket() bind()
Client
socket structures
struct sockaddr: Holds socket address information for many types of sockets
struct sockaddr { unsigned short sa_family; //address family AF_xxx unsigned short sa_data[14]; //14 bytes of protocol addr }
struct sockaddr_in: A parallel structure that makes it easy to reference elements of the socket address
struct sockaddr_in { short int unsigned short int struct in_addr unsigned char } sin_family; sin_port; sin_addr; sin_zero[8]; // set to AF_INET // Port number // Internet address //set to all zeros
connect() - Hello!
Connects to a remote host int connect(int sockfd, struct sockaddr *serv_addr, int addrlen) sockfd is the socket descriptor returned by socket() serv_addr is pointer to struct sockaddr that contains information on destination IP address and port addrlen is set to sizeof(struct sockaddr) returns -1 on error At times, you don't have to bind() when you are using connect()
int recvfrom(int sockfd, void *buf, int len, int flags, struct sockaddr *from, int *fromlen);
from is a pointer to a local struct sockaddr that will be filled with IP address and port of the originating machine fromlen will contain length of address stored in from
Miscellaneous Routines
int getpeername(int sockfd, struct sockaddr *addr, int *addrlen);
Will tell who is at the other end of a connected stream socket and store that info in addr
Miscellaneous Routines
struct hostent *gethostbyname(const char *name);
struct hostent { char *h_name; //official name of host char **h_aliases; //alternate names for the host int h_addrtype; //usually AF_NET int h_length; //length of the address in bytes char **h_addr_list; //array of network addresses for the host } #define h_addr h_addr_list[0]
Example Usage:
struct hostent *h; h = gethostbyname(www.iitk.ac.in); printf(Host name : %s \n, h->h_name); printf(IP Address: %s\n,inet_ntoa(*((struct in_addr *)h->h_addr)));
Summary
Books: Sockets help application process to communicate with each other using standard Unix file descriptors Two types of Internet sockets: SOCK_STREAM and SOCK_DGRAM Many routines exist to help ease the process of communication
References
Unix Network Programming, volumes 1-2 by W. Richard Stevens. TCP/IP Illustrated, volumes 1-3 by W. Richard Stevens and Gary R. Wright Web Resources: Beej's Guide to Network Programming www.ecst.csuchico.edu/~beej/guide/net/