2. What is a socket?
An interface between application and network
The application creates a socket
The socket type dictates the style of communication
reliable vs. best effort
connection-oriented vs. connectionless
Once configured the application can
pass data to the socket for network transmission
receive data from the socket (transmitted through the
network by some other host)
3. What is Socket ?
Endpoint of any connection
Two Types :
TCP
UDP
Identified by Two values
An IP Address
A Port Number
4. Two essential types of
sockets
SOCK_STREAM SOCK_DGRAM
reliable delivery unreliable delivery
in-order guaranteed no order guarantees
connection-oriented no notion of “connection” – app
bidirectional indicates dest. for each packet
can send or receive
App
App D1
3 2 1
socket Dest.
3 2 1
socket D2
D3
4
5. SERVER SIDE socket()
Socket Functions
Well
known bind()
port
CLIENT SIDE listen()
socket() accept()
Connection establishment
connect() Blocks until
(TCP three-way handshake) connection
from client
write()
Data (request) read()
Process request
Data (reply) write()
read()
End-of-file notification
close() read()
Running an App close()
6. Socket Creation in C: socket
int s = socket(domain, type, protocol);
s: socket descriptor, an integer (like a file-handle)
domain: integer, communication domain
e.g., AF_INET (IPv4 protocol) – typically used
type: communication type
SOCK_STREAM: reliable, 2-way, connection-based service
SOCK_DGRAM: unreliable, connectionless,
7. Socket Creation in C: socket
protocol: specifies protocol (see file /etc/protocols for a
list of options) –
usually set to 0 to select the system’s default for the
given combination of family and type.
NOTE: socket call does not specify where data will be
coming from, nor where it will be going to – it just creates
the interface!
9. Type of socket
Type Description
SOCK_STREAM STREAM socket
SOCK_DGRAM Datagram socket
SOCK_SEQPACKET Sequenced packet socket
SOCK_RAW Raw socket
10. Protocol of socket
protocol description
IPPROTO_TCP TCP transport protocol
IPPROTO_UDP UDP transport
protocol
IPPROTO_SCTP SCTP transport
protocol
11. Addresses, Ports and Sockets
Like apartments and mailboxes
You are the application
Your apartment building address is the address
Your mailbox is the port
The post-office is the network
The socket is the key that gives you access to the right mailbox
(one difference: assume outgoing mail is placed by you in your
mailbox)
13. Socket Address Structure
Length field simplifies the handling of variable-length
socket address structures.
Used with routing socket.
In_addr_t datatype must be an unsigned integer type
of at least 32 bits.
14. Datatype
Datatype Description Header
Int8_t Signed 8-bit integer <sys/types.h>
Uint8_t unsigned 8-bit integer <sys/types.h>
Int16_t Signed 16-bit integer <sys/types.h>
Uint16_t unsigned 16-bit integer <sys/types.h>
Int32_t Signed 32-bit integer <sys/types.h>
Uint32_t unsigned 32-bit integer <sys/types.h>
Sa_family_t Add. Family of socket add <sys/socket.h>
struct
Socklen_t Length of socket add struct <sys/socket.h>
In_addr_t IPV4 address, uint32_t <netinet/in.h>
15. Generic Socket Address structure
A Socket address structure must be passed by
reference
socket function that takes one of these pointers as an
argument must deal with socket address structures
from any of the supported protocol families.
How to declare the type of pointer
Soln : void *
Define Generic socket address structure
<sys/socket.h>
16. Generic Socket Address structure
Struct sockaddr
{
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];/* protocol specific address*/
};
From an application programmer's point of view, the
only use of these generic socket address structures is
to cast pointers to protocol-specific structures.
18. New Generic Socket Address structure
Struct sockaddr
{
uint8_t sa_len;
sa_family_t sa_family;
/* implementation dependent elements to
provide
1. alignment
2. enough storage to hold any type of socket
address that the system supports */
};
19. sockaddr_storage different from struct sockaddr
in two ways:
If any socket address structures that the system
supports have alignment requirements, the
sockaddr_storage provides the strictest alignment
requirement.
The sockaddr_storage is large enough to contain any
socket address structure that the system supports.
21. Value-result Arguments
Accept , recvfrom , getpeername pass socket address
structure from kernel to the process.
Size changes from an int to pointer to an integer is
because the size is both a value when the function is
called and Result when function returns.
connect (sockfd, (SA *) &serv, sizeof(serv));
getpeername(unixfd, (SA *) &cli, &len);
22.
23. Address and port byte-ordering
Address and port are stored as integers
u_short sin_port; (16 bit)
in_addr sin_addr; (32 bit)
Problem:
different machines / OS’s use different word orderings
• little-endian: lower bytes first
• big-endian: higher bytes first
these machines may communicate with one another over the
network
Big-Endian
Little-Endian
machine
machine
G !!
!12.40.119.128
ON40 12
128.119.40.12
128 R
128 119 40 12 W 119
23
24. Solution: Network Byte-Ordering
Defs:
Host Byte-Ordering: the byte ordering used by a
host (big or little)
Network Byte-Ordering: the byte ordering used by
the network – always big-endian
Any words sent through the network should be
converted to Network Byte-Order prior to
transmission (and back to Host Byte-Order once
received)
25. Byte Ordering Functions
program
Two types of Byte Ordering
Little-endian Byte Ordering
Address A+1 Address A
MSB LSB
Big-endian Byte Ordering
Address A Address A+1
MSB LSB
26. determine host byte order
int main(int argc, char **argv)
{
union
{ short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
printf("%s: ", CPU_VENDOR_OS);
if (sizeof(short) == 2)
{ if (un.c[0] == 1 && un.c[1] == 2)
printf("big-endiann");
else if (un.c[0] == 2 && un.c[1] == 1)
printf("little-endiann");
else
printf("unknownn");
}
else
printf("sizeof(short) = %dn", sizeof(short));
exit(0);
27. Byte Ordering Functions
<netinet/in.h>
uint16_t htons(uint16_t host16val); -- convert 16-bit
value from host to network order, used for the port
number.
uint32_t htonl(uint32_t host32val); -- convert 32-bit
value from host to network order, used for the Ipv4
address.
uint16_t ntohs(uint16_t network16val); -- convert 16-bit
value from network to host order.
uint32_t ntohl(uint32_t network32val); -- convert 32-bit
value from network to host order.
28. Byte Ordering Functions
#include<strings.h>
Void bzero(void *dest, size_t nbytes);
-- SETS THE SPECIFIED NO. OF BYTES TO O IN THE
DESTINATION.
Void bcopy(const void *src, void *dest, size_t nbytes);
-- copies nbytes of src to dest.
int bcmp(const void *ptrl, const void *ptr2, size_t
nbytes);
-- compare nbytes of the two strings, Returns 0 if
they match, >0 if ptr1 >ptr2, < 0 if ptr1 < ptr2.
29. void *memset(void *dest, int c, size_t nbytes);
---- writes value c in the destination
----bytes of dest. Returns dest.
void *memcpy(void *dest, const void *src, size_t
nbytes);
---- copies nbytes of src to dest. Returns dest.
int memcmp(const void *ptrl, const void *ptr2, size_t
nbutes);
---- compare nbytes of the two strings, Returns 0 if
they match, >0 if ptr1 >ptr2, < 0 if ptr1 < ptr2.
30. Inet_aton, inet_addr, inet_ntoa
An internet address is written as: “192. 43. 234.1”,
saved as a character string. The functions require
their number as a 32-bit binary value.
<arpa/inet.h>
int inet_aton(const char *strptr, struct in_addr *
addrptr);
-- returns 1, 0 on error.
Converts from a dotted-decimal string to a network
address.
31. Inet_aton, inet_addr, inet_ntoa
In_addr_t inet_addr(const char *strptr);
-- Converts from a dotted-decimal string to 32-bit
interger as return value.
--INADDR_NONE IF ERRORs
char *inet_ntoa (struct in_addr addrptr);
-- Returns a pointer to a dotted-decimal string, given
a valid network address.
32. Inet_pton functions
#include<arpa/inet.h>
Convert strptr ( which is in presentation form ) to
numeric form and store it into addrptr.
int inet_pton(int family, const char *strptr, void
*addrptr);
-- returns 1 if OK, 0 if invalid input format, -1 on error.
33. inet_ntop functions
Const char *inet_ntop (int family, const void *addrptr,
char *strptr, size_t len);
-- returns pointer to result if OK, NULL on error.
--len argument is the size of the destination, to
prevent the function from overflowing the caller’s
buffer
Two functions supports both IPv4 and IPv6 protocol
34.
35. Sock_ntop function
A basic problem with inet_ntop is that it requires the
caller to pass a pointer to a binary address.
This address is normally contained in a socket address
structure,
Requiring the caller to know the format of the
structure and the address family.
36. Sock_ntop function
struct sockaddr_in addr;
inet_ntop(AF_INET, &addr.sin_addr, str, sizeof(str));
For IPV6 struct sockaddr_in6 addr6;
inet_ntop(AF_INET6, &addr6.sin6_addr, str,
sizeof(str));
sock_ntop that takes a pointer to a socket address
structure, looks inside the structure, and calls the
appropriate function to return the presentation format of
the address.
37. Sock_ntop function
char *sock_ntop(const struct sockaddr *sockaddr,
socklen_t addrlen);
Returns: non-null pointer if OK, NULL on error
sockaddr points to a socket address structure whose
length is addrlen.
The function uses its own static buffer to hold the
result and a pointer to this buffer is the return value.
38. sock_ntop and related functions.
int sock_bind_wild(int sockfd, int family);
Returns: 0 if OK, -1 on error
sock_bind_wild binds the wildcard address and an
ephemeral port to a socket.
39. sock_ntop and related functions.
sock_cmp_addr compares the address portion of
two socket address structures
int sock_cmp_addr(const struct sockaddr
*sockaddr1,const struct sockaddr *sockaddr2,
socklen_t addrlen);
Returns: 0 if addresses are of the same family
and ports are equal, else nonzero
40. sock_ntop and related functions
int sock_cmp_port(const struct sockaddr
*sockaddr1,const struct sockaddr *sockaddr2,
socklen_t addrlen);
Returns: 0 if addresses are of the same family and ports
are equal, else nonzero
sock_cmp_port compares the port number of two
socket address structures.
41. sock_ntop and related functions
sock_get_port returns just the port number
int sock_get_port(const struct sockaddr *sockaddr,
socklen_t addrlen);
Returns: non-negative port number for IPv4 or IPv6
address, else -1
42. sock_ntop and related functions
char *sock_ntop_host(const struct sockaddr
*sockaddr, socklen_t addrlen);
Returns: non-null pointer if OK, NULL on error
sock_ntop_host converts just the host portion of a
socket address structure to presentation format (not
the port number).
43. sock_ntop and related functions
void sock_set_addr(const struct sockaddr *sockaddr,
socklen_t addrlen, void *ptr);
sock_set_addr sets just the address portion of a
socket address structure to the value pointed to by ptr
44. sock_ntop and related functions
void sock_set_port(const struct sockaddr *sockaddr,
socklen_t addrlen, int port)
sock_set_port sets just the port number of a socket
address structure
void sock_set_wild(struct sockaddr *sockaddr,
socklen_t addrlen);
sock_set_wild sets the address portion of a socket
address structure to the wildcard
45. readn, writen, and readline Functions
A read or write on a stream socket might input or
output fewer bytes than requested.
Buffer limits might be reached for the socket in the
kernel.
#include "unp.h“
ssize_t readn(int filedes, void *buff, size_t nbytes);
ssize_t writen(int filedes, const void *buff, size_t
nbytes);
ssize_t readline(int filedes, void *buff, size_t maxlen);
All return: number of bytes read or written, –1 on
error
46. readn function: Read n bytes from a descriptor.
#include "unp.h"
ssize_t readn(int fd, void *vptr, size_t n)
{ size_t nleft; ssize_t nread; char *ptr; ptr = vptr; nleft =
n;
while (nleft > 0)
{ if ( (nread = read(fd, ptr, nleft)) < 0)
{ if (errno == EINTR)
nread = 0; /* and call read() again */
else
return (-1);
}
else if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
47. writen function: Write n bytes to a descriptor
ssize_t writen(int fd, const void *vptr, size_t n)
{ size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{ if ( (nwritten = write(fd, ptr, nleft)) <= 0)
{ if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call write() again */
else
return (-1); /* error */
}
nleft -= nwritten;
ptr += nwritten;
}
return (n); }
48. readline function: Read a text line from a
descriptor, one byte at a time.
ssize_t readline(int fd, void *vptr, size_t maxlen)
{ ssize_t n, rc; char c, *ptr; ptr = vptr;
for (n = 1; n < maxlen; n++)
{ again : if ( (rc = read(fd, &c, 1)) == 1)
{ *ptr++ = c;
if (c == 'n')
break; /* newline is stored, like fgets() */
} else if (rc == 0)
{ *ptr = 0;
return (n - 1); /* EOF, n - 1 bytes were read */
} else
{ if (errno == EINTR)
goto again;
return (-1); /* error, errno set by read() */
}
}
*ptr = 0; /* null terminate like fgets() */
return (n); }
49. Example – Daytime Server/Client
Application protocol
Daytime client Daytime server
Socket API Socket API
TCP protocol
TCP TCP
IP protocol
IP IP
MAC-level protocol
MAC driver MAC driver
Actual data flow
MAC = media
Network access control
50. Files to be created
Client STRING Server
Client Server
Client source file - client.c
Server source file - server.c
Header file - header.h
51. Running an Application
Compile and run server file ( you can run it as a
background process too)
e.g. $cc server.c
$./a.out 2345 &
Compile and Run client file
e.g. $cc client.c
$./a.out 172.16.2.2 2345
Thu March 05 15:50:00 2009
Back
52. abstract
Socket function
connect function
bind function
listen function
accept function
fork and exec function
concurrent server
close function
getsockname and getpeername function
53. SERVER SIDE socket()
Socket Functions
Well
known bind()
port
CLIENT SIDE listen()
socket() accept()
Connection establishment
connect() Blocks until
(TCP three-way handshake) connection
from client
write()
Data (request) read()
Process request
Data (reply) write()
read()
End-of-file notification
close() read()
Running an App close()
54. #include <sys/socket.h>
int socket(int family, int type, int protocol);
returns:nonnegative descriptor if OK, -1 on error
==>Normaly the protocol argument to the socket function is set to 0
exept for raw socket.
55.
56. connect Function
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr
*servaddr, socklen_t addrlen);
Returns : 0 if successful connect, -1 otherwise
sockfd: integer, socket to be used in connection
struct sockaddr: address of passive participant
integer, sizeof(struct)
(If connect fails, the SYN_SENT socket is no longer
useable.)
57. Connection Setup (SOCK_STREAM)
A connection occurs between two kinds of
participants
passive: waits for an active participant to request
connection
active: initiates connection request to passive side
Once connection is established, passive and
active participants are “similar”
both can send & receive data
either can terminate the connection
58. Connect function
Return error
ETIMEOUT : no response from server
RST : server process is not running
EHOSTUNREACH : client’s SYN unreachable
from some
intermediate router.
59. Error Return by Connect
If the client TCP receives no response to its SYN
segment, ETIMEDOUT is returned.
If the server’s response to the client’s SYN is a
reset (RST), this indicates that no process is
waiting for connections on the server host at the
port specified.
Hard error
ECONNREFUSED is returned to the client as soon as the RST is
received.
60. Error Return by Connect
Three conditions that generate an RST are:
When a SYN arrives for a port that has no listening server
When TCP wants to abort an existing connection
When TCP receives a segment for a connection that does
not exist.
61. Error Return by Connect
ICMP destination unreachable received in response to
client TCP’s SYN (maybe due to transient routing
problem), resend SYN timeout after 75 sec, returns
EHOSTUNREACH or ENETUNREACH
62. connect Function: Three-Way Handshake
• No bind before connect :The kernel chooses the source IP, if
necessary, and an ephemeral port (for the client).
Hard error: RST received in response to client TCP’s SYN (server not
running) returns ECONNREFUSED
Soft error:
1. no response to client TCP’s SYN, resend SYN, timeout after 75 sec (in
4.4BSD), returns ETIMEOUT
2. ICMP destination unreachable received in response to client TCP’s SYN
(maybe due to transient routing problem), retx SYN, timeout after 75 sec,
returns EHOSTUNREACH)
63. The bind function
Assigns a local protocol address to a socket.
int status = bind(int sockid, const struct sockaddr
&myaddr, socklen_t addrlen);
status: error status, = -1 if bind failed, 0 if OK.
sockid: integer, socket descriptor
myaddr: struct sockaddr, the (IP) address and port of
the machine (address usually set to INADDR_ANY –
chooses a local address)
addrlen: the size (in bytes) of the addrport structure
64. bind Function
Usually servers bind themselves to their well-known
ports.
RPC servers let kernel choose ephemeral ports which
are then registered with the RPC port mapper.
Normally, TCP client does not bind an IP address to
its socket.
If a TCP server does not bind an IP address to its
socket, the kernel uses the destination IP address of
the client’s SYN as the server’s source IP address
65. bind Function
Table summarizes the values to which we
set sin_addr and sin_port depending on the
desired result.
Process specifies
IP address port Result
Wildcard 0 kernel chooses IP addr and port
wildcard nonzero kernel chooses IP addr, process specifies port
local IP addr 0 kernel chooses port, process specifies IP addr
local IP addr nonzero process specifies IP addr and port
66. bind Function
Wildcard address is specified by the constant
INADDR_ANY whose value is normally 0.
To obtain value of the ephemeral port assigned by the
kernel, we must call getsockname to return the
protocol address.
67. listen function
#include <sys/socket.h>
int listen(int sockfd, int backlog);
Returns:0 if OK, -1 on error
==>This function is called only by a TCP server
The listen function converts an unconnected socket into a passive
socket, indicating that the kernel should accept incoming connection
requests directed to this socket.
backlog =>specify the maximum number of connections that the
kernel should queue for this socket.
If the queues are full when client SYN arrives, TCP server ignore the
SYN, it does not send RST.
68. listen function
●
An incomplete connection queue, which contains an entry
for each SYN that has arrived from a client for which the
server is awaiting completion of the TCP three-way
handshake
●
A completed connection queue, which contains an entry for
each client with whom the TCP three-way handshake has
completed
69.
70. Backlog argument to the listen function has historically specified the maximum
value for the sum of both queues
71. listen Function
Backlog argument to listen function has specified the
maximum value for the sum of both queues
Berkeley derived : multipied by 1.5
Do not specify backlog of 0
What value should the application specify?
Allow Command line or an environment variable to
override default.
72. Listen that allows an environment var to specify backlog
Void Listen(int fd, int backlog)
{ char *ptr;
if ((ptr=getenv(“LISTENQ”))!=NULL)
backlog=atoi(ptr);
if (listen(fd,backlog)<0)
printf(“listen error”);
}
73. listen Function
If the queues are full when a client SYN arrives, TCP
ignores the arriving SYN : it does not send an RST.
Data that arrives after the three way handshake
completes, but before the server call accept, should be
queued by the server TCP, up to the size of the
connected socket’s receive buffer.
74. accept function
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *cliaddr,
socklen_t *addrlen);
Returns:nonnegative descriptor if OK, -1 on error
=> return the next completed connection from the
front of the completed connection queue.
If queue is empty, the process is put to sleep.
75. Return three values:
1. integer return code
2. protocol address of the client process
3. size of this address :
This integer value contains the actual number of bytes
stored by the kernel in the socket address structure.
If we are not interested in having the protocol address of the
client returned, we set both cliaddr and addrlen to null
pointers.
76. fork and exec function
#include <unistd.h>
pid_t fork(void);
Returns: 0 in child, process ID of child in parent, -1 on error
#include <unistd.h>
int execl(const char *pathname, const char *arg(), …/*(char *) 0*/);
int execv(const char *pathname, char *const argv[]);
int execle(const char *pathname, const char *arg());
int execve(const char *pathname, char *const argv[], char *const
envp[]);
int execlp(const char *filename, const char *arg());
int execvp(const char *filename, char *const argv[]);
All six return: -1 on error, no return on success
77. A process makes a copy of itself so that one copy can
handle one operation while the other copy does
another task.
A process wants to execute another program. Since
the only way to create a new process is by calling fork,
the process first calls fork to make a copy of itself, and
then one of the copies (typically the child process)
calls exec to replace itself with the new program.
78. The differences in the six exec functions are:
(a) whether the program file to execute is specified by a
filename or a pathname;
(b) whether the arguments to the new program are
listed one by one or referenced through an array of
pointers; and
(c) Whether the environment of the calling process is
passed to the new program or whether a new
environment is specified.
79.
80. Concurrent server
pid_t pid
int listenfd, connfd;
listenfd = Socket(...);
//fill in sockaddr_in{} with server’s well-known port
Bind(listenfd, LISTENQ);
for(;;){
connfd = Accept(listenfd, ...);
if( (pid = Fork()) == 0)
{
Close(listenfd); /* child closes listening socket */
doit(connfd); //process the request
Close(); //done with this client
exit(0); //child terminate
}
Close(connfd); // parent close connected socket
81.
82. close
When finished using a socket, the socket should be
closed:
status = close(s);
status: 0 if successful, -1 if error
s: the file descriptor (socket being closed)
Closing a socket
closes a connection (for SOCK_STREAM)
frees up the port used by the socket
83.
84. Return the address family of socket
#include "unp.h"
int sockfd_to_family(int sockfd)
{
struct sockaddr_storage ss;
socklen_t len;
len = sizeof(ss);
if (getsockname(sockfd, (SA *) &ss, &len) < 0)
return (-1);
return (ss.ss_family);
}
85. getsockname and getpeername
function
#include<sys/socket.h>
int getsockname(int sockfd, struct sockaddr *localaddr,
socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *peeraddr,
socklen_t *addrlen);
both return : 0 if OK, -1 on error
=>getsockname : return local address associated with a socket
getpeername : foreign protocol address associated with a socket
86. getsockname and getpeername
This function takes the following three input arguments:
1. The sockets to query for the socket address.
2. The pointer to the receiving buffer (argument name).
3. Pointer to the maximum length variable. This variable
provides the maximum length in bytes that can be
received in the buffer (argument namelen).
87. Reasons:
TCP client that does not call bind, getsockname
returns the local protocol address
After calling bind with a port no. of 0,
getsockname returns the local port number
It can be called to obtain the address family of a
socket.
Server binds to wildcard IP address,getsockname
used to obtain the local protocol address
Only way to obtain the identity of client is to call
getpeername
88. Value-result Arguments
when a socket address structure is passed to any
socket function, it is always passed by reference.
That is, a pointer to the structure is passed.
The length of the structure is also passed as an
argument. But the way in which the length is passed
depends on which direction the structure is being
passed: from the process to the kernel, or vice versa.
Bind, connect, and sendto pass socket address
structure from the process to kernel
89.
90. Files to be created
Client STRING Server
Client Server
Client source file - client.c
Server source file - server.c
Header file - header.h
91. TCP Echo Client/Server
The client reads a line of text from its standard
input and writes the line to the server.
The server reads the line from its network input and
echoes the line back to the client.
The client reads the echoed line and prints it on its
standard output.
92. TCP Echo Server: main Function
Create socket, bind server's well-known port
Wait for client connection to complete
Concurrent server
H:echo_server.c
Header file : unp.h
93. TCP echo client.
Create socket, fill in Internet socket address
structure
Connect to server
G:echo_client.c.txt
94. TCP Echo Client: str_cli
Function
Read a line, write to server
Read echoed line from server, write to standard
output
Return to main