Introduction to Sockets

Download Report

Transcript Introduction to Sockets

shri
Introduction to Sockets
K.C. Rao
([email protected])
CDAC-Mumbai
1
Socket Interface

What is the Socket interface?



It is a protocol independent interface to
multiple transport layer primitives
It is an interface (a “door”) into which an
application process can both send and receive
messages to/from another (remote or local)
application process
History

The first implementation was released along with
BSD 4.2. Since then, it has undergone several
improvements
CDAC-Mumbai
2
Typical Client-Server interaction using TCP
Server
Client
socket()
well-known port
bind()
socket()
listen()
connect()
accept()
Connection establishment
TCP three-way handshake
write()
Data (request)
Block until
connection from client
read()
Process requests
read()
write()
Data (reply)
close()
End-of-file notification
CDAC-Mumbai
read()
close()
3
Creating a Socket
#include <sys/socket.h>
int socket(int family, int type, int protocol);
Returns: non-negative socket descriptor if OK, –1 on error



family specifies address or protocol family. Can
be AF_INET, AF_INET6, AF_LOCAL, etc
type specifies socket type. Can be SOCK_DGRAM,
SOCK_STREAM, SOCK_RAW
protocol argument is normally left to zero except
for raw sockets- need not bother about it in this
elementary discussion
CDAC-Mumbai
4
Closing a socket
#inlcude <unistd.h>
int close(int sockfd);
Returns: 0 if OK, –1 on error
The transport layer generally tries to flush out,
i.e. send, any remaining data, when a close is
done.
CDAC-Mumbai
5
Establishing a connection
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr* servaddr, socklen_t servaddrlen);
Returns: 0 if OK, -1 on error



This function is used by a client to establish a connection
with a server
The socket address structure, sockaddr, contains the
address information of the server to connect to
In general, the port number to be used on the client’s side is
chosen by the kernel (ephemeral port)
CDAC-Mumbai
6
Background Information:
Socket Address Structure




This contains the protocol specific addressing
information
The general structure is named sockaddr
Each of the protocols supported by a socket
implementation have their own socket
address structure sockaddr_suffix
-Where ‘suffix’ represents the protocol family
Eg: sockaddr_in – Internet/IPv4 socket
address structure
sockaddr_ipx – IPX socket address
structure
CDAC-Mumbai
7
The Socket Address Structure(POSIX)
struct sockaddr {
sa_family_t
sa_family;
char
sa_data[14];
};
/*Address Family, AF_XXX*/
/*14 bytes of protocol address*/
struct sockaddr_in {
sa_family_t
sin_family;
in_port_t
sin_port;
___
struct in_addr sin_addr;
----char
sin_zero[8];
};
/* must be AF_INET */
/*16-bit TCP or UDP port number*/
/*network byte ordered*/
/*32-bit IPv4 address*/
/*network byte ordered*/
/* Not used, must be zero */
struct in_addr {
in_addr_t
s_addr;
/* 32-bit IPv4 address*/
/*network byte ordered */
};
CDAC-Mumbai
8
Associating a local address
with a socket
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr* myaddr, socklen_t addrlen);
Returns: 0 if OK, -1 if error


It is used for explicitly associating a protocol
specific local address to a socket
For Internet Protocols, bind() associates with
the socket a port number and/or one of the
IP addresses of the host on which the
program is running (note that the host may
be multi-homed)
CDAC-Mumbai
9
Associating a local address
with a socket


It is necessary for a server wanting to listen
for connections on some port, as also for
connectionless applications (eg UDP based)
On unbounded sockets an implicit bind is
done with IN_ADDRANY and a random port
as the address and port parameters
respectively
CDAC-Mumbai
10
Listening for a connection
#include <sys/socket.h>
int listen(int sockfd, int backlog);
Returns: 0 if OK, -1 if error
listen() performs two duties:
1)
It specifies to the kernel that it should passively
listen for connections to the address associated to
sockfd
2)
It specifies the maximum number of pending
connections the kernel should queue for this
socket
CDAC-Mumbai
11
Accepting a connection
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr* cliaddr, socklen_t* addrlen);
Returns: non-negative descriptor if OK, -1 on error




This function is called by the server to get the next
connection from the queue of completed connections
If no connection has been completed, the function
call blocks
A new socket descriptor is returned for the connection
The structure pointed to by cliaddr is filled in with
information about the protocol address of the
connected peer process(client), while *addrlen gives
the size of this structure
CDAC-Mumbai
12
Functions for
sending/receiving data
#include <sys/types.h>
#include <sys/socket.h>
int write(int sockfd, char* buf, int nbytes);
/*Return value: No. of bytes sent if OK, -1 on error*/
int read(int sockfd, char* buf, int nbytes);
/*Return value: No. of bytes read if OK, 0 on connection closure,
-1 on error*/
CDAC-Mumbai
13
Some additional functions

In network programming, we often
need the help of some additional
functions for tasks like:



Byte ordering
Byte operations
Address Conversions
CDAC-Mumbai
14
Byte Ordering
Increasing memory address
Address A+1
Little-endian
byte order
High-order byte
MSB
Big-endian
byte order
Address A
Low-order byte
16-bit value
High-order byte
Address A
LSB
Low-order byte
Address A+1
Increasing memory address
CDAC-Mumbai
15
Implications of Byte Order




Unfortunately there is no standard byte
order- some systems are big endian, the
others little endian
We refer to the byte ordering used by a given
system as host byte order
The sender and the receiver must agree on
the order in which the bytes of multi-byte
field transmitted.
Hence we specify the network byte order,
which is big-endian byte ordering
CDAC-Mumbai
16
Byte Order Functions
#include <netinet/in.h>
#include <sys/types.h>
uint16_t htons(uint16_t host16bitvalue)
uint32_t htonl(uint32_t host32bitvalue)
Return: value in network byte order
uint16_t ntohs(uint16_t net16bitvalue)
uint32_t ntohl(uint32_t net32bitvalue)
Return: value in host byte order
CDAC-Mumbai
17
Byte Manipulation Functions
#include <string.h>
void *memset(void *s, int c, size_t n);
/*Return: Pointer to the memory area s */
void *memcpy(void *dest, const void *src, size_t n);
/*Return: Pointer to dest */
int memcmp(const void *s1, const void *s2, size_t n);
/* Return: <0, =0, >0, if first n bytes of s1 are respectively less than,
equal to, or greater than the first n bytes of s2 */
CDAC-Mumbai
18
Address Conversion Functions
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char* strptr, struct in_addr* addrptr);
/* return non-zero if string was valid,0 if error */
char* inet_ntoa(struct in_addr inaddr);
/* returns: pointer to dotted-decimal string */
CDAC-Mumbai
19
Address Conversion Functions
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int inet_pton(int family, const char *strpr, void *addrptr);
/* returns 1 if OK, 0 if input not a valid presentation format, -1 on other
errors*/
const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t
len);
/*returns pointer to result if OK, NULL on error*/
CDAC-Mumbai
20
References




Unix Network Programming :Vol 1Stevens
Network Programming for Microsoft
Windows –Jones, Ohlund
Java Network Programming – Rusty,
Harold
TCP/IP Illustrated - Stevens
CDAC-Mumbai
21