Socket Programming


  • Network programming (sockets, RPC(远程过程调用)(Remote Procedure Call))
  • Network (esp. Internet) architecture
  • Switching, Routing, Congestion control, TCP/IP, Wireless networks
    • Using the network
  • Interface hardware & low-level implementation issues, Naming (DNS), Error detection, compression(压缩)
    • Higher level issues
  • Encryption(加密) and Security, caching & content distribution, Peer-to-peer systems(对等网络系统)

Building blocks

  • Nodes: Computers, dedicated routers, . . .

  • Links: Coax(同轴电缆), twisted pair(双绞线), fibers, radio . . .

    • point-to-point
    • multiple access – every node sees every packet //每个节点都能互相访问
  • From Links to Networks: To scale to more nodes, use switching

    • nodes can connect multiple other nodes //多点连接
    • Recursively, one node can connect multiple networks

From Links to Networks

Protocol layering

Link Layer
  • Can view network encapsulation as a stack

  • A network packet from A to D must be put in link packets A to B, B to C, and C to D

    • Each layer produces packets that become the payload of the lower-layer’s packets //上层向下层传包
    • This is almost correct, but TCP/UDP “cheat” to detect certain errors in IP-level information like address (?

OSI layers



Addressing 定址

  • Each node typically has unique address

    • (or at least is made to think it does when there is shortage) //ipv4可能地址不足
  • Each layer can have its own addressing

    • Link layer: e.g., 48-bit Ethernet address (interface) //6Bytes

Ethernet address

Ethernet address

  • Network layer: 32-bit IP address (node) //IPv4 4Bytes, IPv6 8Bytes.

IPv4 address

ip add

IPv4 vs. IPv6

IP4 ip6

  • Transport layer: 16-bit TCP port (service) //2Bytes

TCP port // HTTP 80

tcp port

  • Routing is process of delivering data to destination across multiple link hops


one portion of the path between source and destination.

  • The hop count refers to the number of intermediate devices through which data must pass between source and destination.

  • A large number of hops between source and destination implies lower real-time performance.

  • Special addresses can exist for broadcast/multicast (???)

Hourglass 沙漏


  • Many application protocols over TCP & UDP
  • IP works over many types of network
  • This is “Hourglass” philosophy of Internet
    • Idea: If everybody just supports IP, can use many different applications over many different networks
    • In practice, some claim narrow waist is now network and transport layers, due to NAT (lecture 12) (插眼。。ip掩蔽

Internet protocol

  • Most computer nets connected by Internet protocol

    • Runs over a variety of physical networks, so can connect Ethernet, Wireless, people behind modem lines, etc. //IP 凌驾于物理网络之上
  • Every host has a unique 4-byte IP address
    • E.g., ! //IPv4
    • Given a node’s IP address, the network knows how to route a packet (lectures 3+4) //有IP地址就可以向他路由包了
  • But how do you build something like the web?
    • Need naming (look up – DNS (lecture 8)
    • Need API for browser, server (CS110/this lecture)
    • Need demultiplexing within a host—E.g., which packets are for web server, which for mail server, etc.? (lecture 4) //主机内部分解包
  • Want abstraction of inter-process (not just inter-node) communication //进程间通信 :封装 + IP

    • Solution: Encapsulate another protocol within IP



  • UDP and TCP most popular protocols on IP
    • 都使用 16位端口号 + 32位IP地址
    • Applications bind a port & receive traffic to that port 应用从端口收发消息
  • UDP – unreliable datagram protocol 不可靠报文协议
    • Exposes packet-switched nature of Internet 透明
    • Sent packets may be dropped, reordered, even duplicated (but generally not corrupted) 丢包 无序 重复
  • TCP – transmission control protocol 传输控制协议
    • Provides illusion of a reliable “pipe” between to processes on two different machines (lecture 5) 在两个主机的进程间提供虚拟可靠”管道”
    • Handles congestion & flow control (lecture 6) 处理 阻塞 和 流量控制
    • Most applications use TCP
      • Easier interface to program to (reliability, lecture 5)
      • Automatically avoids congestion (don’t need to worry about taking down network, lecture 6)
    • Servers typically listen on well-known ports
      • SSH: 22
      • Email: 25
      • Finger: 79
      • Web / HTTP: 80

Programming Sockets

  • Berkeley sockets API
    • Bottom-level OS interface to networking
    • Important to know and do once
    • Higher-level APIs build on them

CS110 review



Communication between machines

Socket naming

System calls for using TCP



  1. TCP/IP Ports and Sockets Explained

  2. Hop (networking))

Thanks for Support.