分布式系统(Distributed System) 02:TCP, UDP and Sockets

墨尔本大学 COMP90015 课程笔记

本文已被阅读过

Posted by Yuyao on November 26, 2020

Lecture 02:TCP, UDP and Sockets

1. Introduction

  • Internet and WWW have emerged as global ubiquitous(普及的) media for communication and are changing the way we conduct science, engineering, and commerce.

  • They are also changing the way we learn, live, enjoy, communicate, interact, engage, etc. It appears like the modern life activities are getting completely centered around the Internet.

  • Increasing Demand for Internet Applications

    • To take advantage of opportunities presented by the Internet, businesses are continuously seeking new and innovative ways and means for offering their services via the Internet.
    • This created a huge demand for software designers with skills to create new Internet-enabled applications or migrate existing/legacy(遗留物) applications to the Internet platform.
    • Object-oriented Java technologies—Sockets, threads, RMI, clustering, Web services—have emerged as leading solutions for creating portable(随身携带的), efficient, and maintainable large and complex Internet applications.

2. TCP/UDP and Networking basics

  • Elements of Client-Server Computing/Communication
    • Processes follow protocols that define a set of rules that must be observed by participants:
      • How the data exchange is encoded?
      • How events (sending, receiving) are synchronized (ordered) so that participants can send and receive data in a coordinated(协调一致的) manner(方式)?
    • In face-to-face communication, humans beings follow unspoken protocols based on eye contact, body language, gesture.

  • Networking Basics
    • The Open Systems Interconnection model (OSI model) is a conceptual(概念的) model that characterises and standardises the communication functions of a telecommunication or computing system without regard to its underlying(底层的,基础的) internal structure and technology.
      • 7 Layers in OSI Model

      • In this subject, we pay more attention on 4 layers:

        1. Physical/Link Layer: Functionalities for transmission(传输) of signals representing a stream of data from one computer to another

        2. Internet/Network Layer: IP (Internet Protocols) – a packet of data to be addressed to a remote computer and delivered

        3. Transport Layer
          • Functionalities for delivering data packets to a specific process on a remote computer
          • TCP (Transmission Control Protocol)
          • UDP (User Datagram Protocol)
          • Programming Interface:
          • Sockets
        4. Applications Layer
          • Message exchange between standard or user applications:
          • HTTP, FTP, Telnet, Skype,…

    • TCP (Transmission Control Protocol) is a connection-oriented communication protocol that provides a reliable flow of data between two computers.
      • Analogy(类比): Speaking on Phone
      • Example applications:
        • HTTP, FTP, Telnet
        • Skype uses TCP for call signalling, and both UDP and TCP for transporting media traffic.
      • List the steps involved at the client and at the server to establish a TCP stream socket connection.

      经典的TCP三次握手建立连接

      • Failure Model:
        1. Use checksum to detect and reject corrupt packets
        2. Use sequence numbers to detect and reject duplicates
        3. Timeouts and retransmission is used to deal with lost packet
        4. Under severe congestion, TCP streams declare the connections to be broken
        5. When communication is broken, the processes cannot distinguish between network failure or process crash
        6. Communicating process can’t definitely say whether the messages sent recently were received
    • UDP (User Datagram Protocol) is a connectionless communication protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival or order of arrival
      • Similar to sending multiple emails/letters to friends, each containing part of a message.
      • Example applications:
        • Clock server
        • Ping
        • Live streaming (event/sports broadcasting(广播,转播))
    • TCP VS UDP Communication

    • Three possible failures that can happen when using UDP for cummunication.
      1. Data Corruption(数据损坏): Hello, how are you? — Hello, who like you?
      2. Omission Failures(No guaranteed delivery): Hello, how are you? — Hello, you?
      3. Order: Hello, how are you? — how Hello you, are?
    • TCP can address the issues that not addressed by UDP
      • Connection oriented: The communicating processes establish a connection before communicating. The connection involves a connect request from the client to the server followed by an accept request from the server to the client.

        面向连接:通信过程在通信前建立连接。连接涉及从客户端到服务器的连接请求,然后从服务器到客户端的接受请求。

      • Message sizes: There is no limit on data size applications can use.
      • Lost messages: TCP uses an acknowledgment scheme(方案) unlike UDP. If acknowledgments are not received the messages are retransmitted.
      • Flow control: TCP protocol attempts to match the speed of the process that reads the message and writes to the stream.
      • Message duplication(重复项) or ordering: Message identifiers(消息标识符) are associated with(与…密切相关) IP packets to enable the recipient(接受者) to detect and reject(拒绝) duplicates and reorder messages in case messages arrive out of order.

3. Ports and Sockets

  • Ports
    • The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer.

    • Port is represented by a positive (16-bit) integer value
    • Some ports have been reserved(保留,预留) to support common/well known services:

      • ftp (21/tcp)

        The File Transfer Protocol (FTP) is a standard network protocol used for the transfer of computer files between a client and server on a computer network. FTP is built on a client-server model architecture using separate control and data connections between the client and the server.

      • telnet (23/tcp)

        Telnet is a terminal emulation program(终端仿真程序) for TCP/IP networks such as the Internet. The Telnet program runs on your computer and connects your PC to a server on the network. You can then enter commands through the Telnet program and they will be executed as if you were entering them directly on the server console.

      • smtp (25/tcp)

        SMTP(Simple Mail Transfer Protocol) is part of the application layer of the TCP/IP protocol. Using a process called “store and forward,” SMTP moves your email on and across networks. It works closely with something called the Mail Transfer Agent (MTA) to send your communication to the right computer and email inbox.

      • login (513/tcp)

        • User-level processes/services generally use port number value >= 1024
  • Sockets
    • Sockets provide an interface for programming networks at the transport layer (传输层)
    • Network communication using Sockets is very much similar to performing file I/O
      • In fact, socket handle is treated like file handle.
      • The streams used in file I/O operation are also applicable(适用的) to socket-based I/O
    • Socket-based communication is programming language independent.

      That means, a socket program written in Java language can also communicate to a program written in Java or non-Java socket program

    • Socket Communication 1. A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request.

      1. If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bounds to a different port. It needs a new socket (consequently(因此) a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client.

    • Sockets and Java Socket Classes
      • A socket is an endpoint(端点) of a two-way communication link(双向通讯链接) between two programs running on the network.
      • A socket is bound to a port number so that the TCP layer can identify the application that data destined(注定) to be sent.
      • Java’s .net package provides two classes:
        • Socket – for implementing a client
        • ServerSocket – for implementing a server
      • Implementing a Server

      • Implementing a Client

      • ServerSocket & Exceptions

      • Java API for UDP Programming

      • Multithreaded Server: For Serving Multiple Clients Concurrently

    • Some aspects of the Socket interface:
      • Bound to a local port.
        • Socket address = IP + Port number
      • Sockets are used for communication between two networked processes.
        • sending and receiving data.
      • Each socket is associated with a protocol(UDP or TCP).
      • Acts as programming interface to application code and transport layer
      • Socket handle is mostly like file handle
知识共享许可协议本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。 欢迎转载,并请注明来自:Yuyao 的博客 同时保持文章内容的完整和以上声明信息!