`
hereson
  • 浏览: 1427162 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

【译】C/S 多线程Socket类

阅读更多

Multi-threaded Client/Server Socket Class

C/S 多线程 Socket

·         Download ServerSocket demo project - 47.2 KB
原文地址:http://www.codeproject.com/KB/IP/serversocket.aspx

Screenshots

屏幕截图

Note: The demo can be started in Client or Server mode, executed with "/C" (or "/CLIENT") or "/S" (or "/SERVER", which is the default).

说明:示例可以以客户端或服务器模式启动,默认用“ /C ”(或“ /CLIENT ”)或“ /S ( 或“ /SERVER ) 执行。

Introduction

介绍

This article is about a client/server multi-threaded socket class. The thread is optional since the developer/designer is still responsible for deciding if he/she needs it. There are other Socket classes here and other places over the Internet, but none of them can provide feedback (event detection) to your application like this one does. It provides you with the following events detection: connection established, connection dropped, connection failed and data reception (including 0 byte packet).

本文内容主要是关于一个 C/S 的多线程 Socket 类。线程是可选择的,开发设计人员可根据他们的需求来决定是否使用。在互联网上,也有一些其他的 Socket 类,但它们都没有为你的应用程序提供反馈(事件探测),而本示例却提供了,且提供了如下几种事件探测:连接建立,连接断开,连接失败和数据接收(包括 0 比特的数据包)。

Description

描述

This article presents a new socket class which supports both TCP and UDP communication. It provides some advantages compared to other classes that you may find here or on some other Socket Programming articles. First of all, this class doesn't have any limitation like the need to provide a window handle to be used. This limitation is bad if all you want is a simple console application. So, this library doesn't have such a limitation. It also provides threading support automatically for you, which handles the socket connection and disconnection to a peer. It also features some options not yet found in any socket classes that I have seen so far. It supports both client and server sockets. A server socket can be referred as to a socket that can accept many connections. A client socket is a socket that is connected to a server socket. You may still use this class to communicate between two applications without establishing a connection. In the latter case, you will want to create two UDP server sockets (one for each application). This class also helps reduce coding needed to create chat-like applications and IPC (Inter-Process Communication) between two or more applications (processes). Reliable communication between two peers is also supported with TCP/IP with error handling. You may want to use the smart addressing operation to control the destination of the data being transmitted (UDP only). TCP operation of this class deals only with communication between two peers.

Now for those not familiar with IP Socket, the following section will give some details on how it works. This is also the goal with this article: to explain the basic functionality behind socket objects.

本文介绍的 Socket 类同时支持 TCP UDP 通讯。相对于你从其他 Socket 编程文章中找到的 Socket 类,本文介绍的类有一些优点。首先,该类没有任何限制,比如没有要提供窗口才能使用的限制,若你需要在控制台程序中使用时,这样的限制是相当糟糕的。因此,这儿的库就没有对此进行限制。其次它会自动提供对线程的支持,以处理同另一端的连接和断开连接。它还有一些我在其他 Socket 类中从未见到过的特色选项。它同时提供对客户端和服务端 Socket 的支持。一个服务器端的套接字可以被许多接受连接的套接字引用。一个客户端套接字可以用于与服务端套接字的连接。你可以不建立连接,仅使用该类在两个应用程序之间进行通信,对于后一种情况,你需要创建 2 UDP 服务器套接字(一个应用程序一个)。在你创建一些类似聊天的程序和一些进程间通信(两个或多个应用进程间的通信)时,该类会减少需要编写的代码量。通过 TCP/IP 错误处理,还提供了双方的可靠通信。你可能还想通过智能寻址操作来控制数据传输的目标(仅 UDP )。该类的 TCP 操作仅处理双方之间的通信。对于那些对 IP Socket 还不是很熟悉的朋友,

下面这节就会给出其如何工作的一些细节。这也是本文的目的之一:解释
Socket 对象背后的基本功能。

TCP/IP Stack

TCP/IP

The TCP/IP stack is shorter than the OSI one:

TCP/IP 所分层数要比OSI模型的层数少

TCP is a connection-oriented protocol, while UDP (User Datagram Protocol) is a connectionless protocol.

TCP 是面向连接的协议,而 UDP (用户自寻址协议)则是无连接协议。

IP Datagrams

IP 数据包

The IP layer provides a connectionless and unreliable delivery system. It considers each datagram independently of the others. Any association between datagrams must be supplied by the higher layers. The IP layer supplies a checksum that includes its own header. The header includes the source and destination addresses. The IP layer handles routing through the Internet. It is also responsible for breaking up large datagrams into smaller ones for transmission and reassembling them at the other end.

在网络层提供了无连接的不可靠传输系统。它认为每个数据包对于其他数据包都是独立的。数据包之间的任何联系都需要靠更高的层来代理。网络层提供包含自身报头的检查和。报头又包含了源地址和目的地址。网络层还对互联网的路由进行处理,负责对大数据包进行分解,并在另一端重新进行组合。

UDP

UDP is also connectionless and unreliable. What it adds to IP is a checksum for the contents of the datagram and port numbers. These are used to give a client/server model: see later.

UDP (用户数据报协议)是无连接和不可靠的,它被作为数据包内容和端口号的检查和加到 IP 中,它将被用来给出 C/S 模型,稍后会进行描述。

TCP

TCP supplies logic to give a reliable connection-oriented protocol above IP. It provides a virtual circuit that two processes can use to communicate.

TCP 为网络层之上的逻辑提供可靠的面向连接的协议。它提供一个虚拟电路以用于两个进程之间的通信。

Internet Addresses

In order to use a service, you must be able to find it. The Internet uses an address scheme for machines so that they can be located. The address is a 32-bit integer which gives the IP address. This encodes a network ID and more addressing. The network ID falls into various classes according to the size of the network address.

为了能使用服务,你必须找到它。互联网对计算机使用地址摘要来标识计算机的位置。 IP 地址是一个 32 位的整数。它是通过对网络 ID 和更多的寻址信息加密得到的。根据 IP 地址的大小,网络 ID 分成了不同的几类。

Network Address

Class A uses 8 bits for the network address with 24 bits left over for other addressing. Class B uses 16-bit network addressing; class C uses 24-bit network addressing and class D uses all 32.

A 类地址用 8 位来表示网络寻址, 24 位来表示其他寻址。 B 类地址用 16 位来表示网络寻址, C 类用 24 位来表示网络寻址, D 类则用全部的 32 位来表示网络寻址。

Subnet Address

Internally, the Unix network is divided into subnetworks. Building 11 is currently on one subnetwork and uses 10-bit addressing, allowing 1024 different hosts.

在内部, Unix 的网络划分为若干子网。用 11 来表示当前的一个子网,用 10 位来寻址,则允许有 1024 台不同的主机。

Host Address

8 bits are finally used for host addresses within our subnet. This places a limit of 256 machines that can be on the subnet.

在我们的子网中,有 8 位用于主机寻址。则该子网中的主机不能超过 256 台。

Total Address

The 32-bit address is usually written as 4 integers separated by dots.

32 位的地址常常写成用逗号分隔的 4 个整数。

Port Addresses

A service exists on a host and is identified by its port. This is a 16-bit number. To send a message to a server, you send it to the port for that service of the host that it is running on. This is not location transparency! Certain of these ports are "well known." For example:

一个主机上存在的服务通过其端口号来标识。它是一个 16 位的数字。向服务器发送消息,你就是向那台主机上运行的服务的端口号发送消息。没有哪个位置是透明的!当然这些端口号都是“清楚”的。例如:

tcpmux

1

TCP

echo

7

UDP

echo

7

TCP

systat

11

TCP

netstat

15

TCP

ftp-data

20

TCP File Transfer Protocol (data)

ftp

21

TCP File Transfer Protocol

smtp

25

TCP Simple Mail Transfer Protocol

time

37

TCP Time Server

time

37

UDP Time Server

name

42

UDP Name Server

whois

43

TCP nicname

domain

53

UDP

domain

53

TCP

tftp

69

UDP

rje

77

TCP

finger

79

TCP

link

87

TCP ttylink

supdup

95

TCP

hostname

101

TCP hostname

pop-2

109

TCP Post Office Protocol

uucp-path

117

TCP

nntp

119

TCP Network News Transfer Protocol

ntp

123

TCP Network Time Protocol

Ports in the region 1-255 are reserved by TCP/IP. The system may reserve more. User processes may have their own ports above 1023. The function getservbyname can be used to find the port for a service that is registered.

1-255 之间的端口号是被 TCP/IP 保留的。系统可能保留的端口号更多。用户自己使用的端口号应该在 1023 之上。 Getservbyname 函数用于找到注册服务的端口号。

Sockets

套接字

A socket is a data structure maintained by the system to handle network connections. A socket is created using the call socket . It returns an integer that is like a file descriptor. In fact, under Windows, this handle can be used with the ReadFile and WriteFile functions.

一个套接字是系统用于处理网络连接所保持的数据结构。可以通过调用 socket 方法来创建套接字。结果是返回一个如文件描述符之类的整数。实际上,在 Windows 环境中,这种操作可以结合着 ReadFile WriteFile 函数来使用。

#include < sys / types.h >

#include < sys / socket.h >

int socket( int family, int type, int protocol);

Here, "family" will be AF_INET for IP communications, protocol will be zero and type will depend on whether TCP or UDP is used. Two processes wishing to communicate over a network create a socket each. These are similar to two ends of a pipe, but the actual pipe does not yet exist.

此处,“ family ”参数可以用 AF_INET 表示 IP 通信, protocol 参数为 0 type 参数则要根据是用 TCP 还是 UDP 来定。两个进程之间通过创建各自的套接字来进行通信,这和管道很类似,然而并不存在什么管道。

Connection Oriented (TCP)

面向连接( TCP

One process (server) makes its socket known to the system using bind . This will allow other sockets to find it. It then "listens" on this socket to "accept" any incoming messages. The other process (client) establishes a network connection to it and then the two exchange messages. As many messages as needed may be sent along this channel, in either direction.

服务端进程通过 bind 方法将其套接字告知系统,以使其他的套接字能找到它。它可通过套接字的“侦听( listen )”来“接收( accept )”发过来的信息。客户端的进程同服务端套接字建立连接然后交换信息。需要的信息都可以从该通道向任一端进行发送。

Server:

服务器:

  • Create endpoint ( socket() )
  • Bind address ( bind() )
  • Specify queue ( listen() )
  • Wait for connection ( accept() )
  • Transfer data ( read() / write() )
  • 创建端点 ( socket() )
  • 绑定地址 ( bind() )
  • 指定队列 ( listen() )
  • 等待连接 ( accept() )
  • 传输数据 ( read() / write() )

Client:

客户端:

  • Create endpoint ( socket() )
  • Connect to server ( connect() )
  • Transfer data (
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics