1socket Programming

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

SOCKET PROGRAMMING

Sockets ------ inter process communication.

A socket is one endpoint of a two-way communication link between two


programs running on the network.

Most inter process communication uses the client /server model.

Two processes which will be communicating with each other.

One of the two processes, the client, connects to the other process, the
server, typically to make a request for information.

Ex: person who makes a phone call to another person.


CLIENT / SERVER

Client needs to know of the existence of and the


address of the server

But the server does not need to know the address of


(or even the existence of) the client prior to the
connection being established.

once a connection is established, both sides can send


and receive information.
Connection oriented protocol
EX: Telephone call
CONNECTION-ORIENTED PROTOCOL

Connection-oriented protocol service is


sometimes called a "reliable" network
service, because it guarantees that data will
arrive in the proper sequence.

Transmission Control Protocol (TCP) is a


connection-oriented protocol.
CONNECTION-ORIENTED PROTOCOL
logical connection should be established between two
devices before transferring data.

Usually one device begins by sending a request to open a


connection, and the other responds.

They pass control information to determine if and how the


connection should be set up.

If this is successful, data is sent between the devices.

When they are finished, the connection is broken.


CONNECTIONLESS PROTOCOL

Ex: Posting letters

Letters won’t send in the same order.

They can take any route.


• Trichy – perambalur – chennai
• Trichy – Thanjavur – Chennai
• There is no guarantee that letters will be delivered
- it can be lost.
CONNECTIONLESS PROTOCOL

Protocols do not establish a connection between devices.

As soon as a device has data to send to another, it just sends it.

The device at one end of the communication transmits data to the


other, without first ensuring that the recipient is available and ready to
receive the data.

The device sending a message simply sends it addressed to the


intended recipient.

Internet Protocol (IP) and User Datagram Protocol (UDP) are


connectionless protocols
Protocols provide sequencing if they
ensure the data arrives in the same order
it was sent.

Protocols provide error control if they


automatically discard messages that have
been corrupted and arrange to retransmit
the data.
Types
 Streaming protocols recognize only byte boundaries.
Sequences of bytes may be split up and are delivered to
the recipient as the data arrives. (connection oriented)
 Packet-based protocols handle packets of data,
preserving the packet boundaries and delivering
complete packets to the receiver. Packet-based protocols
normally enforce a maximum packet size .
(connectionless)
Datagram protocols are packet-oriented
transports that provide neither sequencing nor
error control; UDP, part of the TCP/IP protocol
family, is a widely used datagram protocol.

Stream protocols, such as the TCP portion of


TCP/IP, are streaming protocols that provide both
sequencing and error control.
SOCKET

A socket is one end of an inter process


communication channel.

The two processes each establish their


own socket.

socket is a combination of IP address


and port on one system.
CLIENT SIDE
The steps involved in establishing a socket on the
client side are as follows:

Create a socket with the socket() system call.

Connect the socket to the address of the server


using the connect() system call.

Send and receive data. (use the read() and write()


system calls)
SERVER SIDE
steps involved in establishing a socket on the server side ,
 Create a socket with the socket() system call

 Bind the socket to an address using the bind() system


call. For a server socket on the Internet, an address
consists of a port number on the host machine.
 Listen for connections with the listen() system call

 Accept a connection with the accept() system call. This


call typically blocks until a client connects with the
server.
 Send and receive data
ADDRESSES
#include <sys/socket.h>
struct sockaddr
{
unsigned short sa_family;
char sa_data[MAXSOCKADDRDATA];
}
Protocol and Address families
Address Protocol Protocol Description
AF_UNIX PF_UNIX Unix domain
AF_INET PF_INET TCP/IP (version 4)
AF_INET6 PF_INET6 TCP/IP (version 6)
AF_AX25 PF_AX25 AX.25, used by amateur
radio
AF_IPX PF_IPX Novell IPX
AF_APPLETALK PF_APPLETALK AppleTalk DDS
AF_NETROM PF_NETROM NetROM, used by
amateur radio
BASIC SOCKET OPERATIONS

created through the socket() system call, which


returns a file descriptor.

Once the socket has been properly initialized ,


that file descriptor may be used for read() and
write() requests , like any other file descriptor.

When a process is finished with a socket, it


should be close() ed to free the resources
associated with it.
CREATING A SOCKET
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
 Domain – Protocol family
 Type - SOCK_STREAM (TCP), SOCK_DGRAM(UDP)
 Protocol – which protocol to use - this parameter is 0, then
kernel use the default protocol of the specified type and
family.
 PF_INET protocol family,
 IPPROTO_TCP being the default stream protocol and
IPPROTO_UDP the default datagram protocol.
ESTABLISHING CONNECTIONS
 server applications that are started and continuously
running, waiting for other processes to connect to them.
 Client processes instead create a socket, tell the system
which address they want to connect it to, and then try to
establish the connection.
 Once the server (which has been waiting for a client)
accepts the connection attempt, the connection is
established between the two sockets.
 After this happens, the socket may be used for
bidirectional communication.
BINDING AN ADDRESS TO A SOCKET
 Both server and client processes need to tell the system
which address to use for the socket.
 Attaching an address to the local side of a socket is
called binding the socket and is done through the bind()
system call.
#include <sys/socket.h>
int bind(int sock, struct sockaddr * my_addr, socklen_t addrlen);
WAITING FOR CONNECTIONS
Int listen(int sock, int backlog);
Int accept(int sock, struct *sockaddr, int *addrlen);
Backlog – how many connections may be pending on the
socket
Until accept the incoming connection is pending
 Client Server
1. Socket() Socket()
2. bind()
3. listen()
4. Connect()
5. Accept()
UNIX DOMAIN SOCKETS
 connect only to sockets on the same machine.
 Addresses are pathnames that are created in the file
system when a socket is bound to the pathname.
 Unix domain sockets are connection-oriented; each
connection to the socket results in a new communication
channel.
 The server, which may be handling many simultaneous
connections, has a different file descriptor for each.
UNIX DOMAIN ADDRESSES

 Addresses for Unix domain sockets are pathnames in the


file system.
 If the file does not already exist, it is created as a socket-
type file when a socket is bound to the pathname
through bind() .
 To connect() to an existing socket, the process must have
read and write permissions for the socket file. 
#include <sys/socket.h>
#include <sys/un.h>
struct sockaddr_un
{
unsigned short sun_family; /* AF_UNIX */
char sun_path[UNIX_PATH_MAX]; //pathname }
UNNAMED UNIX DOMAIN SOCKETS

#include <sys/socket.h>
int socketpair(int domain, int type, int protocol, int
sockfds[2]);

 The first three parameters are the same as those passed


to socket() .
 The final parameter, sockfds() , is filled in
by socketpair() with two file descriptors, one for each
end of the socket.
PASSING FILE DESCRIPTORS

 Unix domain sockets have a unique ability: File


descriptors can be passed through them.
 No other IPC mechanism supports this facility.

 It allows a process to open a file and pass the file


descriptor to another—possibly unrelated—process.
int sendmsg(int fd, const struct msghdr * msg,
unsigned int flags);
int recvmsg(int fd, struct msghdr * msg,
unsigned int flags);
MESSAGE STRUCTURE

#include <sys/socket.h>
#include <sys/un.h>
struct msghdr {
void * msg_name; /* optional address */
unsigned int msg_namelen; /* size of msg_name */
struct iovec * msg_iov; /* scatter/gather array */
unsigned int msg_iovlen; /* number of elements in msg_iov */
void * msg_control; /* ancillary data */
unsigned int msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */
};
 msg_name and msg_namelen  are not used with stream
protocols.
 For stream sockets ,
set msg_name to NULL and msg_namelen to zero.
 msg_iov and msg_iovlen describe a set of buffers that
are sent or received. 
 msg_flags – should set to zero.
Struct cmsghdr
{
Unsigned int cmsg_len; /length of control msg
Int cmsg_level; //SOL_SOCKET
Int cmsg_type; //SCM_RIGHTS
Int cmsg_data[0];//file descriptor gots here
}
NETWORKING MACHINES WITH TCP/IP

Byte ordering – storing bytes in memory


Big-endian architectures store the most significant byte
at the lowest hardware address, and the other bytes
follow in order from most significant to least significant. 
Little-endian The least significant byte is stored at the
smallest memory address.
EXAMPLE
 decimal number 258 (0100000010 in binary) would be
stored in as a 16 bit binary number.

 00000001—00000010 Big Endian

 00000010—00000001 Little Endian

htons Host to Network Short


htonl Host to Network Long
 TCP/IP mandates that big-endian byte order be used for
transmitting protocol information and data.
 The ordering used for multi byte values sent across the
network is known as the network byte order .
BETWEEN HOST BYTE ORDER AND
NETWORK BYTE ORDER:

#include <netinet/in.h>
unsigned int htonl(unsigned int hostlong); unsigned
short htons(unsigned short hostshort);
Above 2 functions, convert longs and shorts,
respectively, from host order to network order.
unsigned int ntohl(unsigned int netlong); unsigned short
ntohs(unsigned short netshort);
Above 2 functions ,convert longs and shorts from network
order to the host byte ordering.
IPV4 ADDRESSING

 IPv4 connections are a 4-tuple of ( local host , local


port , remote host ,remote port ).
 Local host and remote host are each IPv4 addresses.

 IPv4 addresses are 32-bit (4-byte) numbers unique


across the entire connected network.
 written as aaa . bbb . ccc . ddd, with each element in the
address being the decimal representation of one of the
bytes in the machine's address.
 The left-most number in the address corresponds to the
most significant byte in the address. This format for IPv4
addresses is known as dotted -decimal notation .
 IP number does not provide a unique identification for a
connection on a single machine. 
 Port numbers are 16-bit numbers that uniquely identify
one endpoint of a connection on a single host.
 The combination of an IPv4 address and a port number
identifies a connection endpoint anywhere on a single
TCP/IP network.
 Two connection endpoints form a TCP connection, so
two IP number/port number pairs uniquely identify a
TCP/IP connection on a network.
 Port numbers range from 0 to 65,535
 The reserved ports , numbering from 0 to 1,024, may be
used only by processes running as root. 
IP SOCKET ADDRESSES
#include <sys/socket.h>
#include <netinet/in.h>
struct sockaddr_in
{
short int sin_family; /* AF_INET */
unsigned short int sin_port; /* port number
struct in_addr sin_addr; /* IP address */
}
MANIPULATING IP ADDRESSES
 Applications often need to convert IP addresses between
a human readable notation (dotted-decimal) and struct
in_addr 's binary representation.
#include <arpa/inet.h>
const char * inet_ntoa(struct in_addr address);
 Takes a binary IP address and returns a pointer to a
string containing the dotted-decimal 
 Ntoa – network- to-ascii
 Inet_addr() – converts dotted decimal string to a binary
IP address.
int inet_aton(const char **ddaddress, struct in_addr
*address);
USING HOST NAMES
 DNS – Domain name system
 Converts between IP addresses and host names.

 Many to Many mapping


 Internet
sites use a single machine for both ftp and web site.
www.some.org & ftp.some.org – No need for 2 IP addresses
#include <netdb.h>
Struct hostent
{
Char *h_name;
Char **h_aliases;
Int h_addrtype;
Int h_length;
Char **h_addr_list;
};
Struct hostent *gethostbyname(const char *name);
Struct hostent *gethostbyaddr(const char *addr, int len, int
type);
LOOKING UP PORT NUMBERS
 Internet Assigned Numbers Authority(IANA) maintains port
numbers.
 Internet protocols ftp, http, telnet ---- has unique port number.

 /etc/services maps protocols to port numbers.

Struct servent *getservbyname(const char *name, const char


*protocol);

Struct servent{
Char *s_name;
Char **s_aliases;
Int s_port;
Char *s_proto;
}
 htons() function converts the unsigned short
integer hostshort from host byte order to network byte
order.
 void *memset(void *s, int c, size_t n);

 The memset() function fills the first n bytes of the


memory area pointed to by s with the constant byte c.
SOCKET ERRORS
 EAFNOSUPPORT – unsupported address family was
specified

You might also like