---
title: "网络编程"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/network-programming/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/01-系统基础与网络安全/网络工程/网络编程.md"
content_hash: c4e833aa5c7dfb172eb924e560b04dddf08b6daf22fb20fd5270d82f681c17c7
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 网络编程

> 创建时间：2021/6/6 21:21

  * 网络编程
    * 基础知识
      * Terms
        * Server
        * Host
        * Client
      * The Internet Protocols: TCP, UDP, And IP
        * The application layer & The transport layer
        * The network layer
        * Summary
      * Network Client-Server Applications
      * TCP/IP Sockets
        * Introduction
        * types of sockets in TCP/IP
        * Socket Implementation in .NET
    * 实践应用
      * 经验
    * Ref

## 基础知识

### Terms

The terms server and client each have both a hardware and software aspect.

#### Server

The term **server** is often used to refer both to a piece of computing hardware on which a server application runs and to the server application itself. A good definition for a server then is any computer used to host one or more server applications as its primary job.

#### Host

A server running a server application is also referred to as a host. The term host extends to any computer running any application.

#### Client

The term **client** is also used to describe both hardware and software. Client hardware is any computing device that hosts an application that requires or uses the services of a server application. Client software is any application that requires or uses the services provided by a server application.

### The Internet Protocols: TCP, UDP, And IP

![Alt text](/media/b5cf532fa8372ec408aa.png)

图片来源：C# FOR ARTISTS
The term **packet-switched network** means that data traveling along network pathways is divided into small, routable packages referred to as packets. If a communication link between two points on a network goes down, the packets can be routed through remaining network connections to their intended destination.

The Internet protocols work together as a **layered protocol stack**. The layered protocol stack consists of the application layer, the transport layer, the network layer, the data link layer, and the physical layer. Each layer in the protocol stack provides a set of services to the layer above it.

Putting it all together:

![Alt text](/media/dbacd72e934069b630c9.png)

#### The application layer & The transport layer

The application layer represents any Internet enabled application that requires the services of the transport layer. The purpose of the transport layer is to provide host-to-host, connection-oriented, data transmission service to the application layer.

Two Internet protocols that function at the transport layer include the Transmission Control Protocol(TCP) and the User Datagram Protocol (UDP). TCP guarantees data delivery and saves you the worry. UDP is faster than TCP but unreliable.

#### The network layer

The network layer is responsible for the routing of data traffic between Internet hosts. The Internet Protocol (IP) is a connectionless service that permits the exchange of data between hosts without a prior call setup. (Hence the term connectionless.) It packages data submitted by TCP or UDP into blocks called **datagrams**. IP uses IP addresses and routing tables to properly route datagrams to their intended destination networks.

#### Summary

Computers that participate in a TCP/IP networking environment must be running an instance of the TCP/IP protocol stack. The TCP/IP protocol stack is part of a computer’s operating system.

### Network Client-Server Applications

![Alt text](/media/aa2aabda3fc294408c59.png)

the three components of a typical .NET remoting application:

  * a remotable object

  * a server application that coordinates method calls to the remote object and makes its services available on a particular channel

  * a client application that accesses the services of the remote object via the appropriate channel.

### TCP/IP Sockets

#### Introduction

A **socket** is an abstraction through which an application may **send and receive data** , in much the same way as an open file allows an application to read and write data to stable
storage.
Information written to the socket by an application on one machine can be read by an application on a different machine.

Different types of sockets correspond to different underlying protocol suites and different stacks of protocols within a suite.

#### types of sockets in TCP/IP

The main types of sockets in TCP/IP today are **stream sockets** and **datagram sockets**.

**Stream sockets** use TCP as the end-to-end protocol (with IP underneath) and thus provide a reliable byte-stream service.

**Datagram sockets** use UDP (again, end-to-end with IP underneath) and thus provide a best-effort datagram service that applications can use to send individual messages up to about 65,500 bytes in length.

A TCP/IP **socket** is uniquely identified by an Internet **address** , an end-to-end **protocol** (TCP or UDP), and a port **number**.

![Alt text](/media/4f7a61d42aca67df3bf4.png)

图片来源： _TCP/IP Sockets in C#: Practical Guide for Programmers_

> **A port** identifies an application on a host

#### Socket Implementation in .NET

![Alt text](/media/f51fc6cc96dd5740760f.png)

**abstraction can sometimes hide some of the flexibility and power of a lower level interface.**

In order to allow access to the underlying sockets interface, Microsoft implemented a .NET Socket class, which is a **wrapper** around the WinSock socket functions and has most of the versatility (and complexity) of sockets interface exposed. **Then three higher-level socket classes, TcpClient, TcpListener, and UdpClient, were implemented by using the .NET Socket wrapper class**.

![Alt text](/media/bb1c79bafb698ce90016.png)

## 实践应用

### 经验

TCP是可靠传输协议，建立与TCP进行绑定的socket，调用send函数进行数据发送时，同步调用会阻塞，保证数据相连的一端能收到全部数据，才会执行结束。

## Ref

_C# FOR ARTISTS_
_TCP/IP Sockets in C#: Practical Guide for Programmers_
