46
Networking

Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Networking

Page 2: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Computer Networks

● Computer networks allow us to get amazing things done.● Sharing knowledge (Wikipedia, Khan Academy,

Coursera, Udacity, etc.)● Solving huge problems (folding@home,

Mechanical Turk, etc.)

● Computer networks prevent us from getting amazing things done.● Social networks (Facebook, Google+, etc.)● Streaming video (Netflix, Hulu, etc.)

Page 3: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

How does it all work?

Page 4: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical
Page 5: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical
Page 6: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

LOL

Page 7: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

LOL

Page 8: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

LOL

Page 9: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

LOL

Page 10: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

LOL

Page 11: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

LOL

Page 12: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

OMGLOL

Page 13: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Sending Data

● Data is sent across the Internet in packets.

● Each packet contains a message (called the payload), along with extra information to help it get to its destination correctly.

Page 14: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical
Page 15: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

Page 16: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

Page 17: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

133.0.0.1LOL

Page 18: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

133.0.0.1LOL

Page 19: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

133.0.0.1LOL

Page 20: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

133.0.0.1LOL

Page 21: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

133.0.0.1LOL

Page 22: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

133.0.0.1LOL

Page 23: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

128.0.0.1 128.0.0.2128.0.0.0

129.0.0.0

129.0.0.1

130.0.0.0130.0.0.1

130.0.0.2

131.0.0.0

132.0.0.0 133.0.0.0

133.0.0.1

133.0.0.2

ROFLCOPTER

Page 24: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

IP Addresses

● Each computer may have one or more IP addresses so that it can receive messages over the Internet.● Similar to a phone number.

● There are two types of IP addresses:● IPv4: 232 possible addresses (about four

billion), and we've just about run out!● IPv6: 2128 possible addresses (about 4×1034),

and we're very unlikely to run out in the future.

Page 25: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Hostnames

● In order to make it easier to find remote computers, computers can have names associated with them.● www.google.com● www.stanford.edu

● These names are called hostnames.

● A system called the domain name system (or DNS) is responsible for converting domain names into IP addresses.● Like a huge HashMap<String, IP Address>

Page 26: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

A Small Problem

● At any one time, you could be● Surfing the web,● Downloading music from iTunes,● Checking your email,● etc.

● You might have packets from many different machines all arriving at once.

● How does the computer know how to send each message to the right program?

Page 27: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Ports

● Every packet is labeled with a port number that lets the destination computer know how to process the message.● Has nothing to do with physical ports on the

computer; it's just a way of differentiating traffic.

● Different applications listen in on different ports:● Sending mail (SMTP): Port 25● Browsing the web (HTTP): Port 80● Checking email (IMAP): Port 143

Page 28: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

368980 143

Page 29: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

80CutePuppies.html

368980 143

Page 30: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

368980 143

80CutePuppies.html

Page 31: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

368980 143

Page 32: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

3689Let it Go.m4a

368980 143

Page 33: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

368980 143

3689Let it Go.m4a

Page 34: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

368980 143

Page 35: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Sockets

● A socket is a combination of an IP address (destination computer) and port number (what program should read the message).

● Contains all the information necessary to ensure that a message gets to the right program on the right computer.

● To set up a connection to a remote computer, you need to create a socket connection to that computer.

Page 36: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Application Protocols

● Now that we can get computers talking to one another, how do they communicate information in a meaningful way?

● An application protocol is a set of rules computers can follow to communicate over a network.

● Each computer follows the rules of the protocol to share information.

Page 37: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

An Example: HTTP

Page 38: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Networking in Java

● To connect to a remote machine:● Create a socket connection to the machine

by giving a combination of the host name and the port.

● Create a BufferedReader to read messages coming from the other computer.

● Create a PrintWriter to send messages to the other computer.

● Send and receive messages as you see fit!

Page 39: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Time-Out for Announcements!

Page 40: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Assignment 7

● Assignment 7 (NameSurfer) is due on Monday at 3:15PM.● Stop by the LaIR with questions!● Stop by our office hours with questions!

● Midterms will be graded this weekend; should be returned on Monday.

Page 41: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Casual CS Dinner

● Stanford Women in Computer Science (WiCS) is holding their second biquarterly Casual CS Dinner tonight.

● 6PM – 8PM on the fifth floor of Gates.● Great event – highly recommended!

Page 42: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

RSVP: https://hackoverflow.typeform.com/to/zNSIMK

Page 43: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Back to CS106A!

Page 44: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Client/Server Architecture

● A server is a program that waits for incoming connections.● Typically, has some data or service that it

can provide.

● A client is a program that initiates a connection to a server.● Typically, wants to use that data or service.● The program we just wrote was a client that

connected to a remote web server.

Page 45: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

Acting as a Server

● A program can act as a server as follows:● Create a ServerSocket on a given port and

wait for an incoming connection.● Obtain a Socket that lets you communicate

with the machine that has connected.● Proceed as before.

Page 46: Networking - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/23/Slide… · Social networks (Facebook, Google+, etc.) ... Has nothing to do with physical

A Simple Chat Program