计算机网络(二)

发布于 作者: Ethan

Bandwidth

The number of bits transferred over the network per time.

e.g. 10Mbps network = send 10 Million bits per second

Latency

Time to travel one end to the other.

Round Trip Time (RTT) = time to reach the other end and back.

Latency = Propagation + Transmit + Queue

Propagation = Distance / SpeedOfLight

Transmit = Size / Bandwidth

Bandwidth & Delay

  • A client sending/receiving 1-byte message is latency bound.
  • A large file transfer (1MB) is bandwidth bound.

Another way to think about latency is in terms of instructions per mile.

  • a computer can exec 100b instruction / sec
  • link latency is 100ms RTT
  • distance covered is 5000 miles
  • thus having computer idle the full 100ms for reply result in a waste of 10b instructions, or 2m instructions per mile

BDP

Bandwidth-Delay Product: the number of bits that could be in transit through the link at any given instant.

BDP = Bandwidth * RTT

Higher Bandwidth != Lower Latency

Throughput

To improve communication either

  • reduce RTT
  • increase the amount of data per RTT

In other words, we need to maximize throughput:

Throughput = TransferSize / TransferTime

Jitter

The variation in latency from packet to packet.

It impacts streaming applications (e.g. video/audio).

Causing discontinuity due to variable packet arrival. If know the lower & upper bounds on latency can avoid by delaying the time to start the vedio.

Sockets

The standard interface for app's processes to itneract w/ the network and communicate w/ each other.

socket

  • Network: deliver data packet to the dest host, based on the dest host ID (IP address).
  • OS: deliver to the dest socket, based on the dest port number.
  • App: read data from and write data to the socket, and interpret the data.

Types of socket communicatioin

  • Stream Socket (TCP): stream of bytes, reliable, connection-oriented.
  • Datagram Socket (UDP): collection of messages, best effort, connectionless.

Port Number

5-tuple uniquely id a socket connection between hosts:

  • 2 IP address and 2 ports
  • underlying transport protocol

UNIX Socket API

unix socket tcp api unix socket udp api

C Interface

Server:

getaddrinfo(NULL, MY_PORT, &hints, &result);
//hints.ai_family = AF_UNSPEC; (IPv4 or IPv6, whichever)
//hints.ai_socktype = SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)
sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

bind(sockfd, res->ai_addr, res->ai_addrlen);

listen(sockfd, QUEUE_LENGTH);

new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);

Client:

getaddrinfo(NULL, MY_PORT, &hints, &result);
//hints.ai_family = AF_INET; (IPv4)
//hints.ai_socktype = SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)
//hints.ai_flags = AI_PASSIVE; (suitable for binding)
sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

connect(sockfd, res->ai_addr, res->ai_addrlen);

bytes_sent = send(sockfd, msg, len, 0);

recv(int sockfd, void *buf, int len, int flags);

Byte Order

Network byte order is Big Endian (most significant bit/byte stored first).

Functions to deal with: htons(), htonl(): host to network short and long. ntohs(), ntohl(): network to hsot short and long. use when put/pull data on/off wire.

Share server

Serializing requests is inefficient, use:

  • Nonblocking I/O
  • different process/thread for each request
  • some hybrid of these two approaches