http://blog.163.com/lyzaily@126/blog/static/424388372009101242036128/

我是笨人,是一个喜欢钻研的笨人,每遇到不明白的技术点时总是寝食难安非要追查个水落石出不可。

最近在用socket写个网络通信程序,以前都是在写的都是在pc上跑的网络程序,直接用系统提供的socke API就行了,但是这次是在mobile上写的,就不知道是否还可以像以前一样写网络程序;但是在网上看了很多都是使用的MFC类库中的CSocket类来写的,找了很久也没有看到答案,心里纠结了很久,今天非要弄给水落石出。

        通过翻阅MSDN总要找到了有关系统socket API、CAsyncSocket以及CSocket之间的关系。

现在一一说出使用它们的目的,也就是说为什么要出现这些接口以及这些类:

因为没有时间多写,在这里就先给出MSDN上的解释吧:

      (一)对于socket 的raw API,它是微软在网络协议上封装的一个通用接口:

  Winsock(Windows Sockets 2)其实是一个处于socket raw API 和网络协议栈之间的一个中间层(一个微软定义的一个standard service provider interface(SPI));Windows Sockets 2 is designed for use by C/C++ programmers. 需要程序员Familiarity with Windows networking .Windows Sockets 2可以被用于所有Windows平台,只是可能由于各个平台自身的特点而性能上有所不同。(所以我在mobile手机上也可以使用socket的raw API进行编程了)。

       Using Windows Sockets permits your application to communicate across any network that conforms to the Windows Sockets API。

Although the present Windows Sockets specification defines the sockets abstraction for TCP/IP, any network protocol can comply with Windows Sockets by supplying its own version of the dynamic-link library (DLL) that implements Windows Sockets. Examples of commercial applications written with Windows Sockets include X Windows servers, terminal emulators, and electronic mail systems.

      

Windwos Sockets的目的:

The purpose of Windows Sockets is to abstract away the underlying network so that you do not have to be knowledgeable about that network and so your application can run on any network that supports sockets.

Windwos Sockets的全双工的:

Both kinds of sockets are bidirectional; they are data flows that can be communicated in both directions simultaneously (full-duplex).

可以使用socket编程的三个情况:

Sockets are highly useful in at least three communications contexts:

  • Client/server models.

  • Peer-to-peer scenarios, such as chat applications.

  • Making remote procedure calls (RPC) by having the receiving application interpret a message as a function call.

 

(二)MFC通过给出以下两类,让app应用程序员调用从而写出网络程序: 

要注意一点是,如果你要用MFC提供的类来写网络程序,那么你最有在通讯的两端都是用这两个类来写程序;这个是因为在CSocket 中会自动为你进行字节序转换,而如果你使用CAsyncSocket 类或者socket 的raw AIP需要你自己根据情况进行自己序转换。如果两端都是使用CSocket类编写的通讯程序,就不需要考虑自己序的问题了(下一次我会讲讲网络传输过程中的字节序转换问题,这也是比较头大的问题)。

  •  

    CAsyncSocket

    This class encapsulates the Windows Sockets API. CAsyncSocket is for programmers who know network programming and want the flexibility of programming directly to the sockets API but also want the convenience of callback functions for notification of network events. Other than packaging sockets in object-oriented form for use in C++, the only additional abstraction this class supplies is converting certain socket-related Windows messages into callbacks. For more information, see Windows Sockets: Socket Notifications.

  •  

    CSocket

    This class, derived from CAsyncSocket, supplies a higher level abstraction for working with sockets through an MFC CArchive object. Using a socket with an archive greatly resembles using MFC's file serialization protocol. This makes it easier to use than the CAsyncSocket model. CSocket inherits many member functions from CAsyncSocket that encapsulate Windows Sockets APIs; you will have to use some of these functions and understand sockets programming generally. But CSocket manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket. Most importantly, CSocket provides blocking (with background processing of Windows messages), which is essential to the synchronous operation of CArchive.

 

小结一下:

            (1)为什么说使用CSocket类实现的通信程序为MFC应用呢?

                      这个是因为CSocket类的父类是CASyncSocket,而CASyncSocket的父类就是CObject类,而CObject类就是Microsoft Foundation Class Library的主要基类。

            (2)谁负责CSocket类的字节序转换功能呢?

                     CSocket类依据MFC序列化协议( serialization protocol)通过MFC的一个CArchive对象,从一个socket object接收数据或发送数据。同时CArchive对象和CSocket对象联合可以通过使用CString类来实现Unicode字符与多字符之间的转换。

【注】有关socket的知识有很多,大家可以慢慢的研究MSDN里面有很多详细的解释,我想如果你能不浮躁,静下心来好好学习,你一定能精通这些技术。

 

posted on 2012-02-11 11:15  draeag  阅读(275)  评论(0编辑  收藏  举报