Short Answer Problems
- (3.1) Stream and message.
- (3.2 and 3.3) On the Internet:
Stream | Message |
Connection-oriented | Connectionless |
1-1 | 1-1, 1-Many, or Many-1 |
Bytes | Messages |
Arbitrary length | Limited-size messages |
Used for just about anything | Best for multimedia |
Built on TCP | Built on UDP |
- (3.4) Whatever size it wants.
- (3.5) Message (because it is better for broadcast).
- (3.6) Messages can be (1) lost, (2) delivered out of order, (3) duplicated.
- (3.7) The algorithm is:
- A pair of apps requests a connection;
- The apps use the connection to exchange data;
- The apps request that the connection be terminated.
- (3.9) Applications:
Server | Client |
Doesn’t need to know who the clients are | Must know the server |
Passively waits for connection | Actively initiates the connection |
Stays running | Generally terminates connection when done with server |
Special-purpose program providing the same service to multiple clients | Can be an arbitary program, contacting different servers it needs whenever it needs something |
Communicates by sending and receiving data |
- (3.10) Hardware:
Server-class Machine | Client Machine |
Sophisticated hardware with powerful operating system providing concurrency so many clients can be handled at the same time | End-user device (laptop, phone, etc.) |
Multiple CPUs, lots of memory, multi-user, multi-processing, multi-threaded, etc. | Can be simple |
- (3.11) Yes, of course. Like, why not? How else would it make a request?
- (3.12) Straight from the book:
- A single client
- A single server
- Multiple copies of a client that contact a given server
- Multiple clients that each contact a particular server
- Multiple servers, each for a particular service
- (3.13) Of course not, some computers can't do multiprocessing or multitasking, which are required to allow clients to access services concurrently.
- (3.14) IP address and port number.
- (3.15) Again, from the book:
- Use DNS to translate name to IP address;
- Specify that the service uses port N;
- Contact server and interact.
- (3.16) Threads of control. (Can be implemented with real threads on different cores or processors, green threads, or events.)
- (3.17) Bottlenecks.
- (3.18) Windows, macOS.
- (3.19) In C, through its integer descriptor, the same way you reference any O.S. resource. In other languages, libraries provide a socket object.
- (3.20) socket, bind, listen, accept, getpeername, getsockopt, setsockopt, connect, recv, recvmsg, recvfrom, send, sendmsg, sendto, close, shutdown.
- (3.21) Client: socket → conenct → send/recv → close. Server: socket → bind → listen → accept → send/recv → close or go back to listen. (The server part is not totally accurate because listen and accept are generally done on a main thread and worker threads do the send/recv part.
- (3.22) recv and send.
- (3.23) It can but it shouldn’t—the O.S. should pick the port for the client.
Scripts
Random Number Server and Client
randomnumberserver.js
const net = require('net');
const server = net.createServer((socket) => {
socket.end(`${Math.random()}\n`);
});
server.listen(53211);
randomnumberclient.js
const net = require('net');
const client = new net.Socket();
client.connect({ port: 53211, host: process.argv[2] });
client.on('data', (data) => {
console.log(data.toString('utf-8'));
});
Random Number Web Server
randomnumberwebserver.js
const http = require('http');
const PORT = 53211;
http.createServer((request, response) => {
if (request.method === 'GET' && request.url === '/random') {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end(`${Math.random()}`, 'utf-8');
} else if (request.method === 'GET' && request.url === '/') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end('<a href="/random">Get random number from server</a>');
} else {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end('Sorry, that’s not there');
}
}).listen(PORT);
console.log(`Random number web server running at port ${PORT}`);
Chat Web Server and Client
Missing content
Missing content