嵌入式专业英语一

嵌入式专业英语一

2008年04月18日 星期五 08:44

A ASIC (专用集成电路)

Application-Specific Integrated Circuit. A piece of custom-designed hardware in a chip.

专用集成电路。一个在一个芯片上定制设计的硬件。

address bus (地址总线)

A set of electrical lines connected to the processor and all of the peripherals withwhich itcommunicates. The address bus is used by the processor to select aspecific memory location or register within a particular peripheral. If the address bus contains n electrical lines, the processor can uniquely address up to 2^n such locations.

一个连接处理器与所有外设的,用来通讯的电子线路集。地址总线被处理器用来选择在特定外设中的存储器地址或寄存器。如果地址总线有n 条电子线路,处理器能唯一寻址高达2^n的地址空间。 application software(应用软件)

Describes software modules specific to a particular embedded project. Theapplication software is unlikely to be reusable across embedded platforms, simplybecause each embedded system has a different application.

用来描述一个特定的嵌入式项目中的某一软件模块。应用软件不象可重用的交叉嵌入式平台,只是因为每一个嵌入式系统有不同的应用软件。

assembler (汇编编译器)

A software development tool that translates human-readable assembly languageprograms into machine-language instructions that the processor can understandand execute.

一个能把人可读的汇编语言程序转换到处理器可理解和运行的机器指令的软件开发工具。

assembly language(汇编语言)

A human-readable form of a processor"s instruction set. Most processor-specific functions must be written in assembly language.

一种人可读的处理器指令集的形式。大多数处理器相关的功能必须用汇编语言编写。

B BSP (板卡支持包)

See board support package.

见board support package。

binary semaphore(二元信号)

A type of semaphore with just two states. Also called a mutex. 一种只有两种状态的信号。也叫互斥信号。

board support package(板卡支持包)

Part of a software package that is processor or

platform-dependent. Typically,sample source code for the board support package is provided by the package developer. The sample code must be modified as necessary, compiled, and linked with the remainder of the software package.

软件包的具有平台依赖性的那一部分。典型地,板卡支持包的样例源程序由包开发者提供。样例源程序必须能在需要时被修改、编译并与软件包的剩下的部分连接起来。

bond-out processor (外合处理器)

A special version of a processor that has some of the internal signals brought out to external pins. A bond-out processor is most often found within an emulator and is never intended to be used in a production system.

一种特殊版本的处理器,它有一些,内部的信号能传达到外置的针脚上。一个外合处理器绝大多数情况下只用在模拟器上,从来不会被特意用在产品系统上。

Breakpoint (断点)

A location in a program at which execution is to be stopped and control of the processor switched to the debugger. Mechanisms for creating and removing breakpoints are provided by most debugging tools.

一个在程序中的地址,在那里程序的执行被停止,并且处理器的控制转换到了除错程序。大多数除错工具提供增加与删除一个断点的机制。

C CISC (复杂指令集计算机)

Complex Instruction Set Computer. Describes the architecture of a processor family. CISC processors generally feature variable-length instructions, multiple addressing formats, and contain only a small number of general-purpose registers. Intel"s 80x86 family is the quintessential example of CISC. Contrast with RISC.

复杂指令集计算机。对一种处理器架构的描述。CISC 处理器一般产生变长的指令,多种地址格式,并且仅仅有少量的通用寄存器。Intel 的80x86家族是是典型的CISC 处理器。相对于RISC 而言。 CPU (中央处理器)

Central Processing Unit. The part of a processor that executes

instructions.

中央处理器。处理器中执行指令的那一部分。

Compiler (编译器)

A software development tool that translates high-level

language programs into the machine-language instructions that a particular processor can understand and execute.

把高级编程语言程序转换到只有特定的处理器能了解和执行的机器指令的一种软件开发包。

context (上下文)

The current state of the processor"s registers and flags. 处理器当前的状态和标志。

context switch(上下文切换)

The process of switching from one task to another in a multitasking operating system. A context switch involves saving the context of the running task and restoring the previously-saved context of the other. The piece of code that does this is necessarily processor-specific.

在多任务操作系统中我一个任务切换到另一个的过程。上下文切换包括保存正在运行的任务的上下文和恢复早先保存的另一个任务的上下文。做这个工作的一段代码必须具有处理器特权。

counting semaphore(计数信号)

A type of semaphore that is used to track multiple resources of the same type. An attempt to take a counting semaphore is blocked only if all of the available resources are in use. Contrast with binary semaphore.

一种用来跟踪多个相同类型资源的信号灯。仅仅在所有可用的资源都被用完了时才阻塞。相对二元信号而言。

critical section(临界段)

A block of code that must be executed in sequence and without interruption to guarantee correct operation of the software. See also race condition.

一段必须按次序执行的代码,并且不能被中断,否则不能保证软件正确地操作。参照:竞争状况。

cross-compiler (交叉编译器)

A compiler that runs on a different platform than the one for which it produces object code. A cross-compiler runs on a host computer and produces object code for the target.

一个运行在不同的平台上的编译器,其中之一能产生目标代码。交叉编译器在主机上运行并且产生目标机的目标代码。

D DMA (直接内存访问)

Direct Memory Access. A technique for transferring data

directly between two peripherals (usually memory and an I/O device) with only minimal intervention by the processor. DMA transfers are managed by a third peripheral called a DMA controller.

直接内存访问。一种直接在两个外设(通常是内存和I/O设备)之间进行数据传输的技术,它只要处理器最少的介入。DMA 传输由叫DMA 控制器的第三方外设进行管理。

DRAM (动态随机访问存储器)

Dynamic Random-Access Memory. A type of RAM that maintains its contents only as long as the data stored in the device is refreshed at regular intervals. The refresh cycles are usually performed by a peripheral called a DRAM controller.

动态随机访问存储器。一种RAM ,存储在其设备中的数据被定期刷新时才能保存它的内容。刷新周期一般由一个叫DRAM 控制器的外设完成。

Data bus(数据总线)

A set of electrical lines connected to the processor and all of the peripherals with which it communicates. When the processor wants to read (write) the contents of a memory location or register within a particular peripheral, it sets the address bus pins appropriately and receives (transmits) the

contents on the data bus.

连接处理器与所有外设进行通讯的电子线路集。当一个处理器想去写(读)某一特定外设中的存储器地址或寄存器中的内容时,处理器设置地址总线并在数据总线上接收(传输)内容。

Deadline (死线)

The time at which a particular set of computations must be completed. See also real-time system.

一个特定计算必须被完成的时间。请看实时系统。

Deadlock (死锁)

An unwanted software situation in which an entire set of tasks is blocked, waiting for an event that only a task within the same set can cause. If a deadlock occurs, the only solution is to reset the hardware. However, it is usually possible to prevent deadlocks altogether by following certain software design practices.

一种不希望出现的软件状态,在这个状态下,所有的任务因为等待一个只有在这些被阻塞任务之一才能产生的事件而被阻塞。如果死锁发生,唯一解决的方法是重启动硬件。但是,通过可靠的软件设计实践活动通常可以防止死锁的发生。

debug monitor(除错监视程序)

A piece of embedded software that has been designed

specifically for use as a debugging tool. It usually resides in ROM and communicates with a debugger via a serial port or network connection. The debug monitor provides a set of primitive commands to view and modify memory locations and registers, create and remove breakpoints, and execute your program. The debugger combines these primitives to fulfill higher-level requests like program download and single-step. 嵌入式软件被特殊设计来作为除错工具的一部分。它一般被放在ROM 中,通过串口或网络与除错器进行通讯。除错监视程序提供一个简单的命令集来显示和内存地址和寄存器、建立和移除断点,并且运行你的程序。除错监视器组合这些简单的命令去实现象程序下载各单步调试等高端的请求。

Debugger (除错器)

A software development tool used to test and debug embedded software. The debugger runs on a host computer and connects to the target through a serial port or network connection. Using a debugger you can download software to the target for immediate execution. You can also set breakpoints and examine the contents of specific memory locations and registers.

一个软件开发工具,被用来对嵌入式软件进行测试和除错。除错器在宿主机上运行并且通过串口或网络连接到目标机上。你能使用除错器

下载软件到目标机并直接运行。你也可以设置断点并检查特定内存地址或寄存器的内容。

device driver(设备驱动程序)

A software module that hides the details of a particular peripheral and provides a high-level programming interface to it.

一个软件模块,它隐藏特定外设的细节并提供高级的外设编程接口。 device programmer(设备编程器)

A tool for programming non-volatile memories and other electrically-programmable devices. Typically, the

programmable device is inserted into a socket on the device programmer and the contents of a memory buffer are then transferred into it.

一种用来对不挥发内存和其他电可编程设备进行编程的工具。典型地,可编程设备被插到设备编程器的接口上,接着内存缓存器中的内容被传送到它里面。

digital signal processor(数字信号处理器)

A device that is similar to a microprocessor, except that the internal CPU has been optimized for use in applications involving discrete-time signal processing. In addition to standard microprocessor instructions, DSPs usually support a

set of complex instructions to perform common

signal-processing computations quickly.Common DSP families are TI"s 320Cxx and Motorola"s 5600x series.

一种类似于微处理器的的设备,不同的是它内部的CPU 被优化,用于特定的应用,如离散信号处理。除了标准的微处理器指令外,DSP 常常支持复杂指令集去非常快地完成通用的信号处理计算。通用DSP 家庭是TI 的320Cxx 和Motorola 的5600x 系列。

E EEPROM (电可擦的,可编程的只读存储器)

Electrically Erasable, Programmable Read-Only Memory.

(Pronounced"Double-E"-PROM.) A type of ROM that can be erased electronically.

电可擦的,可编程的只读存储器。一种ROM 能被电擦除。

EPROM (可擦的,可编程的只读存储器)

Erasable, Programmable Read-Only Memory. A type of ROM that can be erased by exposing it to ultraviolet light. Once erased, an EPROM can be reprogrammed with the help of a device programmer. 一种可用紫外线擦除的存储器。一次擦除后,EPROM 可以在设备编程器的帮助下被重编程。

embedded system(嵌入式系统)

A combination of computer hardware and software, and perhaps additional mechanical or other parts, designed to perform a

dedicated function. In some cases, embedded systems are part of a larger system or product, as is the case of an anti-lock braking system in a car. Contrast with general-purpose computer.

计算机硬件和软件的结合体,或许还加上机械等其他部分,被设计来完成专门的功能。在一些情况下,嵌入式系统是一个大的系统或产品的一部分,就象汽车上的防抱死装置。与通用计算机相对。 Emulator(仿真器)

Short for In-Circuit Emulator (ICE). A debugging tool that takes the placeof-emulates-the processor on your target board. Emulators frequently incorporate a special "bond-out" version of the target processor that allows you to observe and record its internal state as your program is executing . 在线仿真器的简写。一个在你的目标板上放置仿真的处理器的调试工具。仿真器经常和一目标处理器的一种“外合”版本合在一起,这个版本的的处理器充许你运行程序时观察和记录它的内部状态。

Executable (可执行的)

A file containing object code that is ready for execution on the target. All that remains is to place the object code into a ROM or download it via a debugging tool.

一个包含准备在目标机上运行的目标代码的文件。放置目标代码到ROM 中或通过调试工具下载。

F Firmware (固件)

Embedded software that is stored as object code within a ROM. This name is most common among the users of digital signal processors.

是作为目标代码存贮在ROM 中的嵌入式软件。这个名字在数字信号处理器的用户中相当流行。

flash memory (闪存)

A RAM-ROM hybrid that can be erased and rewritten under software control. Such devices are divided into blocks, called sectors, that are individually-erasable. Flash memory is common in systems that require nonvolatile data storage at very low cost. In some cases, a large fash memory may even be used instead of a disk-drive.

一种RAM-ROM 的混血儿,它能在软件的控制下被擦除和重写。一些设备被分成叫段组的块,能个别地可擦。闪存用在需要很便宜的非易失数据存贮器的地方,一个大容量的闪存甚至被用作磁盘驱动器。 G general-purpose computer(通用计算机)

A combination of computer hardware and software that serves as a

general-purpose computing platform. For example, a personal computer. Contrast with embedded system.

当作通用计算平台的计算机硬件与软件的组合。例如,PC 。相对于嵌入式计算机。

H HLL

See high-level language.

查阅高级语言。

Heap (堆)

An area of memory that is used for dynamic memory allocation. Calls to malloc and free and the C++ operators new and delete result in run-time manipulation of the heap.

一块被用作动态内存分配的内存区域。调用malloc 和free 、C++的操作符new 、delete 在运行时进行堆的操作。

high-level language(高级语言)

A language, such as C or C++, that is processor-independent. When programming in a high-level language, it is possible to concentrate on algorithms and applications without worrying about the details of a particular processor.

一种语言,象C 或C++,是处理器独立的。当在高级语言上编程时,不需要考虑特定处理器的细节,只用关心算法和应用。

Host (主机)

A general-purpose computer that communicates with the target via a serial port or network connection. This term is usually used to distinguish the computer on which the debugger is running from the embedded system that is being developed. 一台通用计算机,它通过串口或网络连接与目标机通讯。这处名词一般用来区别调试程序运行的计算机和被开发的嵌入式系统。 I

ICE

In-Circuit Emulator. See emulator.

在线仿真器。查阅仿真器。

I/O(输入/输出)

Input/Output. The interface between a processor and the world around it. The simplest examples are switches (inputs) and LEDs (outputs).

输入/输出。处理器与外界的交互界面。最简单的例子是开关(输入)和发光二级管(输出)。

I/O device(IO 设备)

A piece of hardware that interfaces between the processor and the outside world. Common examples are switches and LEDs, serial ports, and network controllers.

一种介于处理器和外界之间的硬件设备。一般的实例是开关、LED 、串口和网络控制器。

I/O map(I/O映射)

A table or diagram containing the name and address range of each peripheral addressable by the processor within the I/O space. I/O maps are a helpful aid in getting to know the target. 一张包含每个外设的名字和地址的表格或图表,可由处理器在I/O空间中设定地址。I/O映射对得知目标机情况非常有利。

I/O space(I/O空间)

A special memory region provided by some processors and

generally reserved for the attachment of I/O devices. Memory locations and registers within an I/O space can be accessed only via special instructions. For example, processors in the 80x86 family have special I/O space instructions called in and out. Contrast with memory space.

一个由处理器提供的特殊内存区域,一般为为I/O设备的附件保留。在I/O空间的内存位置和寄存器只能通过特殊的指定进行访问。例如:80X86家族的处理器有叫做in / out的特殊的I/O空间指令。相对内存空间而言。

ISR (中断服务程序)

See interrupt service routine.

instruction pointer(指令指针)

A register in the processor that contains the address of the next instruction to be executed. Also known as a program counter. 包含下一条要执行指令地址的处理器中的寄存器。也叫程序计数器。

Interrupt (中断)

An asynchronous electrical signal from a peripheral to the processor. When the peripheral asserts this signal, we say that an interrupt occurs. When an interrupt occurs, the current state of the processor is saved and an interrupt service routine is executed. When the interrupt service routine exits, control of the processor is returned to whatever part of the software was previously running.

一个从外设到处理器的异步电信号。当外设发出这个信号,我们说一个中断发生。当一个中断发生,当前的处理器状态被保存并且中断服务程序开始运行。当中断服务程序退出,对处理器的控制权转到先前运行的那个软件上。

interrupt latency(中断延迟)

The amount of time between the assertion of an interrupt and the start of the associated interrupt service routine. 在中断发生和相关的中断服务程序运行之间的时间长短。

interrupt service routine(中断服务程序)

A piece of software executed in response to a particular interrupt.

响应特定中断而运行的一小段软件。

interrupt type(中断类型)

A unique number associated with each interrupt.

和每一个中断相关联的唯一数字。

interrupt vector (中断向量)

The address of an interrupt service routine.

中断服务程序所在的地址。

interrupt vector table(中断向量表)

A table containing interrupt vectors and indexed by interrupt type. This table contains the processor"s mapping between interrupts and interrupt service routines and must be initialized by the programmer.

一个表格,包含由中断类型决定的中断向量和索引。这个表格包含中断与中断服务程序之间的处理器的映射,必须由程序员进行初始化。 intertask communication(进程间通讯)

A mechanism used by tasks and interrupt service routines to share information and synchronize their access to shared

resources. The most common building blocks of intertask communication are semaphores and mutexes.

一种被用来在任务和中断服务程序之间共享信息和同步它们对共享资源访问的机制。大部分进程间通讯的建立的基石是信号灯和互斥。 K Kernel (内核)

An essential part of any multitasking operating system, the kernel contains just the scheduler and context-switch routine. 任何多任务操作系统的本质部分,内核仅仅包含调度程序和上下文切换进程。

L Linker (连接程序)

A software development tool that accepts one or more object files as input and outputs a relocatable program. The linker is thus run after all of the source files have been compiled or assembled.

一种能把一个或更多目标文件组合成可输入和输出的可重定位程序的开发工具。连接程序在所有的源文件都被编译或汇编之后运行。 Locator (定位程序)

A software development tool that assigns physical addresses to the relocatable program produced by the linker. This is the last step in the preparation of software for execution by an embedded system and the resulting file is called an executable. In some

cases, the locator"s function may be hidden within the linker. 一种分配物理地址给连接程序处理过的可重定位程序的软件开发工具。这是准备一个软件在嵌入式系统中运行的最后一步,并且结果文件叫做可执行的。在一些情况下,定位程序功能隐藏在连接程序中。 logic analyzer(逻辑分析仪)

A hardware debugging tool that can be used to capture the logic levels (0 or 1) of dozens, or even hundreds, of electrical signals in real-time. Logic analyzers can be quite helpful for debugging hardware problems and complex processor-peripheral interactions.

一种硬件调试工具,能捕获实时电信号的许多逻辑电平(0或1),逻辑分析仪在调试硬件问题和复杂的处理外设交互时相当有用。

TCP/IP 应用程序的通信连接模式

2011年05月06日 星期五 17:42

TCP/IP 应用层与应用程序

TCP/IP 起源于二十世纪 60 年代末美国政府资助的一个分组交换网络研究项目,它是一个真正的开放协议,很多不同厂家生产各种型号的计算机,它们运行完全不同的操作系统,但 TCP/IP 协议组件允许它们互相进行通信。现在 TCP/IP 已经从一个只供一些科学家使用的小实验网成长为一个由成千上万的计算机和用户构成的全球化网络,TCP/IP 也已成为全球因特网(Internet )的基础,越来越多的 TCP/IP 互联网应用和企业商业应用正在改变着世界。

TCP/IP 通讯协议采用了四层的层级模型结构(注:这与 OSI 七层模型不相同),每一层都调用它的下一层所提供的网络任务来完成自己的需求。TCP/IP 的每一层都是由一系列协议来定义的。这 4 层分别为:

应用层 (Application):应用层是个很广泛的概念,有一些基本相同的系统级 TCP/IP 应用以及应用协议,也有许多的企业商业应用和互联网应用。

传输层 (Transport):传输层包括 UDP 和 TCP,UDP 几乎不对报文进行检查,而 TCP 提供传输保证。

网络层 (Network):网络层协议由一系列协议组成,包括 ICMP、IGMP 、RIP 、OSPF 、IP(v4,v6) 等。

链路层 (Link):又称为物理数据网络接口层,负责报文传输。 图1显示了 TCP/IP 层级模型结构,应用层之间的协议通过逐级调用传输层(Transport layer )、网络层(Network Layer )和物理数据链路层(Physical Data Link )而可以实现应用层的应用程序通信互联。

应用层需要关心应用程序的逻辑细节,而不是数据在网络中的传输活动。应用层其下三层则处理真正的通信细节。在 Internet 整个发展过程中的所有思想和着重点都以一种称为 RFC(Request For Comments )的文档格式存在。针对每一种特定的 TCP/IP 应用,有相应的 RFC 文档。一些典型的 TCP/IP 应用有 FTP、Telnet 、SMTP 、SNTP 、REXEC 、TFTP 、LPD 、SNMP 、NFS 、INETD 等。RFC 使一些基本相同的 TCP/IP 应用程序实现了标准化,从而使得不同厂家开发的应用程序可以互相通信。

图 1 TCP/IP 层级模型结构

然而除了这些已经实现标准化的系统级 TCP/IP 应用程序外,在企业商业应用和互联网应用开发中,存在着大量的商业应用程序通信互联问题。如图 1 显示,其中的应用层所包含应用程序主要可以分成两类,即系统级应用和商业应用,互联网商业应用是商业应用中的主要形式之一。

不同开发商和用户在开发各自商业应用通信程序时也存在有许多不同的设计方式。关于 TCP/IP 应用层以下的技术文献与书籍早已是汗牛充栋,但是关于 TCP/IP 应用本身,尤其是关于商业应用的通信设计模式技术讨论方面的文章还是比较少的。TCP/IP 应用通信设计模式实际上是在 TCP/IP 基础编程之上的一种应用编程设计方式,

也属于一种应用层协议范畴,其可以包含有 TCP/IP 地址族模式设计、I/O 模式设计、通信连接模式设计以及通信数据格式设计等。鉴于目前讨论 TCP/IP 商业应用程序设计模式问题这方面的文章还很少见,本文尝试给出一些通信连接模式设计中共同的概念与一些典型的设计模式,在以后的文章中将继续讨论地址族模式设计、I/O 模式设计、以及通信数据格式设计等方面的模式设计实现话题。 通信连接模式设计主要考虑内容有:

通信两端程序建立通信方式

通信连接方式

通信报文发送与接收方式

以下内容将介绍建立通信的 Client/Server 模型,然后逐一介绍通信连接模式设计所需要考虑的这些内容。

传输层接口 APIs 与 TCP/IP 应用程序 C/S 模型

传输层接口 APIs

TCP/IP 应用层位于传输层之上,TCP/IP 应用程序需要调用传输层的接口才能实现应用程序之间通信。目前使用最广泛的传输层的应用编程接口是套接字接口(Socket )。Socket APIs 是于 1983 年在 Berkeley Socket Distribution (BSD) Unix 中引进的。 1986 年

AT&T 公司引进了另一种不同的网络层编程接口 TLI(Transport Layer Interface ),1988 年 AT&T 发布了一种修改版的 TLI,叫做 XTI (X/open Transport interface)。XTI/TLI 和 Socket 是用来处理相同任务的不同方法。关于 TCP/IP APIs 使用文章与书籍已相当多,本文则是侧重于如何组合使用这些 APIs 来进行 TCP/IP 应用程序连接模式设计,并归纳出几种基本应用连接模式。

如图 2 显示,应用层是通过调用传输层接口 APIs(Socket 或 XTI/TLI)来与传输层和网络层进行通信的。

图 2 传输层接口

不管是使用何种编程接口,要在两个机器或两个程序之间建立通信,通信双方必须建立互相一致的通信模式。如果双方的通信设计模式不一致就无法建立有效的通信连接。

以下是经常使用的 socket APIs ,是建立 TCP/IP 应用程序的标准接口,也是影响 TCP/IP 应用程序通信方式的几个主要 APIs,不同 APIs 组合再结合系统调用可以实现不同方式的应用。Sockets 支持多种传输层和网络层协议,支持面向连接和无连接的数据传输,允许应用分布式工作。

socket():是用来创建一个 socket,socket 表示通信中的一个节点,其可以在一个网络中被命名,用 socket 描述符表示,socket 描述符类似于 Unix 中的文件描述符。

bind():是用来把本地 IP 层地址和 TCP 层端口赋予 socket。 listen() :把未连接的 socket 转化成一个等待可连接的 socket ,允许该 socket 可以被请求连接,并指定该 socket 允许的最大连接数。

accept():是等待一个连接的进入,连接成功后,产生一个新的 socket 描述符,这个新的描述符用来建立与客户端的连接。 connect():用来建立一个与服务端的连接。

send():发送一个数据缓冲区,类似 Unix 的文件函数 write()。

另外 sendto() 是用在无连接的 UDP 程序中,用来发送自带寻址信息的数据包。

recv():接收一个数据缓冲区,类似 Unix 的文件函数 readI()。另外 recvfrom() 是用在无连接的 UDP 程序中,用来接收自带寻址信息的数据包。

close():关闭一个连接

Client/Server 模型

Sockets 是以 Client 和 Server 交互通信方式来使用的。典型的系统配置是把 Server 放在一台机器中,而把 Client 放在另一台机器中,Client 连接到 Server 交换信息。一个 socket 有一系列典型的事件流。例如,在面向连接的 Client/Server 模型中,Server 端的 socket 总是等待一个 Client 端的请求。要实现这个请求,Server 端首先需要建立能够被 Client 使用的地址,当地址建立后,Server 等待 Client 请求服务。当一个 Client 通过 socket 连接到 Server 后,Client 与 Server 之间就可以进行信息交换。

Client/Server 是通信程序设计的基本模式。从软件开发的角度讲,TCP/IP 应用程序都是基于 Client/Server 方式的。注意本篇文章以下 Client/Server 概念是针对程序内部调用 Socket API 所讲的概

念,与针对整个程序甚至针对机器而讲的客户端 / 服务器概念有所不同。用 Server APIs 建立的程序可以被当作客户端使用,用 Client APIs 建立的程序也可以被用作服务器端使用。建立 Server 需要的 APIs 有 socket(), bind(), listen(), accept(),建立 Client 需要的 APIs 有 Socket(), Connect()。在实际应用开发中,同一个程序里往往同时可以有 Client 和 Server 的代码,或者多种形式的组合。在实际应用编程中,针对 Socket APIs 不同有效组合,结合系统调用可以有多种复杂的设计变化。

面向连接的应用编程存在三类基本的不同级别的设计方式范畴,根据 Socket APIs 从上到下顺序依次是:

Client/Server 通信建立方式

Client/Server 通信连接方式

Client/Server 通信发送与接收方式

下面内容以面向连接的 Socket 应用编程为例来说明这几种不同通信范畴的设计实现。

Client/Server 建立方式设计概述

一个 Client 连接一个 Server

如果只有两台机器之间连接,那么一个是 Client,另一个是 Server ,如下面图 3 所示。这是最简单的 TCP/IP 的应用,也是 TCP/IP 应用早期的 Peer to Peer (P2P) 概念。其流程基本如图 4 所示。

图 3 TCP/IP 应用单点 Client/Server

图 4 显示了 TCP/IP 应用编程最基本的 Client/Server 模式,显示了基本的 Client/Server 通信所需要调用的 Socket APIs 以及顺序。

图 4 TCP/IP 应用编程基本 Client/Server 模式

多个 Client 连接一个 Server

多个 Client 同时连接一个 Server 是 TCP/IP 应用的主流形式,如图 5 所示,其中 Client 连接数可以从几个到成千上万。 图 5 TCP/IP 应用多 Client 端的 Client/Server

由于 socket APIs 缺省方式下都是阻塞方式的,实现多个 Client 同时连接一个 Server 就需要特别的设计。其实现方式可以有多种不同的设计,这其中也涉及 I/O 模式设计。下面将展开介绍其中几种设计形式。

利用一个 Client 连接一个 Server 形式实现多 Client 连接 从程序设计角度讲,只要 Client 和 Server 端口是一对一形式,那么就属于一个 Client 连接一个 Server 形式。在处理多个 Client 端连接时,Server 端轮流使用多个端口建立多个

Client-Server 连接,连接关闭后,被释放端口可以被循环使用。在这种多连接形式中需要谨慎处理 Client 端如何获取使用 Server 端的可用端口。比如图 6 显示 Server 有一个服务于所有进程的进

程可以先把 Server 端的可用端口发送给 Client 端,Client 端再使用该端口建立连接来处理业务。Server 针对每一个 Client 连接用一个专门的进程来处理。由于可用端口数有限,Server 用一个有限循环来处理每一个可用的端口连接。由于新端口需要用 bind() 来绑定,所以需要从 bind() 开始到 close() 结束都需要包含在循环体内。

图 6 利用一对一 Client-Server 模式实现多 Client 连接

使用多个 accept() 实现多 Client 连接

多进程 Server 一般有一个专注进程是服务于每一个连接的。当 Client 端完成连接后,专注进程可以循环被另外的连接使用。使用多个 accept() 也可以实现处理多 Client 连接。多 accept() 的 Server 也只有一个 socket(),一个 bind(),一个 listen(),这与通常情况一样。但是它建立许多工作子进程,每一个工作子进程都有 accept(),这样可以为每一个 Client 建立 socket 描述符。如图 7 所示,由于 accept() 连接成功后,会产生一个新的 socket 描述符,

这样通过循环多进程利用 accept() 产生的多 socket 描述符就可以与多个 Client 进行连接通信。循环体是从 accept() 开始到 close() 结束的。

图 7 使用多 accept() 实现多 Client 连接

使用并发 Server 模式实现多 Client 连接

并发服务器模式曾经是 TCP/IP 的主流应用程序设计模式,得到广泛使用,目前互联网上仍有相当多的应用使用此种模式。其设计思

路是在 accept 之后 fork 出一个子进程。因为 socket 会产生监听 socket 描述符 listenfd,accept 会产生连接 socket 描述符 connfd 。连接建立后,子进程继承连接描述符服务于 Client,父进程则继续使用监听描述符等待另外一个 Client 的连接请求,以产生另外一个连接 socket 描述符和子进程。如图 8 所示,accept() 接收到一个 Client 连接后,产生一个新的 socket 描述符,通过 fork() 系统调用,用一个子进程来处理该 socket 描述符的连接服务。而父进程可以立即返回到 accept(),等待一个新的 Client 请求,这就是典型的并发服务器模式。并发服务器模式同时处理的最大并发 Client 连接数由 listen() 的第二个参数来指定。

图 8 TCP/IP 应用并发 Server

使用 I/O 多路技术实现多 Client 连接

以上三种连接设计,多 Server 端口、多 accept() 和并发服务器模式,都是通过 fork() 系统调用产生多进程来实现多 Client 连接的。使用 I/O 多路技术也可以同时处理多个输入与输出问题,即用一个进程同时处理多个文件描述符。I/O 多路技术是通过

select() 或 poll() 系统调用实现的。poll() 与 select() 功能完全相同,但是 poll() 可以更少使用内存资源以及有更少的错误发生。select() 调用需要与操作文件描述符集的 APIs 配合使用。select() 系统调用可以使一个进程检测多个等待的 I/O 是否准备好,当没有设备准备好时,select() 处于阻塞状态中,其中任一设

备准备好后,select() 函数返回调用。select() API 本身也有一个超时时间参数,超时时间到后,无论是否有设备准备好,都返回调用。其流程如图 9 所示。在 socket APIs listen() 和 accept() 之间插入 select() 调用。使用这三个宏 FD_ZERO()、FD_CLR() 和

FD_SET(),在调用 select() 前设置 socket 描述符屏蔽位,在调用 select() 后使用 FD_ISSET 来检测 socket 描述符集中对应于 socket 描述符的位是否被设置。 FD_ISSET() 就相当通知了一个 socket 描述符是否可以被使用,如果该 socket 描述符可用,则可对该 socket 描述符进行读写通信操作。通常,操作系统通过宏 FD_SETSIZE 来声明在一个进程中 select() 所能操作的文件或 socket 描述符的最大数目。更详细的 I/O 多路技术实现,可以参考其他相关文献。

图 9 I/O 多路技术实现多连接的 Server

一个 Client 连接多个 Server

一个 Client 连接多个 Server 这种方式很少见,主要用于一个客户需要向多个服务器发送请求情况,比如一个 Client 端扫描连接多个 Server 端情况。如图 10 所示。此种方式设计主要是 Client 端应用程序的逻辑设计,通常需要在 Client 端设计逻辑循环来连接多个 Server,在此不做更多描述。

图 10 单 Client 对多 Server

复杂 Client/Server 设计与现代 P2P

最近几年,对等网络技术 ( Peer-to-Peer,简称 P2P) 迅速成为计算机界关注的热门话题之一,以及影响 Internet 未来的科技之

一。与早期点对点 (Peer to Peer) 的 Client/Server 模式不同,现在的 P2P 模式是指每个结点既可充当服务器,为其他结点提供服务,同时也可作为客户端享用其他结点提供的服务。实际上 P2P 模式仍然是基于 Client/Server 模式的,每个通信节点都既是 Server ,又是 Client,P2P 是基于复杂 Client/Server 设计的 TCP/IP 应用。图 11 显示 P2P 模式下两个用户 PC 之间的对等连接。

图 11 P2P 模式

在技术上,P2P 本身是基于 TCP/IP Client/Server 技术的一种设计模式思想, P2P 也属于网络应用层技术,与 Web 和 FTP 等应用是并列的。只是 P2P 应用在设计实现上更要复杂的多。P2P 技术实现的协同工作是无需专门的服务器支持的 (Serverless),这里的服务器概念与 Client/Server 中的 Server 概念是不一样的。在传统意义上中心服务器机器上往往运行的是 TCP/IP 应用的 Server 端程序,所以传统意义上的 Server 概念在机器与应用上是重合的。如果更改 TCP/IP 的应用设计,使应用程序既可做 Server 又可做 Client ,就可以实现无中心服务器的 P2P 模式。

在设计模式上,P2P 模式实现了网络终端用户不依赖中心服务器或者服务商而直接进行信息和数据交换的可能,因此 P2P 正在改变着整个互联网的一些基础应用,从而极大地增加了用户之间的信息沟通和交流能力。目前互联网的 P2P 应用与网络都正在飞速发展,一些典型的 P2P 应用程序比如有 BitTorrent, eDonkey 等,另外一些即时通信(IM )类软件比如 MSN、QQ 等也正在向无中心服务器模式转变。

无中心服务器的 Internet 应用程序大大降低应用提供商的运营成本,而且减少人们对于 Server 稳定性的依赖。

Client/Server 通信连接方式设计

Client/Server 通信方式建立后,下一步就需要考虑通信连接的方式,主要有两种方式的连接,即长连接通信与短连接通信。通信连接方式涉及到的 APIs 主要是 connect() 和 accept()。要实现某种 Client/Server 方式,就必须考虑用某种特定的连接方式。 短连接通信

短连接通信是指 Client 方与 Server 方每进行一次通信报文收发交易时才进行通讯连接,交易完毕后立即断开连接。此种方式常用于多个 Client 连接一个 Server 情况,常用于机构与用户之间通信,比如 OLTP(联机事务处理)类应用。在短连接情况下,Client 端完成任务后,就关闭连接并退出。在 Server 端,可以通过循环 accept(),使 Server 不会退出,并连续处理 Client 的请求。图 12 显示了一般情况下短连接通信模式的 Socket 事件流,不同设计的连接多 Client 的 Server 有不同的循环流程。

图 12 短连接模式通信

长连接通信

长连接通信是指 Client 方与 Server 方先建立通讯连接,连接建立后不会断开,然后再进行报文发送和接收,报文发送与接收完毕后,原来连接不会断开而继续存在,因此可以连续进行交易报文的发送与接收。这种方式下由于通讯连接一直存在,其 TCP/IP 状态是

Established ,可以用操作系统的命令 netstat 查看连接是否建立。由于在长连接情况下,Client 端和 Server 端一样可以固定使用一个端口,所以长连接下的 Client 也需要使用 bind() 来绑定 Client 的端口。在长连接方式下,需要循环读写通信数据。为了区

分每一次交易的通信数据,每一次交易数据常常需要在数据头部指定该次交易的长度,接收 API 需要首先读出该长度,然后再按该长度读出指定长度的字节。长连接方式常用于一个 Client 端对一个 Server 端的通讯,一般常用于机构与机构之间的商业应用通信,以处理机构之间连续的大量的信息数据交换。或者说可用于两个系统之间持续的信息交流情况。通常为了加快两个系统之间的信息交流,通常还需要建立几条长连接的并行通信线路。图 13 显示了一般情况下长连接通信模式的 socket 事件流,可见其最大特点是 Client 和 Server 都有循环体,而且循环体只包含读写 APIs。

图 13 长连接模式通信

Client/Server 通信发送与接收方式设计

在通信数据发送与接收之间也存在不同的方式,即同步和异步两种方式。这里的同步和异步与 I/O 层次的同异步概念不同。主要涉及 socket APIs recv() 和 send() 的不同组合方式。

同步发送与接收

从应用程序设计的角度讲,报文发送和接收是同步进行的,既报文发送后,发送方等待接收方返回消息报文。同步方式一般需要考虑超时问题,即报文发出去后发送方不能无限等待,需要设定超时时间,超过该时间后发送方不再处于等待状态中,而直接被通知超时返回。同步发送与接收经常与短连接通信方式结合使用,称为同步短连接通信方式,其 socket 事件流程可如上面的图 12 所示。

异步发送与接收

从应用程序设计的角度讲,发送方只管发送数据,不需要等待接收任何返回数据,而接收方只管接收数据,这就是应用层的异步发送与接收方式。要实现异步方式,通常情况下报文发送和接收是用两个不同的进程来分别处理的,即发送与接收是分开的,相互独立的,互不影响。异步发送与接收经常与长连接通信方式结合使用,称为异步长连接通信方式。从应用逻辑角度讲,这种方式又可分双工和单工两

种情况。

异步双工

异步双工是指应用通信的接收和发送在同一个程序中,而有两个不同的子进程分别负责发送和接收,异步双工模式是比较复杂的一种通信方式,有时候经常会出现在不同机构之间的两套系统之间的通信。比如银行与银行之间的信息交流。它也可以适用在现代 P2P 程序中。如图 14 所示,Server 和 Client 端分别 fork 出两个子进程,形成两对子进程之间的连接,两个连接都是单向的,一个连接是用于发送,另一个连接用于接收,这样方式的连接就被称为异步双工方式连接。

图 14 长连接异步双工模式

异步单工

应用通信的接收和发送是用两个不同的程序来完成,这种异步是利用两对不同程序依靠应用逻辑来实现的。图 15 显示了长连接方式下的异步单工模式,在通信的 A 和 B 端,分别有两套 Server 和 Client 程序,B 端的 Client 连接 A 端的 Server,A 端的 Server 只负责接收 B 端 Client 发送的报文。A 端的 Client 连接 B 端的 Server ,A 端 Client 只负责向 B 端 Server 发送报文。

图 15 长连接异步单工模式

典型通信连接模式

综上所述,在实际 TCP/IP 应用程序设计中,就连接模式而言,

我们需要考虑 Client/Server 建立方式、Client/Server 连接方式、Client/Server 发送与接收方式这三个不同级别的设计方式。实际 TCP/IP 应用程序连接模式可以是以上三类不同级别 Client/Server 方式的组合。比如一般 TCP/IP 相关书籍上提供的 TCP/IP 范例程序大都是同步短连接的 Client/Server 程序。有的组合是基本没有实用价值的,比较常用的有价值的组合是以下几种:

同步短连接 Server/Client

同步长连接 Server/Client

异步短连接 Server/Client

异步长连接双工 Server/Client

异步长连接单工 Server/Client

其中异步长连接双工是较为复杂的一种通信方式,有时候经常会出现在不同银行或不同城市之间的两套系统之间的通信,比如国家金卡工程。由于这几种通信方式比较固定,所以可以预先编制这几种通信方式的模板程序。

总结

本文探讨了 TCP/IP 应用程序中连接模式的设计。在以后的文章中还将继续讨论 TCP/IP 应用程序设计中的其他方面的设计话题,包

括地址族模式设计、I/O 模式设计、以及通信数据格式设计等。 + plus 加号;正号

- minus 减号;负号

± plus or minus 正负号

× is multiplied by 乘号

÷ is divided by 除号

= is equal to 等于号

≠ is not equal to 不等于号

≡ is equivalent to 全等于号

≌ is equal to or approximately equal to 等于或约等于号

≈ is approximately equal to 约等于号

< is less than 小于号

> is greater than 大于号

≮ is not less than 不小于号

≯ is not more than 不大于号

≤ is less than or equal to 小于或等于号

≥ is more than or equal to 大于或等于号

% per cent 百分之…

‰ per mill 千分之…

∞ infinity 无限大号

∝ varies as 与…成比例

√ (square) root 平方根

∵ since; because 因为

∴ hence 所以

∷ equals, as (proportion) 等于,成比例

∠ angle 角

≲ semicircle 半圆

≰ circle 圆

○ circumference 圆周

π pi 圆周率

△ triangle 三角形

≱ perpendicular to 垂直于

∪ union of 并,合集

∩ intersection of 交,通集

∫ the integral of …的积分

∑ (sigma) summation of 总和

° degree 度

′ minute 分

″ second 秒

℃ Celsius system 摄氏度

{ open brace, open curly 左花括号

} close brace, close curly 右花括号

( open parenthesis, open paren 左圆括号

) close parenthesis, close paren 右圆括号

() brakets/ parentheses 括号

[ open bracket 左方括号

] close bracket 右方括号

[] square brackets 方括号

. period, dot 句号,点

| vertical bar, vertical virgule 竖线

& ampersand, and, reference, ref 和,引用

* asterisk, multiply, star, pointer 星号,乘号,星,指针

/ slash, divide, oblique 斜线,斜杠,除号

// slash-slash, comment 双斜线,注释符

# pound 井号

/ backslash, sometimes escape 反斜线转义符,有时表示转义符或续行符 ~ tilde 波浪符

. full stop 句号

, comma 逗号

: colon 冒号

; semicolon 分号

? question mark 问号

! exclamation mark (英式英语) exclamation point (美式英语)

' apostrophe 撇号

- hyphen 连字号

-- dash 破折号

... dots/ ellipsis 省略号

" single quotation marks 单引号

"" double quotation marks 双引号

‖ parallel 双线号

& ampersand = and

~ swung dash 代字号

§ section; division 分节号

→ arrow 箭号;参见号

嵌入式专业英语一

2008年04月18日 星期五 08:44

A ASIC (专用集成电路)

Application-Specific Integrated Circuit. A piece of custom-designed hardware in a chip.

专用集成电路。一个在一个芯片上定制设计的硬件。

address bus (地址总线)

A set of electrical lines connected to the processor and all of the peripherals withwhich itcommunicates. The address bus is used by the processor to select aspecific memory location or register within a particular peripheral. If the address bus contains n electrical lines, the processor can uniquely address up to 2^n such locations.

一个连接处理器与所有外设的,用来通讯的电子线路集。地址总线被处理器用来选择在特定外设中的存储器地址或寄存器。如果地址总线有n 条电子线路,处理器能唯一寻址高达2^n的地址空间。 application software(应用软件)

Describes software modules specific to a particular embedded project. Theapplication software is unlikely to be reusable across embedded platforms, simplybecause each embedded system has a different application.

用来描述一个特定的嵌入式项目中的某一软件模块。应用软件不象可重用的交叉嵌入式平台,只是因为每一个嵌入式系统有不同的应用软件。

assembler (汇编编译器)

A software development tool that translates human-readable assembly languageprograms into machine-language instructions that the processor can understandand execute.

一个能把人可读的汇编语言程序转换到处理器可理解和运行的机器指令的软件开发工具。

assembly language(汇编语言)

A human-readable form of a processor"s instruction set. Most processor-specific functions must be written in assembly language.

一种人可读的处理器指令集的形式。大多数处理器相关的功能必须用汇编语言编写。

B BSP (板卡支持包)

See board support package.

见board support package。

binary semaphore(二元信号)

A type of semaphore with just two states. Also called a mutex. 一种只有两种状态的信号。也叫互斥信号。

board support package(板卡支持包)

Part of a software package that is processor or

platform-dependent. Typically,sample source code for the board support package is provided by the package developer. The sample code must be modified as necessary, compiled, and linked with the remainder of the software package.

软件包的具有平台依赖性的那一部分。典型地,板卡支持包的样例源程序由包开发者提供。样例源程序必须能在需要时被修改、编译并与软件包的剩下的部分连接起来。

bond-out processor (外合处理器)

A special version of a processor that has some of the internal signals brought out to external pins. A bond-out processor is most often found within an emulator and is never intended to be used in a production system.

一种特殊版本的处理器,它有一些,内部的信号能传达到外置的针脚上。一个外合处理器绝大多数情况下只用在模拟器上,从来不会被特意用在产品系统上。

Breakpoint (断点)

A location in a program at which execution is to be stopped and control of the processor switched to the debugger. Mechanisms for creating and removing breakpoints are provided by most debugging tools.

一个在程序中的地址,在那里程序的执行被停止,并且处理器的控制转换到了除错程序。大多数除错工具提供增加与删除一个断点的机制。

C CISC (复杂指令集计算机)

Complex Instruction Set Computer. Describes the architecture of a processor family. CISC processors generally feature variable-length instructions, multiple addressing formats, and contain only a small number of general-purpose registers. Intel"s 80x86 family is the quintessential example of CISC. Contrast with RISC.

复杂指令集计算机。对一种处理器架构的描述。CISC 处理器一般产生变长的指令,多种地址格式,并且仅仅有少量的通用寄存器。Intel 的80x86家族是是典型的CISC 处理器。相对于RISC 而言。 CPU (中央处理器)

Central Processing Unit. The part of a processor that executes

instructions.

中央处理器。处理器中执行指令的那一部分。

Compiler (编译器)

A software development tool that translates high-level

language programs into the machine-language instructions that a particular processor can understand and execute.

把高级编程语言程序转换到只有特定的处理器能了解和执行的机器指令的一种软件开发包。

context (上下文)

The current state of the processor"s registers and flags. 处理器当前的状态和标志。

context switch(上下文切换)

The process of switching from one task to another in a multitasking operating system. A context switch involves saving the context of the running task and restoring the previously-saved context of the other. The piece of code that does this is necessarily processor-specific.

在多任务操作系统中我一个任务切换到另一个的过程。上下文切换包括保存正在运行的任务的上下文和恢复早先保存的另一个任务的上下文。做这个工作的一段代码必须具有处理器特权。

counting semaphore(计数信号)

A type of semaphore that is used to track multiple resources of the same type. An attempt to take a counting semaphore is blocked only if all of the available resources are in use. Contrast with binary semaphore.

一种用来跟踪多个相同类型资源的信号灯。仅仅在所有可用的资源都被用完了时才阻塞。相对二元信号而言。

critical section(临界段)

A block of code that must be executed in sequence and without interruption to guarantee correct operation of the software. See also race condition.

一段必须按次序执行的代码,并且不能被中断,否则不能保证软件正确地操作。参照:竞争状况。

cross-compiler (交叉编译器)

A compiler that runs on a different platform than the one for which it produces object code. A cross-compiler runs on a host computer and produces object code for the target.

一个运行在不同的平台上的编译器,其中之一能产生目标代码。交叉编译器在主机上运行并且产生目标机的目标代码。

D DMA (直接内存访问)

Direct Memory Access. A technique for transferring data

directly between two peripherals (usually memory and an I/O device) with only minimal intervention by the processor. DMA transfers are managed by a third peripheral called a DMA controller.

直接内存访问。一种直接在两个外设(通常是内存和I/O设备)之间进行数据传输的技术,它只要处理器最少的介入。DMA 传输由叫DMA 控制器的第三方外设进行管理。

DRAM (动态随机访问存储器)

Dynamic Random-Access Memory. A type of RAM that maintains its contents only as long as the data stored in the device is refreshed at regular intervals. The refresh cycles are usually performed by a peripheral called a DRAM controller.

动态随机访问存储器。一种RAM ,存储在其设备中的数据被定期刷新时才能保存它的内容。刷新周期一般由一个叫DRAM 控制器的外设完成。

Data bus(数据总线)

A set of electrical lines connected to the processor and all of the peripherals with which it communicates. When the processor wants to read (write) the contents of a memory location or register within a particular peripheral, it sets the address bus pins appropriately and receives (transmits) the

contents on the data bus.

连接处理器与所有外设进行通讯的电子线路集。当一个处理器想去写(读)某一特定外设中的存储器地址或寄存器中的内容时,处理器设置地址总线并在数据总线上接收(传输)内容。

Deadline (死线)

The time at which a particular set of computations must be completed. See also real-time system.

一个特定计算必须被完成的时间。请看实时系统。

Deadlock (死锁)

An unwanted software situation in which an entire set of tasks is blocked, waiting for an event that only a task within the same set can cause. If a deadlock occurs, the only solution is to reset the hardware. However, it is usually possible to prevent deadlocks altogether by following certain software design practices.

一种不希望出现的软件状态,在这个状态下,所有的任务因为等待一个只有在这些被阻塞任务之一才能产生的事件而被阻塞。如果死锁发生,唯一解决的方法是重启动硬件。但是,通过可靠的软件设计实践活动通常可以防止死锁的发生。

debug monitor(除错监视程序)

A piece of embedded software that has been designed

specifically for use as a debugging tool. It usually resides in ROM and communicates with a debugger via a serial port or network connection. The debug monitor provides a set of primitive commands to view and modify memory locations and registers, create and remove breakpoints, and execute your program. The debugger combines these primitives to fulfill higher-level requests like program download and single-step. 嵌入式软件被特殊设计来作为除错工具的一部分。它一般被放在ROM 中,通过串口或网络与除错器进行通讯。除错监视程序提供一个简单的命令集来显示和内存地址和寄存器、建立和移除断点,并且运行你的程序。除错监视器组合这些简单的命令去实现象程序下载各单步调试等高端的请求。

Debugger (除错器)

A software development tool used to test and debug embedded software. The debugger runs on a host computer and connects to the target through a serial port or network connection. Using a debugger you can download software to the target for immediate execution. You can also set breakpoints and examine the contents of specific memory locations and registers.

一个软件开发工具,被用来对嵌入式软件进行测试和除错。除错器在宿主机上运行并且通过串口或网络连接到目标机上。你能使用除错器

下载软件到目标机并直接运行。你也可以设置断点并检查特定内存地址或寄存器的内容。

device driver(设备驱动程序)

A software module that hides the details of a particular peripheral and provides a high-level programming interface to it.

一个软件模块,它隐藏特定外设的细节并提供高级的外设编程接口。 device programmer(设备编程器)

A tool for programming non-volatile memories and other electrically-programmable devices. Typically, the

programmable device is inserted into a socket on the device programmer and the contents of a memory buffer are then transferred into it.

一种用来对不挥发内存和其他电可编程设备进行编程的工具。典型地,可编程设备被插到设备编程器的接口上,接着内存缓存器中的内容被传送到它里面。

digital signal processor(数字信号处理器)

A device that is similar to a microprocessor, except that the internal CPU has been optimized for use in applications involving discrete-time signal processing. In addition to standard microprocessor instructions, DSPs usually support a

set of complex instructions to perform common

signal-processing computations quickly.Common DSP families are TI"s 320Cxx and Motorola"s 5600x series.

一种类似于微处理器的的设备,不同的是它内部的CPU 被优化,用于特定的应用,如离散信号处理。除了标准的微处理器指令外,DSP 常常支持复杂指令集去非常快地完成通用的信号处理计算。通用DSP 家庭是TI 的320Cxx 和Motorola 的5600x 系列。

E EEPROM (电可擦的,可编程的只读存储器)

Electrically Erasable, Programmable Read-Only Memory.

(Pronounced"Double-E"-PROM.) A type of ROM that can be erased electronically.

电可擦的,可编程的只读存储器。一种ROM 能被电擦除。

EPROM (可擦的,可编程的只读存储器)

Erasable, Programmable Read-Only Memory. A type of ROM that can be erased by exposing it to ultraviolet light. Once erased, an EPROM can be reprogrammed with the help of a device programmer. 一种可用紫外线擦除的存储器。一次擦除后,EPROM 可以在设备编程器的帮助下被重编程。

embedded system(嵌入式系统)

A combination of computer hardware and software, and perhaps additional mechanical or other parts, designed to perform a

dedicated function. In some cases, embedded systems are part of a larger system or product, as is the case of an anti-lock braking system in a car. Contrast with general-purpose computer.

计算机硬件和软件的结合体,或许还加上机械等其他部分,被设计来完成专门的功能。在一些情况下,嵌入式系统是一个大的系统或产品的一部分,就象汽车上的防抱死装置。与通用计算机相对。 Emulator(仿真器)

Short for In-Circuit Emulator (ICE). A debugging tool that takes the placeof-emulates-the processor on your target board. Emulators frequently incorporate a special "bond-out" version of the target processor that allows you to observe and record its internal state as your program is executing . 在线仿真器的简写。一个在你的目标板上放置仿真的处理器的调试工具。仿真器经常和一目标处理器的一种“外合”版本合在一起,这个版本的的处理器充许你运行程序时观察和记录它的内部状态。

Executable (可执行的)

A file containing object code that is ready for execution on the target. All that remains is to place the object code into a ROM or download it via a debugging tool.

一个包含准备在目标机上运行的目标代码的文件。放置目标代码到ROM 中或通过调试工具下载。

F Firmware (固件)

Embedded software that is stored as object code within a ROM. This name is most common among the users of digital signal processors.

是作为目标代码存贮在ROM 中的嵌入式软件。这个名字在数字信号处理器的用户中相当流行。

flash memory (闪存)

A RAM-ROM hybrid that can be erased and rewritten under software control. Such devices are divided into blocks, called sectors, that are individually-erasable. Flash memory is common in systems that require nonvolatile data storage at very low cost. In some cases, a large fash memory may even be used instead of a disk-drive.

一种RAM-ROM 的混血儿,它能在软件的控制下被擦除和重写。一些设备被分成叫段组的块,能个别地可擦。闪存用在需要很便宜的非易失数据存贮器的地方,一个大容量的闪存甚至被用作磁盘驱动器。 G general-purpose computer(通用计算机)

A combination of computer hardware and software that serves as a

general-purpose computing platform. For example, a personal computer. Contrast with embedded system.

当作通用计算平台的计算机硬件与软件的组合。例如,PC 。相对于嵌入式计算机。

H HLL

See high-level language.

查阅高级语言。

Heap (堆)

An area of memory that is used for dynamic memory allocation. Calls to malloc and free and the C++ operators new and delete result in run-time manipulation of the heap.

一块被用作动态内存分配的内存区域。调用malloc 和free 、C++的操作符new 、delete 在运行时进行堆的操作。

high-level language(高级语言)

A language, such as C or C++, that is processor-independent. When programming in a high-level language, it is possible to concentrate on algorithms and applications without worrying about the details of a particular processor.

一种语言,象C 或C++,是处理器独立的。当在高级语言上编程时,不需要考虑特定处理器的细节,只用关心算法和应用。

Host (主机)

A general-purpose computer that communicates with the target via a serial port or network connection. This term is usually used to distinguish the computer on which the debugger is running from the embedded system that is being developed. 一台通用计算机,它通过串口或网络连接与目标机通讯。这处名词一般用来区别调试程序运行的计算机和被开发的嵌入式系统。 I

ICE

In-Circuit Emulator. See emulator.

在线仿真器。查阅仿真器。

I/O(输入/输出)

Input/Output. The interface between a processor and the world around it. The simplest examples are switches (inputs) and LEDs (outputs).

输入/输出。处理器与外界的交互界面。最简单的例子是开关(输入)和发光二级管(输出)。

I/O device(IO 设备)

A piece of hardware that interfaces between the processor and the outside world. Common examples are switches and LEDs, serial ports, and network controllers.

一种介于处理器和外界之间的硬件设备。一般的实例是开关、LED 、串口和网络控制器。

I/O map(I/O映射)

A table or diagram containing the name and address range of each peripheral addressable by the processor within the I/O space. I/O maps are a helpful aid in getting to know the target. 一张包含每个外设的名字和地址的表格或图表,可由处理器在I/O空间中设定地址。I/O映射对得知目标机情况非常有利。

I/O space(I/O空间)

A special memory region provided by some processors and

generally reserved for the attachment of I/O devices. Memory locations and registers within an I/O space can be accessed only via special instructions. For example, processors in the 80x86 family have special I/O space instructions called in and out. Contrast with memory space.

一个由处理器提供的特殊内存区域,一般为为I/O设备的附件保留。在I/O空间的内存位置和寄存器只能通过特殊的指定进行访问。例如:80X86家族的处理器有叫做in / out的特殊的I/O空间指令。相对内存空间而言。

ISR (中断服务程序)

See interrupt service routine.

instruction pointer(指令指针)

A register in the processor that contains the address of the next instruction to be executed. Also known as a program counter. 包含下一条要执行指令地址的处理器中的寄存器。也叫程序计数器。

Interrupt (中断)

An asynchronous electrical signal from a peripheral to the processor. When the peripheral asserts this signal, we say that an interrupt occurs. When an interrupt occurs, the current state of the processor is saved and an interrupt service routine is executed. When the interrupt service routine exits, control of the processor is returned to whatever part of the software was previously running.

一个从外设到处理器的异步电信号。当外设发出这个信号,我们说一个中断发生。当一个中断发生,当前的处理器状态被保存并且中断服务程序开始运行。当中断服务程序退出,对处理器的控制权转到先前运行的那个软件上。

interrupt latency(中断延迟)

The amount of time between the assertion of an interrupt and the start of the associated interrupt service routine. 在中断发生和相关的中断服务程序运行之间的时间长短。

interrupt service routine(中断服务程序)

A piece of software executed in response to a particular interrupt.

响应特定中断而运行的一小段软件。

interrupt type(中断类型)

A unique number associated with each interrupt.

和每一个中断相关联的唯一数字。

interrupt vector (中断向量)

The address of an interrupt service routine.

中断服务程序所在的地址。

interrupt vector table(中断向量表)

A table containing interrupt vectors and indexed by interrupt type. This table contains the processor"s mapping between interrupts and interrupt service routines and must be initialized by the programmer.

一个表格,包含由中断类型决定的中断向量和索引。这个表格包含中断与中断服务程序之间的处理器的映射,必须由程序员进行初始化。 intertask communication(进程间通讯)

A mechanism used by tasks and interrupt service routines to share information and synchronize their access to shared

resources. The most common building blocks of intertask communication are semaphores and mutexes.

一种被用来在任务和中断服务程序之间共享信息和同步它们对共享资源访问的机制。大部分进程间通讯的建立的基石是信号灯和互斥。 K Kernel (内核)

An essential part of any multitasking operating system, the kernel contains just the scheduler and context-switch routine. 任何多任务操作系统的本质部分,内核仅仅包含调度程序和上下文切换进程。

L Linker (连接程序)

A software development tool that accepts one or more object files as input and outputs a relocatable program. The linker is thus run after all of the source files have been compiled or assembled.

一种能把一个或更多目标文件组合成可输入和输出的可重定位程序的开发工具。连接程序在所有的源文件都被编译或汇编之后运行。 Locator (定位程序)

A software development tool that assigns physical addresses to the relocatable program produced by the linker. This is the last step in the preparation of software for execution by an embedded system and the resulting file is called an executable. In some

cases, the locator"s function may be hidden within the linker. 一种分配物理地址给连接程序处理过的可重定位程序的软件开发工具。这是准备一个软件在嵌入式系统中运行的最后一步,并且结果文件叫做可执行的。在一些情况下,定位程序功能隐藏在连接程序中。 logic analyzer(逻辑分析仪)

A hardware debugging tool that can be used to capture the logic levels (0 or 1) of dozens, or even hundreds, of electrical signals in real-time. Logic analyzers can be quite helpful for debugging hardware problems and complex processor-peripheral interactions.

一种硬件调试工具,能捕获实时电信号的许多逻辑电平(0或1),逻辑分析仪在调试硬件问题和复杂的处理外设交互时相当有用。

TCP/IP 应用程序的通信连接模式

2011年05月06日 星期五 17:42

TCP/IP 应用层与应用程序

TCP/IP 起源于二十世纪 60 年代末美国政府资助的一个分组交换网络研究项目,它是一个真正的开放协议,很多不同厂家生产各种型号的计算机,它们运行完全不同的操作系统,但 TCP/IP 协议组件允许它们互相进行通信。现在 TCP/IP 已经从一个只供一些科学家使用的小实验网成长为一个由成千上万的计算机和用户构成的全球化网络,TCP/IP 也已成为全球因特网(Internet )的基础,越来越多的 TCP/IP 互联网应用和企业商业应用正在改变着世界。

TCP/IP 通讯协议采用了四层的层级模型结构(注:这与 OSI 七层模型不相同),每一层都调用它的下一层所提供的网络任务来完成自己的需求。TCP/IP 的每一层都是由一系列协议来定义的。这 4 层分别为:

应用层 (Application):应用层是个很广泛的概念,有一些基本相同的系统级 TCP/IP 应用以及应用协议,也有许多的企业商业应用和互联网应用。

传输层 (Transport):传输层包括 UDP 和 TCP,UDP 几乎不对报文进行检查,而 TCP 提供传输保证。

网络层 (Network):网络层协议由一系列协议组成,包括 ICMP、IGMP 、RIP 、OSPF 、IP(v4,v6) 等。

链路层 (Link):又称为物理数据网络接口层,负责报文传输。 图1显示了 TCP/IP 层级模型结构,应用层之间的协议通过逐级调用传输层(Transport layer )、网络层(Network Layer )和物理数据链路层(Physical Data Link )而可以实现应用层的应用程序通信互联。

应用层需要关心应用程序的逻辑细节,而不是数据在网络中的传输活动。应用层其下三层则处理真正的通信细节。在 Internet 整个发展过程中的所有思想和着重点都以一种称为 RFC(Request For Comments )的文档格式存在。针对每一种特定的 TCP/IP 应用,有相应的 RFC 文档。一些典型的 TCP/IP 应用有 FTP、Telnet 、SMTP 、SNTP 、REXEC 、TFTP 、LPD 、SNMP 、NFS 、INETD 等。RFC 使一些基本相同的 TCP/IP 应用程序实现了标准化,从而使得不同厂家开发的应用程序可以互相通信。

图 1 TCP/IP 层级模型结构

然而除了这些已经实现标准化的系统级 TCP/IP 应用程序外,在企业商业应用和互联网应用开发中,存在着大量的商业应用程序通信互联问题。如图 1 显示,其中的应用层所包含应用程序主要可以分成两类,即系统级应用和商业应用,互联网商业应用是商业应用中的主要形式之一。

不同开发商和用户在开发各自商业应用通信程序时也存在有许多不同的设计方式。关于 TCP/IP 应用层以下的技术文献与书籍早已是汗牛充栋,但是关于 TCP/IP 应用本身,尤其是关于商业应用的通信设计模式技术讨论方面的文章还是比较少的。TCP/IP 应用通信设计模式实际上是在 TCP/IP 基础编程之上的一种应用编程设计方式,

也属于一种应用层协议范畴,其可以包含有 TCP/IP 地址族模式设计、I/O 模式设计、通信连接模式设计以及通信数据格式设计等。鉴于目前讨论 TCP/IP 商业应用程序设计模式问题这方面的文章还很少见,本文尝试给出一些通信连接模式设计中共同的概念与一些典型的设计模式,在以后的文章中将继续讨论地址族模式设计、I/O 模式设计、以及通信数据格式设计等方面的模式设计实现话题。 通信连接模式设计主要考虑内容有:

通信两端程序建立通信方式

通信连接方式

通信报文发送与接收方式

以下内容将介绍建立通信的 Client/Server 模型,然后逐一介绍通信连接模式设计所需要考虑的这些内容。

传输层接口 APIs 与 TCP/IP 应用程序 C/S 模型

传输层接口 APIs

TCP/IP 应用层位于传输层之上,TCP/IP 应用程序需要调用传输层的接口才能实现应用程序之间通信。目前使用最广泛的传输层的应用编程接口是套接字接口(Socket )。Socket APIs 是于 1983 年在 Berkeley Socket Distribution (BSD) Unix 中引进的。 1986 年

AT&T 公司引进了另一种不同的网络层编程接口 TLI(Transport Layer Interface ),1988 年 AT&T 发布了一种修改版的 TLI,叫做 XTI (X/open Transport interface)。XTI/TLI 和 Socket 是用来处理相同任务的不同方法。关于 TCP/IP APIs 使用文章与书籍已相当多,本文则是侧重于如何组合使用这些 APIs 来进行 TCP/IP 应用程序连接模式设计,并归纳出几种基本应用连接模式。

如图 2 显示,应用层是通过调用传输层接口 APIs(Socket 或 XTI/TLI)来与传输层和网络层进行通信的。

图 2 传输层接口

不管是使用何种编程接口,要在两个机器或两个程序之间建立通信,通信双方必须建立互相一致的通信模式。如果双方的通信设计模式不一致就无法建立有效的通信连接。

以下是经常使用的 socket APIs ,是建立 TCP/IP 应用程序的标准接口,也是影响 TCP/IP 应用程序通信方式的几个主要 APIs,不同 APIs 组合再结合系统调用可以实现不同方式的应用。Sockets 支持多种传输层和网络层协议,支持面向连接和无连接的数据传输,允许应用分布式工作。

socket():是用来创建一个 socket,socket 表示通信中的一个节点,其可以在一个网络中被命名,用 socket 描述符表示,socket 描述符类似于 Unix 中的文件描述符。

bind():是用来把本地 IP 层地址和 TCP 层端口赋予 socket。 listen() :把未连接的 socket 转化成一个等待可连接的 socket ,允许该 socket 可以被请求连接,并指定该 socket 允许的最大连接数。

accept():是等待一个连接的进入,连接成功后,产生一个新的 socket 描述符,这个新的描述符用来建立与客户端的连接。 connect():用来建立一个与服务端的连接。

send():发送一个数据缓冲区,类似 Unix 的文件函数 write()。

另外 sendto() 是用在无连接的 UDP 程序中,用来发送自带寻址信息的数据包。

recv():接收一个数据缓冲区,类似 Unix 的文件函数 readI()。另外 recvfrom() 是用在无连接的 UDP 程序中,用来接收自带寻址信息的数据包。

close():关闭一个连接

Client/Server 模型

Sockets 是以 Client 和 Server 交互通信方式来使用的。典型的系统配置是把 Server 放在一台机器中,而把 Client 放在另一台机器中,Client 连接到 Server 交换信息。一个 socket 有一系列典型的事件流。例如,在面向连接的 Client/Server 模型中,Server 端的 socket 总是等待一个 Client 端的请求。要实现这个请求,Server 端首先需要建立能够被 Client 使用的地址,当地址建立后,Server 等待 Client 请求服务。当一个 Client 通过 socket 连接到 Server 后,Client 与 Server 之间就可以进行信息交换。

Client/Server 是通信程序设计的基本模式。从软件开发的角度讲,TCP/IP 应用程序都是基于 Client/Server 方式的。注意本篇文章以下 Client/Server 概念是针对程序内部调用 Socket API 所讲的概

念,与针对整个程序甚至针对机器而讲的客户端 / 服务器概念有所不同。用 Server APIs 建立的程序可以被当作客户端使用,用 Client APIs 建立的程序也可以被用作服务器端使用。建立 Server 需要的 APIs 有 socket(), bind(), listen(), accept(),建立 Client 需要的 APIs 有 Socket(), Connect()。在实际应用开发中,同一个程序里往往同时可以有 Client 和 Server 的代码,或者多种形式的组合。在实际应用编程中,针对 Socket APIs 不同有效组合,结合系统调用可以有多种复杂的设计变化。

面向连接的应用编程存在三类基本的不同级别的设计方式范畴,根据 Socket APIs 从上到下顺序依次是:

Client/Server 通信建立方式

Client/Server 通信连接方式

Client/Server 通信发送与接收方式

下面内容以面向连接的 Socket 应用编程为例来说明这几种不同通信范畴的设计实现。

Client/Server 建立方式设计概述

一个 Client 连接一个 Server

如果只有两台机器之间连接,那么一个是 Client,另一个是 Server ,如下面图 3 所示。这是最简单的 TCP/IP 的应用,也是 TCP/IP 应用早期的 Peer to Peer (P2P) 概念。其流程基本如图 4 所示。

图 3 TCP/IP 应用单点 Client/Server

图 4 显示了 TCP/IP 应用编程最基本的 Client/Server 模式,显示了基本的 Client/Server 通信所需要调用的 Socket APIs 以及顺序。

图 4 TCP/IP 应用编程基本 Client/Server 模式

多个 Client 连接一个 Server

多个 Client 同时连接一个 Server 是 TCP/IP 应用的主流形式,如图 5 所示,其中 Client 连接数可以从几个到成千上万。 图 5 TCP/IP 应用多 Client 端的 Client/Server

由于 socket APIs 缺省方式下都是阻塞方式的,实现多个 Client 同时连接一个 Server 就需要特别的设计。其实现方式可以有多种不同的设计,这其中也涉及 I/O 模式设计。下面将展开介绍其中几种设计形式。

利用一个 Client 连接一个 Server 形式实现多 Client 连接 从程序设计角度讲,只要 Client 和 Server 端口是一对一形式,那么就属于一个 Client 连接一个 Server 形式。在处理多个 Client 端连接时,Server 端轮流使用多个端口建立多个

Client-Server 连接,连接关闭后,被释放端口可以被循环使用。在这种多连接形式中需要谨慎处理 Client 端如何获取使用 Server 端的可用端口。比如图 6 显示 Server 有一个服务于所有进程的进

程可以先把 Server 端的可用端口发送给 Client 端,Client 端再使用该端口建立连接来处理业务。Server 针对每一个 Client 连接用一个专门的进程来处理。由于可用端口数有限,Server 用一个有限循环来处理每一个可用的端口连接。由于新端口需要用 bind() 来绑定,所以需要从 bind() 开始到 close() 结束都需要包含在循环体内。

图 6 利用一对一 Client-Server 模式实现多 Client 连接

使用多个 accept() 实现多 Client 连接

多进程 Server 一般有一个专注进程是服务于每一个连接的。当 Client 端完成连接后,专注进程可以循环被另外的连接使用。使用多个 accept() 也可以实现处理多 Client 连接。多 accept() 的 Server 也只有一个 socket(),一个 bind(),一个 listen(),这与通常情况一样。但是它建立许多工作子进程,每一个工作子进程都有 accept(),这样可以为每一个 Client 建立 socket 描述符。如图 7 所示,由于 accept() 连接成功后,会产生一个新的 socket 描述符,

这样通过循环多进程利用 accept() 产生的多 socket 描述符就可以与多个 Client 进行连接通信。循环体是从 accept() 开始到 close() 结束的。

图 7 使用多 accept() 实现多 Client 连接

使用并发 Server 模式实现多 Client 连接

并发服务器模式曾经是 TCP/IP 的主流应用程序设计模式,得到广泛使用,目前互联网上仍有相当多的应用使用此种模式。其设计思

路是在 accept 之后 fork 出一个子进程。因为 socket 会产生监听 socket 描述符 listenfd,accept 会产生连接 socket 描述符 connfd 。连接建立后,子进程继承连接描述符服务于 Client,父进程则继续使用监听描述符等待另外一个 Client 的连接请求,以产生另外一个连接 socket 描述符和子进程。如图 8 所示,accept() 接收到一个 Client 连接后,产生一个新的 socket 描述符,通过 fork() 系统调用,用一个子进程来处理该 socket 描述符的连接服务。而父进程可以立即返回到 accept(),等待一个新的 Client 请求,这就是典型的并发服务器模式。并发服务器模式同时处理的最大并发 Client 连接数由 listen() 的第二个参数来指定。

图 8 TCP/IP 应用并发 Server

使用 I/O 多路技术实现多 Client 连接

以上三种连接设计,多 Server 端口、多 accept() 和并发服务器模式,都是通过 fork() 系统调用产生多进程来实现多 Client 连接的。使用 I/O 多路技术也可以同时处理多个输入与输出问题,即用一个进程同时处理多个文件描述符。I/O 多路技术是通过

select() 或 poll() 系统调用实现的。poll() 与 select() 功能完全相同,但是 poll() 可以更少使用内存资源以及有更少的错误发生。select() 调用需要与操作文件描述符集的 APIs 配合使用。select() 系统调用可以使一个进程检测多个等待的 I/O 是否准备好,当没有设备准备好时,select() 处于阻塞状态中,其中任一设

备准备好后,select() 函数返回调用。select() API 本身也有一个超时时间参数,超时时间到后,无论是否有设备准备好,都返回调用。其流程如图 9 所示。在 socket APIs listen() 和 accept() 之间插入 select() 调用。使用这三个宏 FD_ZERO()、FD_CLR() 和

FD_SET(),在调用 select() 前设置 socket 描述符屏蔽位,在调用 select() 后使用 FD_ISSET 来检测 socket 描述符集中对应于 socket 描述符的位是否被设置。 FD_ISSET() 就相当通知了一个 socket 描述符是否可以被使用,如果该 socket 描述符可用,则可对该 socket 描述符进行读写通信操作。通常,操作系统通过宏 FD_SETSIZE 来声明在一个进程中 select() 所能操作的文件或 socket 描述符的最大数目。更详细的 I/O 多路技术实现,可以参考其他相关文献。

图 9 I/O 多路技术实现多连接的 Server

一个 Client 连接多个 Server

一个 Client 连接多个 Server 这种方式很少见,主要用于一个客户需要向多个服务器发送请求情况,比如一个 Client 端扫描连接多个 Server 端情况。如图 10 所示。此种方式设计主要是 Client 端应用程序的逻辑设计,通常需要在 Client 端设计逻辑循环来连接多个 Server,在此不做更多描述。

图 10 单 Client 对多 Server

复杂 Client/Server 设计与现代 P2P

最近几年,对等网络技术 ( Peer-to-Peer,简称 P2P) 迅速成为计算机界关注的热门话题之一,以及影响 Internet 未来的科技之

一。与早期点对点 (Peer to Peer) 的 Client/Server 模式不同,现在的 P2P 模式是指每个结点既可充当服务器,为其他结点提供服务,同时也可作为客户端享用其他结点提供的服务。实际上 P2P 模式仍然是基于 Client/Server 模式的,每个通信节点都既是 Server ,又是 Client,P2P 是基于复杂 Client/Server 设计的 TCP/IP 应用。图 11 显示 P2P 模式下两个用户 PC 之间的对等连接。

图 11 P2P 模式

在技术上,P2P 本身是基于 TCP/IP Client/Server 技术的一种设计模式思想, P2P 也属于网络应用层技术,与 Web 和 FTP 等应用是并列的。只是 P2P 应用在设计实现上更要复杂的多。P2P 技术实现的协同工作是无需专门的服务器支持的 (Serverless),这里的服务器概念与 Client/Server 中的 Server 概念是不一样的。在传统意义上中心服务器机器上往往运行的是 TCP/IP 应用的 Server 端程序,所以传统意义上的 Server 概念在机器与应用上是重合的。如果更改 TCP/IP 的应用设计,使应用程序既可做 Server 又可做 Client ,就可以实现无中心服务器的 P2P 模式。

在设计模式上,P2P 模式实现了网络终端用户不依赖中心服务器或者服务商而直接进行信息和数据交换的可能,因此 P2P 正在改变着整个互联网的一些基础应用,从而极大地增加了用户之间的信息沟通和交流能力。目前互联网的 P2P 应用与网络都正在飞速发展,一些典型的 P2P 应用程序比如有 BitTorrent, eDonkey 等,另外一些即时通信(IM )类软件比如 MSN、QQ 等也正在向无中心服务器模式转变。

无中心服务器的 Internet 应用程序大大降低应用提供商的运营成本,而且减少人们对于 Server 稳定性的依赖。

Client/Server 通信连接方式设计

Client/Server 通信方式建立后,下一步就需要考虑通信连接的方式,主要有两种方式的连接,即长连接通信与短连接通信。通信连接方式涉及到的 APIs 主要是 connect() 和 accept()。要实现某种 Client/Server 方式,就必须考虑用某种特定的连接方式。 短连接通信

短连接通信是指 Client 方与 Server 方每进行一次通信报文收发交易时才进行通讯连接,交易完毕后立即断开连接。此种方式常用于多个 Client 连接一个 Server 情况,常用于机构与用户之间通信,比如 OLTP(联机事务处理)类应用。在短连接情况下,Client 端完成任务后,就关闭连接并退出。在 Server 端,可以通过循环 accept(),使 Server 不会退出,并连续处理 Client 的请求。图 12 显示了一般情况下短连接通信模式的 Socket 事件流,不同设计的连接多 Client 的 Server 有不同的循环流程。

图 12 短连接模式通信

长连接通信

长连接通信是指 Client 方与 Server 方先建立通讯连接,连接建立后不会断开,然后再进行报文发送和接收,报文发送与接收完毕后,原来连接不会断开而继续存在,因此可以连续进行交易报文的发送与接收。这种方式下由于通讯连接一直存在,其 TCP/IP 状态是

Established ,可以用操作系统的命令 netstat 查看连接是否建立。由于在长连接情况下,Client 端和 Server 端一样可以固定使用一个端口,所以长连接下的 Client 也需要使用 bind() 来绑定 Client 的端口。在长连接方式下,需要循环读写通信数据。为了区

分每一次交易的通信数据,每一次交易数据常常需要在数据头部指定该次交易的长度,接收 API 需要首先读出该长度,然后再按该长度读出指定长度的字节。长连接方式常用于一个 Client 端对一个 Server 端的通讯,一般常用于机构与机构之间的商业应用通信,以处理机构之间连续的大量的信息数据交换。或者说可用于两个系统之间持续的信息交流情况。通常为了加快两个系统之间的信息交流,通常还需要建立几条长连接的并行通信线路。图 13 显示了一般情况下长连接通信模式的 socket 事件流,可见其最大特点是 Client 和 Server 都有循环体,而且循环体只包含读写 APIs。

图 13 长连接模式通信

Client/Server 通信发送与接收方式设计

在通信数据发送与接收之间也存在不同的方式,即同步和异步两种方式。这里的同步和异步与 I/O 层次的同异步概念不同。主要涉及 socket APIs recv() 和 send() 的不同组合方式。

同步发送与接收

从应用程序设计的角度讲,报文发送和接收是同步进行的,既报文发送后,发送方等待接收方返回消息报文。同步方式一般需要考虑超时问题,即报文发出去后发送方不能无限等待,需要设定超时时间,超过该时间后发送方不再处于等待状态中,而直接被通知超时返回。同步发送与接收经常与短连接通信方式结合使用,称为同步短连接通信方式,其 socket 事件流程可如上面的图 12 所示。

异步发送与接收

从应用程序设计的角度讲,发送方只管发送数据,不需要等待接收任何返回数据,而接收方只管接收数据,这就是应用层的异步发送与接收方式。要实现异步方式,通常情况下报文发送和接收是用两个不同的进程来分别处理的,即发送与接收是分开的,相互独立的,互不影响。异步发送与接收经常与长连接通信方式结合使用,称为异步长连接通信方式。从应用逻辑角度讲,这种方式又可分双工和单工两

种情况。

异步双工

异步双工是指应用通信的接收和发送在同一个程序中,而有两个不同的子进程分别负责发送和接收,异步双工模式是比较复杂的一种通信方式,有时候经常会出现在不同机构之间的两套系统之间的通信。比如银行与银行之间的信息交流。它也可以适用在现代 P2P 程序中。如图 14 所示,Server 和 Client 端分别 fork 出两个子进程,形成两对子进程之间的连接,两个连接都是单向的,一个连接是用于发送,另一个连接用于接收,这样方式的连接就被称为异步双工方式连接。

图 14 长连接异步双工模式

异步单工

应用通信的接收和发送是用两个不同的程序来完成,这种异步是利用两对不同程序依靠应用逻辑来实现的。图 15 显示了长连接方式下的异步单工模式,在通信的 A 和 B 端,分别有两套 Server 和 Client 程序,B 端的 Client 连接 A 端的 Server,A 端的 Server 只负责接收 B 端 Client 发送的报文。A 端的 Client 连接 B 端的 Server ,A 端 Client 只负责向 B 端 Server 发送报文。

图 15 长连接异步单工模式

典型通信连接模式

综上所述,在实际 TCP/IP 应用程序设计中,就连接模式而言,

我们需要考虑 Client/Server 建立方式、Client/Server 连接方式、Client/Server 发送与接收方式这三个不同级别的设计方式。实际 TCP/IP 应用程序连接模式可以是以上三类不同级别 Client/Server 方式的组合。比如一般 TCP/IP 相关书籍上提供的 TCP/IP 范例程序大都是同步短连接的 Client/Server 程序。有的组合是基本没有实用价值的,比较常用的有价值的组合是以下几种:

同步短连接 Server/Client

同步长连接 Server/Client

异步短连接 Server/Client

异步长连接双工 Server/Client

异步长连接单工 Server/Client

其中异步长连接双工是较为复杂的一种通信方式,有时候经常会出现在不同银行或不同城市之间的两套系统之间的通信,比如国家金卡工程。由于这几种通信方式比较固定,所以可以预先编制这几种通信方式的模板程序。

总结

本文探讨了 TCP/IP 应用程序中连接模式的设计。在以后的文章中还将继续讨论 TCP/IP 应用程序设计中的其他方面的设计话题,包

括地址族模式设计、I/O 模式设计、以及通信数据格式设计等。 + plus 加号;正号

- minus 减号;负号

± plus or minus 正负号

× is multiplied by 乘号

÷ is divided by 除号

= is equal to 等于号

≠ is not equal to 不等于号

≡ is equivalent to 全等于号

≌ is equal to or approximately equal to 等于或约等于号

≈ is approximately equal to 约等于号

< is less than 小于号

> is greater than 大于号

≮ is not less than 不小于号

≯ is not more than 不大于号

≤ is less than or equal to 小于或等于号

≥ is more than or equal to 大于或等于号

% per cent 百分之…

‰ per mill 千分之…

∞ infinity 无限大号

∝ varies as 与…成比例

√ (square) root 平方根

∵ since; because 因为

∴ hence 所以

∷ equals, as (proportion) 等于,成比例

∠ angle 角

≲ semicircle 半圆

≰ circle 圆

○ circumference 圆周

π pi 圆周率

△ triangle 三角形

≱ perpendicular to 垂直于

∪ union of 并,合集

∩ intersection of 交,通集

∫ the integral of …的积分

∑ (sigma) summation of 总和

° degree 度

′ minute 分

″ second 秒

℃ Celsius system 摄氏度

{ open brace, open curly 左花括号

} close brace, close curly 右花括号

( open parenthesis, open paren 左圆括号

) close parenthesis, close paren 右圆括号

() brakets/ parentheses 括号

[ open bracket 左方括号

] close bracket 右方括号

[] square brackets 方括号

. period, dot 句号,点

| vertical bar, vertical virgule 竖线

& ampersand, and, reference, ref 和,引用

* asterisk, multiply, star, pointer 星号,乘号,星,指针

/ slash, divide, oblique 斜线,斜杠,除号

// slash-slash, comment 双斜线,注释符

# pound 井号

/ backslash, sometimes escape 反斜线转义符,有时表示转义符或续行符 ~ tilde 波浪符

. full stop 句号

, comma 逗号

: colon 冒号

; semicolon 分号

? question mark 问号

! exclamation mark (英式英语) exclamation point (美式英语)

' apostrophe 撇号

- hyphen 连字号

-- dash 破折号

... dots/ ellipsis 省略号

" single quotation marks 单引号

"" double quotation marks 双引号

‖ parallel 双线号

& ampersand = and

~ swung dash 代字号

§ section; division 分节号

→ arrow 箭号;参见号


相关文章

  • 留学研修计划
  • 计划一:留学研修计划 一.拟留学专业(研究课题)的国内外研究现状 本人拟留学专业为教育社会学,具体希望研究课题为中外对比视角下的义务教育均衡发展理论与实践研究. 19世纪后期,义务教育陆续普及之后,教育发展的不均衡问题日益凸显出来,成了世界 ...查看


  • 电子信息工程专业人才培养方案
  • 电子信息工程专业人才培养方案 发布时间:2015-06-24 浏览次数: 1.培养目标 培养能为社会主义现代化建设服务,德智体美全面发展.具有较高的文化素养.敬业精神和社会责任感,有团队协作精神.有良好的社会道德和职业道德,有良好的身体素质 ...查看


  • 计算机专业课程表
  • 2007计算机科学与技术专业教学计划 一.培养目标 本专业培养适应海峡西岸经济区,尤其是厦门市经济建设与社会发展需要,具有良好的科学素养,系统扎实地掌握计算机科学与技术基本理论.基本知识和基本技能与方法,符合计算机发展规律要求,能综合运用知 ...查看


  • 自我职业化(2)
  • 自我职业化 [内容摘要]:对于我们现在的大学生来说,毕业后从事什么样的工作一直是关系我们一生生活方式的一个重要的问题. 关键字:无线通信研究技术员.条件要求.自我差距.研究生.专业知识.英语水平 一.我以后希望做的工作--无线通信技术研究员 ...查看


  • 一些关于电子通信方面的就业岗位和要求
  • 一些关于电子通信方面的就业岗位和要求 一些关于电子通信方面的就业岗位和要求 1.电子工程师: 电子专业,会熟练使用PROTEL99,POWER PCB等 2.PCB 布线工程师: 精通allegro PCB设计工具:精通BGA .QFP 等 ...查看


  • 电子信息就业方向
  • 电子信息就业方向 电子信息工程专业就业形势分析近几年来,IT.信息与电子类的毕业生在市场供求关系上普遍还是保持稳定的状况,社会需求量相对乐观.按照2005年毕业生就业情况统计,信息产业.IT.电子类的毕业生在各理工类中就业率比较靠前.在今后 ...查看


  • 探测制导与控制技术本科专业培养方案(1)
  • 西 北 工 业 大 学 本 科 生 培 养 方 案 专业名称 探测.制导与控制技术 专业代码 082103 学院名称 航天学院,航海学院,电子信息学院 培养方案制定人签字 年 月 日 院长签字 年 月 日 校长签字 年 月 日 西 北 工 ...查看


  • 通信行业招聘
  • 高级客户经理/销售经理 职位描述: 负责信令软件.媒体处理软件,通信板卡.信令网关的销售工作,完成销售指标. 任职要求: 1.本科以上学历(通信或计算机相关专业): 2.具有两年以上通信行业销售经验, 并有很好的销售业绩: 3.有嵌入式系统 ...查看


  • 自我推荐简历
  • 自我推荐和简历 各位考官大家好,我是XX 学习XX 专业的应界毕业生,这次很荣 幸我能来面试这份工作,虽然我没有工作经验,可是我有激情,有信心,有专业技能对待好这份工作的,我相信只要给我一次机会,我一定会用实际行动来证明的! 各位领导大家好 ...查看


  • 自动化专业英语翻译
  • B 计算机与网络基础 计算机系统的组成 在说明了什么是计算机之后,让我们在来看一下计算机的定义:计算机是一种能接收.存储和处理数据,并能产生输出结果的快速.精确的符号加工系统,这一系统是在存储指令程序控制下工作的.本文说明为什么计算机是一个 ...查看


热门内容