Title: CPSC 441 Tutorial
1CPSC 441 Tutorial
2Contact info
- www.cpsc.ucalgary.ca/gongm/CPSC441
- gongm_at_cpsc.ucalgary.ca
3Assignment 1 socket programming
- Whats a socket?
- A way to speak to other programs using standard
Unix file descriptors - Sockets represents end points in a line of
communication. - Using these line of communication between two
points are established. - Jeez, everything in Unix is a file!
4Referencing A socket
- When a new file is opened using open()
- Next available lowest file descriptor is
returned. - This is a handle by which all future operations
can use to reference. - Same way an handle is generated for a socket when
a socket is opened. - Socket pair four-tuple local IP
addresslocal port, foreign IP address foreign
port
5Types of Sockets
- Two types of Internet Sockets
- Stream sockets (Analogy phone)
- Reliable two-way connected communication streams.
- SOCK_STREAM
- Datagram Sockets (Analogy mail)
- Connectionless communication, best effort
- SOCK_DGRAM
6Necessary Background Information POSIX data
types
- int8_t signed 8bit int
- uint8_t unsigned 8 bit int
- int16_t signed 16 bit int
- uint16_t unsigned 16 bit int
- int32_t signed 32 bit int
- uint32_t unsigned 32 bit int
- u_char, u_short, u_int, u_long
7More POSIX data types
- sa_family_t address family of socket address
structure - socklen_t length of struct, normally
uint32_t - in_addr_t IPv4 address,
- normally uint32_t
- in_port_t IP port number,
- normally uint16_t
8Socket Address Structures
- Predefined data structures
- struct sockaddr_in
/ INET socket addr info / - unit8_t sin_len /length of
structure(16)/ sa_family_t sin_family / set me
to AF_INET / - in_port_t sin_port / 16 bit TCP or
UDP port num, / - /network byte order/
- struct in_addr sin_addr /
32 bit IPv4 address / - /network byte order/
- char sin_zero8 / not used /
-
- IP address (32-bit numeric representation)
- struct in_addr
- in_addr_t s_addr /
32 bit IPv4 address, - /network byte order /
-
9Generic Socket Address Structure
- generic socket address.
- struct sockaddr
- unit8_t sin_len
- sa_family_t sin_family / address family
AF_xxxx value / - char sa_data14 / protocol- spe
cific address /
10sockaddr_in
sockaddr
sin_len
sa_len
sa_family
sa_data
11Byte Ording Functions
- Integers
- little endian least significant bit first
- big endian most significant bit first
- The terms little-endian and big-endian
indicate which end of the multibyte value, the
little end or the big end, is stored at the
starting address of the value
12Data Types and structures
- There are two type of byte ordering
- Most significant byte first. (big-endian)
- Least significant byte first. (little-endian)
- Former is called Network byte ordering.
- Latter is called host byte ordering
13Network Byte Order Functions
- h host byte order n network byte
order - s short (16bit) l long
(32bit) - uint16_t htons(uint16_t)
- uint16_t ntohs(uint_16_t)
- uint32_t htonl(uint32_t)
- uint32_t ntohl(uint32_t)
14Conversion of Natives
- htons() -- "Host to Network Short"
- htonl() -- "Host to Network Long"
- ntohs() -- "Network to Host Short"
- ntohl() -- "Network to Host Long"
- sin_addr and sin_port need to be in network byte
order. - sin_family need not be in network byte order.
15Flowchat
TCP Server
socket()
bind()
TCP Client
listen()
socket()
connection establishment 3-way handshake
accept()
connect()
blocks until connection from client
write()
data (request)
read()
process request
data (reply)
read()
write()
close()
end-of-file notification
read()
close()
16Socket Conceptual View
17Creating a socket1
- In order to create a socket pair following
function has to be used. - Format int socket(family, type, protocol)
18Protocol family constants
19Type of socket
20Creating a socket2
- family, type and protocol parameters
- family PF_INET/AF_INET, PF_UNIX/AF_UNIX
- type
- SOCK_DGRAM datagram service (i.e., UDP)
- SOCK_STREAM byte stream service (i.e.,TCP)
- protocol usually 0 for default type
21Assigning an address to a socket
- The bind() system call is used to assign an
address to an existing socket. - int bind( int sockfd,
- const struct sockaddr myaddr, int
addrlen) - bind returns 0 if successful or -1 on error.
const!
22bind()
- calling bind() assigns the address specified by
the sockaddr structure to the socket descriptor. - You can give bind() a sockaddr_in structure
- bind( mysock,
- (struct sockaddr) myaddr,
- sizeof(myaddr) )
23bind() Example
- int mysock,err
- struct sockaddr_in myaddr
- mysock socket(AF_INET,SOCK_STREAM,0)
- myaddr.sin_family AF_INET
- myaddr.sin_port htons( portnum )
- myaddr.sin_addr htonl( ipaddress)
- errbind(mysock, (sockaddr ) myaddr,
sizeof(myaddr))
24Uses for bind()
- There are a number of uses for bind()
- Server would like to bind to a well known address
(port number). - Client can bind to a specific port.
- Client can ask the O.S. to assign any available
port number.
25Port number - who cares ?
- Clients typically dont care what port they are
assigned. - When you call bind you can tell it to assign you
any available port - myaddr.port htons(0)
26What is my IP address ?
- How can you find out what your IP address is so
you can tell bind() ? - There is no realistic way for you to know the
right IP address to give bind() - what if the
computer has multiple network interfaces? - specify the IP address as INADDR_ANY, this tells
the OS to take care of things.
27IPv4 Address Conversion
- int inet_aton( char , struct in_addr )
- Convert ASCII dotted-decimal IP address to
network byte order 32 bit value. Returns 1 on
success, 0 on failure. - char inet_ntoa(struct in_addr)
- Convert network byte ordered value to ASCII
dotted-decimal (a string).
28- A Few Words about Port Numbers
- 1-255 standard services (21 ftp, 25 SMTP, 80
HTTP) - 1-1023 available only to system
- 1024-4099 usable by system users
- 5000 - usable only by users
29Listen()
- This function is called after both the socket()
and bind() functions - int listen (int sockfd, int backlog)
- notify OS/network ready to accept requests
- backlog max. listen queue size while waiting for
accept() - backlog does not block/wait for requests
30Accept()
- int accept(int sockfd, struct sockaddr
fromaddr, int addrlen) - use socket (sockfd) for accepting incoming
connection requests - create new socket (return value) for data
exchange w/ client - block until client connects, cannot selectively
accept
31Flowchat
TCP Server
socket()
bind()
TCP Client
listen()
socket()
connection establishment 3-way handshake
accept()
connect()
blocks until connection from client
write()
data (request)
read()
process request
data (reply)
read()
write()
close()
end-of-file notification
read()
close()
32connect()
- int connect (int sockfd, const struct sockaddr
toaddr, int addrlen) - Returns 0 if OK, -1 on error
- Client issues connect() to establish connection
with server
33Fork functions
- Pid_t fork(void)Return 0 in child, process ID of
child in parent - For ( )
- Connfd accept (listenfd, )
- If ((pidFork())0)
- doit(connfd) //process the request
- close(connfd) //done with this client
- exit(0) //child terminates
-
- close(connfd) //parent closes connected
socket -
34Sending and Receiving Data
- Connection-oriented
- send() and recv() or read() and write()
- Format
- int send(int sockfd, char buff, int nbytes, int
flags) - int recv(int sockfd, char buff, int nbytes, int
flags)
35Closing a Socket
- Format int close(int sockfd)
- Call this function to close a created socket.
- System takes care of buffered data.