IM over Socket Between Windows Mobile Devices

    在《利用WiFi在Windows Mobile上建立Ad-hoc网络》一文中,讲述了利用WiFi在Windows Mobile上建立Ad-hoc网络的方法,在Windows Mobile 6与Windows Mobile 2003se for Pocket PC给出了演示。那么,点对点的Ad-hoc网络能够完成什么功能呢?一方面,它可以用来消息的实时发送与接收,也就是俗称IM(Instant Messager);另一方面,可以用来传送文件。这篇文章先来讲讲如何利用Socket来实现Windows Mobile设备之间的IM(其实Windows Mobile设备与PC同步以后,可以通过Socket与PC实现通信,具体可以参考Fox的Blog:Windows Mobile的简单IM程序(Socket via C# ))。

    1. 应用分为Server端和Client端,写应用程序的时候,参考了《EVC高级编程及其应用开发》的第9章和第11章,该书的源代码可以在http://www.waterpub.com.cn/上下载。

    2. 开发环境为Visual Studio 2008 + Windows Mobile 2003 for Pocket PC SDK + Windows Mobile 6 Pro SDK,使用MFC来开发。

    3. 新建项目的时候,在Advanced Features选项中,选中Windows sockets,以添加对Socket的支持。如下图1所示:

image

图1: 添加Windows sockets支持

    4. 设计Server端和Client端简单的UI,如下图2所示:

image image

图2: Server端和Client端简单的UI

    5. 通过Add Variable为控件添加成员变量。

    6. 参考《EVC高级编程及其应用开发》第11章的内容,进行代码编写。Server端和Client端的文件组成如下图3所示:

image image

图3: Server端和Client端的文件组成

    7. 在Client端,CTCPClient_CE类完成TCP客户端操作的封装,然后在对话框中调用该类与TCP服务器建立连接,并进行数据传输,最后再断开与服务器的连接。在Server端,CTCPServer_CE类负责监听客户端的连接,一旦客户端有连接请求,它就负责接收此连接,并创建一个新的CTCPCustom_CE对象与客户端进行通讯。而CTCPServer_CE类接着监听客户端的连接请求。

    8. 代码测试。按照《利用WiFi在Windows Mobile上建立Ad-hoc网络》的方法,利用WiFi在Windows Mobile上建立Ad-hoc网络。在Windows Mobile 2003se for Pocket PC设备上运行Server端程序,在Windows Mobile 6 Professional设备上运行Client端程序,如下图4所示:

Screen01 Screen02

图4: 设备测试截图

注:Server端和Client端均采用异步Socket方式通信。

最后,放上源代码,从这里下载:TCPTest.rar,希望对大家有用。

 

参考链接:

1. 《利用WiFi在Windows Mobile上建立Ad-hoc网络

2. 《Windows Mobile的简单IM程序(Socket via C# )

3. 《EVC高级编程及其应用开发》

posted on 2010-05-25 22:17  施炯  阅读(2346)  评论(1编辑  收藏  举报