Title: Sockets and Other Necessities for Assignment 2
1Sockets and Other Necessities for Assignment 2
- Stephen Curial
- curial_at_cs.ualberta.ca
- http//www.cs.ualberta.ca/curial
2Outline
- Sockets
- Network Byte Order
- I/O Multiplexing
- Processes
- Timers
- General Comments
3What is a Socket?
4What is a Socket?
- Inter Process Communication (IPC)
- Allows 2 processes to communicate
- Dont have to be on the same computer
- Give the programmer a file descriptor (fd) as a
handle to the socket
5How do sockets work?
Created using socket() sys. call
Created using socket() sys. call
Server
Client
Port a
Port
6How do sockets work?
connect()
Server
Client
Port a
Port
7How do sockets work?
Server
Client
Port a
Port
Port b
Created with accept()
8How do sockets work?
Server
Client
Port a
Port
Port b
This is the connection the 2 processes use for
reading and writing.
9What functions are used?
Server
Client
socket()
socket()
bind()
connect()
listen()
accept()
/ block until connection from client /
10socket()
- Create a fd for use in network communication
- int socket(int domain, int type, int protocol)
- domain AF_UNIX, AF_INET, PF_INET,
- type SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
- protocol 0 IPPROTO_TCP
IP
TCP
11bind()
- Bind a local IP address protocol to a socket
- int bind(int sockfd,
- struct sockaddr my_addr,
- socklen_t addrlen)
12listen()
- Place socket in passive mode and set the number
of incoming TCP connections the system will
en-queue. - int listen(int s, int backlog)
13connect()
- Connect to the server
- int connect(int sockfd, const struct
sockaddr serv_addr, socklen_t addrlen)
14accept()
- Accept the next incoming connection
- int accept(int s,
- struct sockaddr addr,
- socklen_t addrlen)
15close()
- Terminate communication and de-allocate file
descriptor. - int close(int fd)
16read()
- Read count bytes from fd into buf
- count bytes must have been written to the socket
or the read will block - ssize_t read(int fd, void buf,
- size_t count)
17write()
- Write count bytes from fd into buf
- ssize_t write(int fd, const void buf,
- size_t count)
18Outline
- Sockets
- Network Byte Order
- I/O Multiplexing
- Processes
- Timers
- General Comments
19Endianness
- Assume we have the word 0xAABBCCDD at address
0x100 - This can cause portability issues.
Big Endian
Little Endian
AA
BB
CC
DD
DD
CC
BB
AA
100
101
102
103
100
101
102
103
20Endianness Solution
- Send everything over the network in a common
format - Network Byte Order (Big Endian)
- uint16_t htons(uint16_t hostshort) / host to
network short / - uint32_t htonl(uint32_t hostlong) / host to
network long / - uint16_t ntohs(uint16_t netshort) / network to
host short / - uint32_t ntohl(uint32_t netlong) / network
to host long /
21Port Numbers
Pier 10
400
BIN 707
22Port Numbers
- 0 - 1024 for well-known services
- Eg. http, ssh, ftp, etc.
- Free to use 1025 - 65535
23Header Payload Messages
- struct header
- int msg_type
- int msg_size
msg_type
msg_size
anything(int,char,)
int
int
header
payload
24Outline
- Sockets
- Network Byte Order
- I/O Multiplexing
- Processes
- Timers
- General Comments
25What is I/O Multiplexing?
26What is I/O Multiplexing?
- Its how you (a single thread) talk to multiple
sockets without blocking.
27I/O Multiplexing using select()
- fd_set (is simply a bit-vector)
- Think of it as an array of Booleans
- FD_CLR(int fd, fd_set set)
- FD_ISSET(int fd, fd_set set)
- / returns true/false (1/0) /
- FD_SET(int fd, fd_set set)
- FD_ZERO(int fd, fd_set set)
1023
0 1 2
28select()
- int select(int n, fd_set readfds,
- fd_set writefds, fd_set
exceptfds, struct timeval
timeout) - n - the largest fd
- readfds - select will modify this bit-vector so
only fds with something to read have a value of
1.
29Typical usage of select() - Initialization (this
is in the server)
- fd_set allfds / contains a 1 at the location of
the fd for every connection (also has a 1 in the
location of the fd for the port we listen for
connections on) / - fd_set readfds
- int maxfd / The largest fd connected /
- int server_fd / returned from socket() /
- int i
- FD_ZERO(allfds)
- FD_SET(server_fd, allfds)
- maxfd server_fd
30Typical usage of select() - Main Server Loop
- while(server_open)
- memcpy(readfds, allfds, sizeof(fd_set) ) /
memcpy(void dest, const void src, size_t n) / - select(maxfd1,readfds,NULL,NULL,NULL)
- for(i 0 i lt maxfd i)
- if(FD_ISSET(i,readfds))
- if(i server_fd)
- / accept client
- add new fd to allfds
- adjust max /
- else
- / Service client with fd i/
-
-
- / end for /
- / end while /
31Yet Another Way to Multiplex poll()
- int poll(struct pollfd fdarray,
- unsigned int num_fds,
- int timeout)
- struct pollfd
- int fd
- short events / events of interest /
- short revents / events that occurred /
-
-
32poll()
revents
fd
events
fdarray0
revents
fd
events
fdarray1
revents
fd
events
fdarray2
revents
fd
events
fdarray3
revents
fd
events
fdarrayn
33poll()Example (Not Typical) Usage
- struct pollfd fdarray10
- for(i 0 i lt 10 i)
- fdarrayi.fd sock_fd
- fdarrayi.events POLLIN POLLPRI
-
-
- while(1)
- poll(fdarray, 10, INFTIM)
- for i 0 i lt 10 i)
- if(fdarray.revents (POLLIN POLLPRI))
- / Service event /
-
-
34Outline
- Sockets
- Network Byte Order
- I/O Multiplexing
- Processes
- Timers
- General Comments
35Processes Threads
- How do you create a new process?
- Build one from scratch
- Load code and data into memory
- Create a dynamic memory workspace (heap)
- Create a process control block (pcb)
- Clone an existing one
- Stop current process save its state
- Make a copy of code, data, heap, pcb
- Use copy on write for efficiency
Based on Paul Lus 379 slide
36fork()
- Unix uses fork() to create a copy of the calling
process - pid_t fork()
37Typical usage of fork()
if( fork() ) / Parent / else /Child /
Based on Paul Lus 379 slide
38Process vs. Thread
- Creating a thread is cheap(er)
- Switching between threads is cheaper
- Threads within a process can share resources
- Same address space
- No memory protection
39Memory Layout of a Single Process
kernel
0x00000000
PCB
Stack
Heap
Data
Text
0xFFFFFFFF
Based on Paul Lus 379 slide
40Process vs. ThreadMemory Layout
Traditional Process
Multi-threaded Process
Text
Text
Data
Data
Stack
Stack
Stack
Stack
Based on Paul Lus 379 slide
41POSIX Threads - pthreads
- include ltpthread.hgt
- int pthread_create(pthread_t thread, const
pthread_attr_t attr, void
(start_routine)(void ), void arg) - int pthread_join(pthread_t thread, void
value_ptr)
42Outline
- Sockets
- Network Byte Order
- I/O Multiplexing
- Processes
- Timers
- General Comments
43Time
- http//www.cplusplus.com/ref/ctime/
Functions clock Return number of clock
ticks since process start ctime Convert
time_t value to string difftime Return
difference between two times gmtime Convert
time_t value to tm structure as UTC
time localtime Convert time_t value to tm
structure as local time mktime Convert tm
structure to time_t value time Get
current time
44Timers - Using the Alarm Interrupt
- include ltstdio.hgt
- include ltsignal.hgt
- include ltunistd.hgt
- define KEEP_ALIVE_TIME 5
- void send_keep_alive()
- alarm(KEEP_ALIVE_TIME)
- printf(\n)
-
- int main()
- signal(SIGALRM,send_keep_alive)
- alarm(KEEP_ALIVE_TIME)
- while(1)
-
- return 0
45Outline
- Sockets
- Network Byte Order
- I/O Multiplexing
- Processes
- Timers
- General Comments
46Where to find more information
- If I havent presented something that you think
that you need look for it. - google
- man pages
- Stevens Advanced Programming In the Unix
Environment - Newsgroup - ask your classmates
47How do I get a good mark?
- Test your assignments before submission!
- 90 of the marks lost on AS1 could have been
caught through some simple testing - Once all of your files are tared create a new
directory and un-tar the files, compile, re-test. - Read the assignment spec. very carefully!
48Have a good weekend!