Notes
Notes
Network Programming
Application
FTP HTTP Video Audio
Layers
Network IP
Data Link
Ethernet WLAN 4G WiFi
Physical
Programming
n Sockets API
¡ An interface to the transport layer
n Introduced in 1981 by BSD 4.1
n Implemented as library and/or system calls
n Similar interfaces to TCP and UDP
n Can also serve as interface to IP (for super-
user); known as “raw sockets”
File 2
File 3
File 2
Client
n Client
Can you think of any network ¡ Initiates contact
apps that are not client/server? ¡ Waits for response
Spring 2018 Copyright ©: CS 438 Staff, University of Illinois 14
Server-side service models
n Concurrent
¡ Server processes multiple clients’ requests
simultaneously
n Sequential
¡ Server processes only one client’s requests at a
time
n Hybrid
¡ Server maintains multiple connections, but
processes responses sequentially
n Which one is best?
Spring 2018 Copyright ©: CS 438 Staff, University of Illinois 15
Wanna See Real Clients and
Servers?
n Apache Web server
¡ Open source server first released in 1995
¡ Name derives from “a patchy server” ;-)
¡ Software available online at http://www.apache.org
n Mozilla Web browser
¡ http://www.mozilla.org/developer/
n Sendmail
¡ http://www.sendmail.org/
n BIND Domain Name System
¡ Client resolver and DNS server
¡ http://www.isc.org/index.pl?/sw/bind/
Spring 2018 Copyright ©: CS 438 Staff, University of Illinois 16
What interfaces to expose to
programmer?
This is a long
sequence of text
I would like to send
to the other host
This is a long
sequence of text
I would like to send
to the other host
connection moved
to complete queue
connect completes
connection added to
incomplete queue
accept
n Notes
¡ After accept() returns a new socket
descriptor, I/O can be done using read() and
write()
¡ Why does accept() need to return a new
descriptor?
write accept
read
write
read
n Example
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
Spring 2018 Copyright ©: CS 438 Staff, University of Illinois 47
Functions: read
int read (int sockfd, char* buf, size_t nbytes);
n Read data from a stream (TCP) or “connected”
datagram (UDP) socket
¡ Returns number of bytes read or -1, sets errno on failure
¡ Returns 0 if socket closed
¡ sockfd: socket file descriptor (returned from socket)
¡ buf: data buffer
¡ nbytes: number of bytes to try to read
¡ Example
if((r = read(newfd, buf, sizeof(buf))) < 0) {
perror(“read”); exit(1);
}
socket
socket bind
sendto
recvfrom
sendto
recvfrom
IP
Ethernet Adapter
/* 1) create socket s1 */
/* 2) create socket s2 */ What problems does
/* 3) bind s1 to port 2000 */
/* 4) bind s2 to port 3000 */
this code have?
while(1) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
recvfrom(s2, buf, sizeof(buf), ...);
/* process buf */
}
n Bit vectors
¡ Only first num_fds checked
¡ Macros to create and check sets
fds_set myset;
void FD_ZERO (&myset); /* clear all bits */
void FD_SET (n, &myset); /* set bits n to 1 */
void FD_CLEAR (n, &myset); /* clear bit n */
int FD_ISSET (n, &myset); /* is bit n set? */
n Time structure
Number of seconds since
midnight, January 1, 1970
GMT
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
unix will have its own "Y2K" problem one
second after 10:14:07pm, Monday
January 18, 2038 (will appear to be
3:45:52pm, Friday December 13, 1901)
Spring 2018 Copyright ©: CS 438 Staff, University of Illinois 78
Select
Which file
n High-resolution sleep function descriptors are set
¡ All descriptor sets NULL and what should the
¡ Positive timeout
timeout value be?
n Wait until descriptor(s) become ready
¡ At least one descriptor in set
¡ timeout NULL
n Wait until descriptor(s) become ready or timeout occurs
¡ At least one descriptor in set
¡ Positive timeout
n Check descriptors immediately (poll)
¡ At least one descriptor in set
¡ 0 timeout
Which to use?
n BSD-family (e.g., FreeBSD, MacOS)
¡ poll() just calls select() internally
n System V family (e.g., AT&T Unix)
¡ select() just calls poll() internally
n Thread management
¡ Creating, detaching, joining, etc.
Set/query thread attributes
n Mutexes
¡ Synchronization
n Condition variables
¡ Communications between threads that
share a mutex
Spring 2018 Copyright ©: CS 438 Staff, University of Illinois 83
Creating a Thread
int pthread_create (pthread_t* tid,
pthread_attr_t* attr, void*(child_main), void*
arg);
n pthread_create() takes a pointer to a function as
one of its arguments
¡ child_main is called with the argument specified by arg
¡ child_main can only have one parameter of type void *
¡ Complex parameters can be passed by creating a structure
and passing the address of the structure
¡ The structure can't be a local variable
n When coding
¡ Include <pthread.h> first in all source
files
n When compiling
¡ Use compiler flag –D_REENTRANT
n When linking
¡ Link library -lpthread