Sockets and Other Necessities for Assignment 2 - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Sockets and Other Necessities for Assignment 2

Description:

bind() listen() accept() socket() connect() read() write() write() read ... bind() Bind a local IP address & protocol to a socket. int bind(int sockfd, ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 49
Provided by: stephen452
Category:

less

Transcript and Presenter's Notes

Title: Sockets and Other Necessities for Assignment 2


1
Sockets and Other Necessities for Assignment 2
  • Stephen Curial
  • curial_at_cs.ualberta.ca
  • http//www.cs.ualberta.ca/curial

2
Outline
  • Sockets
  • Network Byte Order
  • I/O Multiplexing
  • Processes
  • Timers
  • General Comments

3
What is a Socket?
4
What 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

5
How do sockets work?
Created using socket() sys. call
Created using socket() sys. call
Server
Client
Port a
Port
6
How do sockets work?
connect()
Server
Client
Port a
Port
7
How do sockets work?
Server
Client
Port a
Port
Port b
Created with accept()
8
How do sockets work?
Server
Client
Port a
Port
Port b
This is the connection the 2 processes use for
reading and writing.
9
What functions are used?
Server
Client
socket()
socket()
bind()
connect()
listen()
accept()
/ block until connection from client /
10
socket()
  • 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
11
bind()
  • Bind a local IP address protocol to a socket
  • int bind(int sockfd,
  • struct sockaddr my_addr,
  • socklen_t addrlen)

12
listen()
  • Place socket in passive mode and set the number
    of incoming TCP connections the system will
    en-queue.
  • int listen(int s, int backlog)

13
connect()
  • Connect to the server
  • int connect(int sockfd, const struct
    sockaddr serv_addr, socklen_t addrlen)

14
accept()
  • Accept the next incoming connection
  • int accept(int s,
  • struct sockaddr addr,
  • socklen_t addrlen)

15
close()
  • Terminate communication and de-allocate file
    descriptor.
  • int close(int fd)

16
read()
  • 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)

17
write()
  • Write count bytes from fd into buf
  • ssize_t write(int fd, const void buf,
  • size_t count)

18
Outline
  • Sockets
  • Network Byte Order
  • I/O Multiplexing
  • Processes
  • Timers
  • General Comments

19
Endianness
  • 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
20
Endianness 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 /

21
Port Numbers
Pier 10
400
BIN 707
22
Port Numbers
  • 0 - 1024 for well-known services
  • Eg. http, ssh, ftp, etc.
  • Free to use 1025 - 65535

23
Header Payload Messages
  • struct header
  • int msg_type
  • int msg_size

msg_type
msg_size

anything(int,char,)
int
int
header
payload
24
Outline
  • Sockets
  • Network Byte Order
  • I/O Multiplexing
  • Processes
  • Timers
  • General Comments

25
What is I/O Multiplexing?
26
What is I/O Multiplexing?
  • Its how you (a single thread) talk to multiple
    sockets without blocking.

27
I/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
28
select()
  • 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.

29
Typical 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

30
Typical 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 /

31
Yet 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 /

32
poll()
revents
fd
events
fdarray0
revents
fd
events
fdarray1
revents
fd
events
fdarray2
revents
fd
events
fdarray3

revents
fd
events
fdarrayn
33
poll()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 /

34
Outline
  • Sockets
  • Network Byte Order
  • I/O Multiplexing
  • Processes
  • Timers
  • General Comments

35
Processes 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
36
fork()
  • Unix uses fork() to create a copy of the calling
    process
  • pid_t fork()

37
Typical usage of fork()
if( fork() ) / Parent / else /Child /
Based on Paul Lus 379 slide
38
Process 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

39
Memory Layout of a Single Process
kernel
0x00000000
PCB
Stack
Heap
Data
Text
0xFFFFFFFF
Based on Paul Lus 379 slide
40
Process vs. ThreadMemory Layout
Traditional Process
Multi-threaded Process
Text
Text
Data
Data
Stack
Stack
Stack
Stack
Based on Paul Lus 379 slide
41
POSIX 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)

42
Outline
  • Sockets
  • Network Byte Order
  • I/O Multiplexing
  • Processes
  • Timers
  • General Comments

43
Time
  • 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
44
Timers - 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

45
Outline
  • Sockets
  • Network Byte Order
  • I/O Multiplexing
  • Processes
  • Timers
  • General Comments

46
Where 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

47
How 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!

48
Have a good weekend!
Write a Comment
User Comments (0)
About PowerShow.com