Upload
denis-hensley
View
238
Download
1
Tags:
Embed Size (px)
Citation preview
Process to process communication over network
connectionsIncludes references to
Comer and Stevens
Internetworking with TCP/IP vol 3 Client-server programming and applications
Program to protocol
Network protocols allow message exchange between systems connected by networks
Application programs must connect to the network protocol implementations in the local systems to carry their messages
Two sides needed: send and receive Program activity coordination is a separate
issue
Client - Server Computing
Direction of initiation determines which side is which– Client initiates– Server responds
Same system may be client and server
Parameterization of clients
Parameter specifies machine where server resides
Optional parameter specifies the port where the server listens– Include the port number parameter in defining
clients– Especially useful for testing new versions
ex: telnet doyle.netlab.csc.villanova.edu 23
Addressing
Specify the machine
– name gets converted to IP address
Specify the port number
– Which running process to connect to.
How do you know
Which machine– you have to know the name or the IP address of
the partner• Names get converted by a Domain Name Service
Which port– Common services have assigned port numbers– telnet = 23, http = 80, etc.
Port numbers
The port numbers are divided into three ranges: the Well Known Ports, the Registered Ports, and the Dynamic and/or Private Ports.
Well Known Ports: 0 through 1023.
Registered Ports: 1024 through 49151
Dynamic and/or Private Ports: 49152 through 65535
Ref: http://www.isi.edu/in-notes/iana/assignments/port-numbers
Well known Port number samples
ssh 22telnet 23smtp 25sql*net 66bootps 67 (bootstrap server)bootpc 68 (bootstrap client)
http 80kerberos 88npp 92 (network print)iso tsap 102 (class 0 )pop3 110Sun rpc 111nntp 119 (network news)
daytime 13/tcp Daytime (RFC 867)daytime 13/udp Daytime (RFC 867)ftp-data 20/tcp File Transfer [Default Data]ftp-data 20/udp File Transfer [Default Data]ftp 21/tcp File Transfer [Control]ftp 21/udp File Transfer [Control]
Domain Name Service
Structure of Internet Names– gadget.netlab.csc.villanova.edu– Lookup in order from right to left
• netlab and csc are both entries in the villanova DNS, so that lookup can go directly from villanova to netlab without using the csc DNS
• That is always an option in configuring the servers
DNS - Christie’s table; Addresses for the canonical names
;
localhost.netlab.csc.villanova.edu. IN A 127.0.0.1
christie.netlab.csc.villanova.edu. IN A 153.104.203.200
doyle.netlab.csc.villanova.edu. IN A 153.104.203.210
gadget.netlab.csc.villanova.edu. IN A 153.104.203.42
missmarple.netlab.csc.villanova.edu. IN A 153.104.203.7
lordpeter.netlab.csc.villanova.edu. IN A 153.104.203.41
poirot.netlab.csc.villanova.edu. IN A 153.104.203.148
koko.netlab.csc.villanova.edu. IN A 153.104.203.45
sherlock.netlab.csc.villanova.edu. IN A 153.104.203.44
cadfael.netlab.csc.villanova.edu. IN A 153.104.203.43
columbo.netlab.csc.villanova.edu. IN A 153.104.203.46
weber.netlab.csc.villanova.edu. IN A 153.104.203.205
matlock.netlab.csc.villanova.edu. IN A 153.104.203.203
samspade.netlab.csc.villanova.edu. IN A 153.104.203.204
poe.netlab.csc.villanova.edu. IN A 153.104.203.50
Which domain server to use
A configuration option
Example for Windows
Port number usage When writing a new application, allow the user
to specify the port number or to use the default Some implementations do not allow the user to
specify a port number– uses the protocol’s reserved number only– Allowing a port number to be specified makes it
easier to test the implementation• allow the “real” version to run while testing a different
version
Server
Usually runs in privileged condition– needs to access protected resources– be careful about what access it gives to clients
Responsibilities– Authenticate client identity– Enforce authorization restrictions– Data protection: security and privacy– Protection: system resources from abuse
Server characteristics
Iterative– serve one client at a time
Concurrent– serve multiple clients at one time
Connectionless vs Connection-oriented
Directly related to the choice of transport protocol– UDP: connectionless– TCP: connection-oriented
Can build connection-oriented client/server application over a connectionless transport layer
Server state
Stateless server – no memory of past interaction– each request must be fully qualified
Stateful server– records state information– reduces needed content in subsequent messages– depends on reliable network communication– vulnerable to system failures, reboots
Idempotent operations
An operation that always yields the same result, regardless of repetition or missing prior steps– READ or WRITE, without automatic increment
of pointer– not idempotent - increment, add amount to
total, etc.
Concurrent processes
Multiprogramming– Apparent simultaneous execution
Multiprocessing– Real simultaneous execution
Server Concurrency
Need to serve requests from multiple clients Process
– Fundamental unit of computation– Address space plus at least one thread of
execution• instruction pointer tells where in the process the
thread is currently executing
Threads
Similar to a process except– a thread is owned by a process– a thread may share common resources
belonging to a process
A thread has a private copy of local variables
Concurrent processes In a uniprocessor system
– Each process gets a slice of the CPU time in turn– All appear to run at the same time
If each process has only one thread of execution, that process shares the CPU with other processes on a time sharing plan
If a process has more than one thread, the CPU time is divided among the threads.– OS vary: divide time among all threads or divide among processes and
multiple threads share the time allotted to their parent process
In a multiprocessor system, each process or thread may be allocated a separate CPU
Threads in servers
A process may respond to client requests by spawning a separate thread for each client
Clients appear to be served simultaneously but actually receive intermingled slices of the CPU time.
Initiating concurrent subprocesses
Windows: – _beginthread
• parameter specifies what to execute as thread
unix– fork
• new process is a copy of the initiating process
• pid identifies each
Unix concurrency
fork() creates an exact copy of the calling process and continues execution from exactly the same place in the code.– Copy includes the stack showing function calls,
local and global variables– Original and forked process continue from the
same point in execution– One distinction: fork returns a value: new process
returns 0, original returns non zero value
Unix concurrency example
sum = 0;
pid = fork();
if (pid != 0 ) { /* original */
printf (“The original process\n”);
}else { /* the new process */
printf (“The new process\n”);
Note that the order in which the two lines are printed may vary from one run to another.
Concurrency in Windows
_beginthread– parameter specifies what is to be executed
Not necessarily executing the same code as the calling process
Application Program Interface (API)
Identify endpoints of the connection Establish the communication Allow message send Allow wait for incoming message Possibly allow for incoming or outgoing high
priority data Graceful termination of the communication Handle error situations
API implementation options
Everything embedded in the operating system
TCP/IP and API run in user space TCP/IP in operating system; API in user
space Generally invisible differences for user
Sockets
A de facto standard interface to TCP/IP protocols
Defined in BSD unix
Adapted by other operating systems
The Socket API - connection orientedSocket()
Listen()
Bind()
Accept()
recv()
send()
Ser
ver
Sid
e
Cli
ent S
ide
recv()
send()
Connect()
Socket()
closesocket closesocket
Socket
Create a new socket– allocate storage for an instance of a data structure– start to fill in the values with the parameters
• protocol family the application will use• type of service to be used
Newly created socket is neither active nor passive until the application does something else
Socket
Serves same purpose as Open for files Same table may hold both file descriptors
(created by open) and socket descriptors (created by socket)
Most fields in the data structure are initially empty, filled in later
Socket data structure Family: PF_INET
– (Protocol Family_Internet protocols)
service: SOCK_STREAM– (Using TCP, connection-oriented communication)
IP addresses– local and remote
Port numbers– local and remote
Addressing
AF_INET– Address Family Internet
Endpoint address
Socket data structure
Socket data structure
Family: PF_INET
Service: SOCK_STREAM
Local IP:
Remote IP:
Local Port:
Remote port:
Etc.
One table per process
connect Establish an active connection to a remote server
– identify the remote end point• IP address• protocol port number
– fill in the remote address part of the socket data structure
– Choose and fill in the local address part of the socket data structure
– makes the socket active– causes TCP to begin the three-way handshake
Three-way handshake
SYN(seq=x)
SYN(seq=y,ack=x+1)
SYN(seq=x+1,ack=y+1)
Client Server
time
time
Tell the peer what sequence number you expect to see next
send
A write operation Used by clients and servers to move data Non-blocking
– possible brief blocking if buffers are full
recv A read operation Get incoming data from the socket Note that TCP is stream-oriented
– it does not recognize boundaries in the message– programs may need to loop to receive all of the
incoming message– separate message segments may be combined in a
single buffer retrieval UDP is message-oriented
– boundaries remain as the sender intended
closesocket
Deallocate the socket complement of the socket call if socket shared by >1 process, reference
count is decremented. Socket is deallocated when the reference count reaches zero
bind
Server side Fills in the local address
– IP address– Port number on which the service will be
available
listen
Place the socket in passive mode– ready to receive an incoming connection
Specify the size of a queue for incoming connection requests– How many concurrent users are we able to
serve?
accept Get the next incoming connection request Peer of connect
– sets up the connection between the client and server so that exchange of messages can take place
Create a new socket for an incoming connection request– The original socket remains available for receiving additional
requests– All communication for the incoming connection goes over the
new socket– When the communication is completed, the new socket is closed
Functions included in socket interface
inet_addr– takes ASCII string, dotted decimal IP address– returns binary IP address
gethostbyname– takes ASCII string, domain name– returns hostent structure
hostent structurestruct hostent { char FAR* h_name; /* official host name */
char FAR* FAR* h_aliases; /* other aliases */short h_addrtype; /* address type */short h_length; /* address length */char FAR* FAR* h_addr_list; /* list of addresses */
}#define h_addr h_addr_list[0]
Predefined and available in winsock.h (windows)
struct hostent { char *h_name; /* official host name */
char **h_aliases; /* other aliases */short h_addrtype; /* address type */short h_length; /* address length */char *h_addr_list; /* list of addresses */
}#define h_addr h_addr_list[0]
Predefined and available in netdb.h (unix)
Using gethostbyname
Struct hostent *hptr;
char *examplenam = “merlin.cs.purdue.edu”;
if (hptr = gethostbyname( examplenam )) {
/* IP address is now in hptr->h_addr */
} else {
/*error in name - handle it */
}
Comer TCP/IP vol III page 65
Other services
Getservbyname– takes two strings
• desired service
• protocol
– returns• pointer to structure of type servent
Client side operation
Steps for a client– Find IP address and protocol port number of the
server– Allocate a socket– Allow TCP to choose an arbitrary, unused local
port number– Connect the socket to the server– Use the connection for application operation– Close the connection
Servers Iterative
– processes one request at a time Concurrent
– processes multiple requests at one time Connection oriented
– Uses tcp to handle packet loss and out-of-order delivery– Require a separate socket for each connection
• possibly resource depletion if the connection is not closed
Connectionless– Uses udp which provides no quality control– Multiple clients can share a single socket
Iterative connection-oriented server Create a socket and bind to the well-known address
for the service offered Place a socket in passive mode Accept the next connection request and obtain a new
socket for the connection Repeatedly receive a request from the client,
formulate a response, and send a reply back to the client
When the client is finished, close the connection and return to the accept step for a new connectionSingle thread, handle connections from clients one at a timeOK if each request is very short.
Iterative connectionless server
Create a socket and bind to the well-known address for the service offered
Repeatedly receive the next request from a client, formulate a response, and send a reply back to the client.
One socket used repeatedly for new clientsOne client is served before another can receive attention
Concurrent Connectionless Server
Master: Create a socket and bind to the well-known address for the service offered. Leave the socket unconnected
Master: Repeatedly receive the next request from a client, and create a new slave thread to handle the response
Slave: Receive a request and access to a socket on creation Slave: Form a reply and send it back to the client Slave: Exit. (slave thread terminates after one request)
Concurrent connection-oriented server
Master: Create a socket and bind to the well-known address for the service offered. Leave the socket unconnected
Master: Place the socket in passive mode Master: Repeatedly accept the next request from a client, and
create a new slave thread to handle the response Slave: Receive a connection request (socket) upon creation Slave: Interact with the client using the connection; receive
requests and send replies Slave: Close connection and exit. Slave thread exits after
handling all requests from one client.
Server summary Iterative, connectionless server
– Good for trivial processing per request Iterative, connection-oriented server
– Good for trivial processing per request where reliable transport is required.– Overhead of establishing and terminating a tcp session can impact response
time Concurrent, connectionless server
– Unusual choice– New thread created for each request, not each client– OK if time to service a request is substantially greater than the time to create
a thread Concurrent, connection-oriented
– Most general type of server– Reliable transport– Multiple concurrent clients