Introduction to Socket
Programming
Basic Model of Communication
Client
Application
Client Server
Layer
Client
Layers of TCP/IP Protocol
Suite
Physical layer
Data link layer
Network layer
Transport layer
Application layer
What is a Socket?
Socket Basics
Socket is an endpoint in a connection.
Socket address = ? + ?
IP add + Port num
A socket is referred to by using a file
descriptor.
Clients & Server
SOCKET
Server with many sockets Client
Server with many client connections
Types of Sockets
Stream sockets :- Connection oriented
and use TCP (reliable)
Datagram sockets :- Connectionless and
use UDP (unreliable)
Byte Ordering
3B4F
3B 4F
Memory location 1000 1008
Big Endian/Network Byte Order
4F 3B
Memory location 1000 1008
Little Endian/Host Byte Order
Conversion between Byte
Orderings
Network byte order
Host byte order Host byte order
Host Computer Host Computer
Functions used to convert b/w orderings are:
1. htons()
2. htonl()
3. ntohs()
4. ntohl()
Primitives
Server Side Client Side
1. socket() 1. socket()
2. bind() 2. connect()
3. listen() 3. send() and recv()
4. accept()
5. send() and recv()
Structures to be used
1. struct sockaddr
2. struct sockaddr_in
3. struct in_addr
Structure 1
struct sockaddr
{
unsigned short sa_family; /*address family, AF_xxx*/
char sa_data[14]; /*14 bytes of protocol address*/
};
Structure 2
struct sockaddr_in
{
short int sin_family; /* Address family*/
unsigned short int sin_port; /* Port number*/
struct in_addr sin_addr; /* Internet address */
unsigned char sin_zero[8];
/* To make it the same size as struct sockaddr*/
};
Structure 3
struct in_addr
{
unsigned long s_addr;
/*32-bit long, or 4 bytes of IP address*/
};
Dealing with IP addresses
inet_addr():- Converts an IP address in
numbers-and-dots notation into an
unsigned long.
inet_aton():- Cleaner interface.
Prototypes
1. int socket( int domain, int type, int protocol );
2. int bind(int sockfd, struct sockaddr*my_addr, int addrlen);
3. connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
4. int listen(int sockfd, int backlog);
5. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
6. int send(int sockfd, const void *msg, int len, int flags);
7. int recv(int sockfd, void *buf, int len, unsigned int flags);
8. close(sockfd);
9. int shutdown(int sockfd, int how);
Libraries
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
Sample Code 1:-Initialization
struct sockaddr_in my_addr;
my_addr.sin_family = AF_INET; /* host byte order*/
my_addr.sin_port = htons(MYPORT); /* short,
network byte order*/
inet_aton("10.12.110.57",&(my_addr.sin_addr));
/
*my_addr.sin_addr.s_addr=inet_addr(“10.12.110.57”
)*/
memset(&(my_addr.sin_zero), ’\0’, 8); /* zero the rest
of the struct*/
Sample Code 2:-Server side
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT 3490 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
main()
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector’s address information
int sin_size;
sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
memset(&(my_addr.sin_zero), ’\0’, 8); // zero the rest of the struct
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, BACKLOG);
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
}
Sample Code 3:-Client Side
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define DEST_IP "10.12.110.57"
#define DEST_PORT 23
main()
{
int sockfd;
struct sockaddr_in dest_addr; // will hold the destination addr
sockfd = socket(PF_INET, SOCK_STREAM, 0);
dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(DEST_PORT); // short, network byte order
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
memset(&(dest_addr.sin_zero), ’\0’, 8); // zero the rest of the struct
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
}