Socket Programming Basics
Socket Programming Basics
Socket Programming Basics
Generic
IP Specifc
struct sockaddr
{
unsigned short sa_family;
char sa_data[14];
};
struct sockaddr_in
{
unsigned short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr
{
unsigned long s_addr;
};
sockaddr
sockaddr_in
Family
Blob
2 bytes
2 bytes
4 bytes
Family
Port
Internet address
8 bytes
Not used
A new generic socket address structure was defned as part of the IPv6
sockets API, to overcome some of the shortcomings of the existing struct
sockaddr. Unlike the struct sockaddr, the new struct sockaddr_storage is
large enough to hold any socket address type supported by the system.
The sockaddr_storage structure is defned by including the <netinet/in.h>
header
Byte Ordering
Naming
Addressing
11
memory
address A
Stored at little-endian
computer
high-order byte
Integer representation (2
byte)
D3
F2
Stored at big-endian
computer
low-order byte
high-order byte
low-order byte
Answer:
Message in Memory of
little-endian Computer
Processing
Message is:
[Hello,1]
48 45 4C 4C 6F 01
00
Example:
Message is sent
across Network
Message is:
[Hello,512]
Processing
Message in Memory of
of big-endian Computer
48 45 4C 4C 6F 01
00
13
uint16_t htons(uint16_t
host16bitvalue)
uint32_t htonl(uint32_t host32bitvalue)
uint16_t ntohs(uint16_t net16bitvalue)
uint32_t ntohs(uint32_t net32bitvalue)
14
host16bitvalue);
host32bitvalue);
net16bitvalue);
net32bitvalue);
# if __BYTE_ORDER == __BIG_ENDIAN
/* The host byte order is the same as network byte order,
so these functions are all just identity. */
# define ntohl(x) (x)
# define ntohs(x) (x)
# define htonl(x) (x)
# define htons(x) (x)
# else
# if __BYTE_ORDER == __LITTLE_ENDIAN
#
define ntohl(x)
__bswap_32 (x)
#
define ntohs(x)
__bswap_16 (x)
#
define htonl(x)
__bswap_32 (x)
#
define htons(x)
__bswap_16 (x)
# endif
# endif
Host name
identifies a single host (Domain Name System)
variable length string (e.g. www.berkeley.edu)
is mapped to one or more IP addresses
IP Address
written as dotted octets (e.g. 10.0.0.1)
32 bits. Not a number! But often needs to be
converted to a 32-bit to use.
Port number
identifies a process on a host
16 bit number
19
#include <arpa/inet.h>
int inet_aton(const char *strptr, struct in_addr *addrptr);
Returns: 1 if string was valid, 0 on error
in_addr_t inet_addr(const char *strptr);
Returns: 32-bit binary network byte ordered IPv4 address;
INADDR_NONE if error
Problem: 255.255.255.255 it will return INADDR_NONE
char *inet_ntoa(struct in_addr inaddr);
Returns: pointer to dotted-decimal string
To help specify this size, the following two defnitions are defned by includin
<netinet/in.h> header:
#define INET_ADDRSTRLEN 16 /* for IPv4 dotted-decimal */
#define INET6_ADDRSTRLEN 46 /* for IPv6 hex string */
If len is too small to hold the resulting presentation format, including the terminating
a null pointer is returned and errno is set to ENOSPC.
Sock_ntop Function
sock_ntop that takes a pointer to a socket address structure, looks inside the structu
and calls the appropriate function to return the presentation format of the address.
#include "unp.h"
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.
#include "unp.h"
ssize_t readn(int fledes, void *buff, size_t nbytes);
ssize_t writen(int fledes, const void *buff, size_t nbytes);
ssize_t readline(int fledes, void *buff, size_t maxlen);
All return: number of bytes read or written, 1 on error
26
Issues in Client/Server
Programming
27
Options:
29
30
32
Local IP address
33
34
Partial Close
shutdown(ints,intdirection);
direction can be 0 to close the reading end
or 1 to close the writing end.
shutdown sends info to the other process!
38
39
TCP Reads
Server Design
Iterative
Connectionless
Iterative
Connection-Oriented
Concurrent
Connectionless
Concurrent
Connection-Oriented
42
43
Concurrent
Large or variable size requests
Harder to program
Typically uses more system resources
Iterative
Small, fxed size requests
Easy to program
44
Connectionless vs.
Connection-Oriented
Connection-Oriented
EASY TO PROGRAM
transport protocol handles the tough
stuff.
requires separate socket for each
connection.
Connectionless
less overhead
no limitation on number of clients
45
Connectionless Iterative
Uses connectionless protocol: UDP.
server
Concurrent Server
Design Alternatives
49
More Forking
Example
Prefork()d Server
56
Preforking
Prethreaded Server
60
Many factors:
Statelessness
62
63
THANK YOU