惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 饽饽

C语言指针的用法 CRC 循环冗余校验码 (VC编程) 无线网卡或笔记本找不到网络的一个原因 (one reason of your laptop cannot find wireless network) 电脑能够上QQ,可以ping IP,玩网络游戏,就是不能上网,收发电子邮件的解决办法 有关学校的打油诗 CMPP2.0 协议SP端的·NET开发 C++中的虚函数(virtual function) - 饽饽 - 博客园 MIMO的文章 VC++调用其它语言编写的程序 - 饽饽 - 博客园 VC++调用其它程序2 - 饽饽 - 博客园 Vc编程基础 VC MFC基本控件的使用 VC基础教程 基于WINSOCK控件的VC网络通讯程序 用Winsock实现语音全双工通信使用 socket编程原理 VC常用技巧集锦 windows网络编程 用Winsock控件实现点对点通信
VISCAL C++下WINSOCK CONTROL类(ACTIVEX)的使用
饽饽 · 2005-05-12 · via 博客园 - 饽饽

VISCAL C++下WINSOCK CONTROL类(ACTIVEX)的使用

p 在编制局域网通讯程序中,利用Viscal C++的Socket类需要对网络通讯的机制和参数有较深的了解,需要较深的计算机知识。而在利用VB编程时,各种控件的使用比较简单,不需要许多的参数。在VISCAL C++中利用Winsock Control(ActiveX控件)的使用难度则介于上述二者之间,只需对少许一些参数做简单设置即可。如果在用VISCAL C++编制程序时需要进行网络通讯而又不想花费过多的时间,在VISCAL C++程序中插入Winsock Control(ActiveX控件)是一种合适的方法。笔者利用Winsock Control(ActiveX控件)编制一个简单的demo程序。

一、 Winsock Control(ActiveX控件)的一般使用步骤

1. 作为SERVER
Server : Construct-> Create-> Bind -> Listen-> Accept-> Send->Close ;
2. 作为Client
Client : Construct ->Create-> Connect-> Receive-> Close。
在使用Winsock Control(ActiveX控件)的过程中,Winsock Control类使用的一个特殊的数据类型为VARIANT结构,它的几个主要的域为:
  类型    值域       说明
vt=VT_BSTR   bstrVal                字符串类型
vt=VT_I2   Ival   整数类型
vt=VT_ERROR   scodee   错误代码
在使用之前,应对VARIANT变量的类型和值域进行赋值。一般情况下使用方法为:
VARIANT [variant name]
[variant name].vt=[variant type]
[variant name].[variant type]=[value]
如:
Cstring  m_host;
m_host="FSS0";
VARIANT  vtHost;
vtHost.vt=VT_BSTR;
vtHost.bstrVal=m_host.AllocSysString();

二、 程序实例

1.Server端利用ClassWizard建立一个ServerDemo程序,注意选择SDI,支持ActiveX,将View类设为CFormView类, 从Project->Add File->Compont and Control, 在对话框中选Registered ActiveX,选择Microsoft WinSock Control ,点击Insert按钮,依照缺省设置即可。这样在工程文件中会出现mswinsockcontrol类编辑ServerDemo程序的主对话框,可以看到在控件面版上出现mswinsockcontrol类的控件。将两个mswinsockcontrol类控件插入对话框,ID为IDC_WINSOCK1和IDC_WINSOCK2,生成变量m_server,m_connect.加入2个按钮,ID为ID_LISTEN,ID_SEND。
在ServerDemoView.h中,加入以下变量
 VARIANT localport;
 VARIANT localip;
 CString strport;
 CString strip;
 VARIANT vtCommand;
 CString m_command;
 VARIANT vtData;
 VARIANT vtType;
 VARIANT vtMaxlen;

在ServerDemoView .cpp的构造函数中加入:
 localport.vt=VT_BSTR;
 localip.vt=VT_BSTR;
 strport="4000";
 strip="128.1.25.240";//在程序中应将“128.1.25.240”该为自己server的IP
 vtCommand.vt=VT_BSTR;
 m_command="Server Send";
 vtData.vt=VT_BSTR;
 vtType.vt=VT_ERROR; 
利用ClassWizar映射ID_LISTEN的动作:OnListen(),在其中加入代码:
 localport.bstrVal=strport.AllocSysString();
 localip.bstrVal=strip.AllocSysString();
 m_server.Bind(localport, localip);
 m_server.Listen();
利用ClassWizar响应m_server的ConnectionRequest事件,OnConnectionRequestWinsock1(long requestID)
 m_connect.Accept(requestID);
利用ClassWizar映射ID_SEND的动作:OnSend(),在其中加入代码:
 vtCommand.bstrVal=m_command.AllocSysString();
 m_connect.SendData(vtCommand);
利用ClassWizar响应ID_WINSOCK2的DataArrival事件,OnDataArrivalWinsock2(long bytesTotal)
在其中加入代码:
 CString s;
 vtMaxlen.vt=VT_I2;
 vtMaxlen.iVal=bytesTotal;
 vtData.bstrVal=strport.AllocSysString();
 vtType.bstrVal=strport.AllocSysString();
 m_connect.GetData(&vtData,vtType,vtMaxlen);
 s=vtData.bstrVal;
 AfxMessageBox(s,MB_OK);
在DestroyWindow() 中加入:
 m_server.Close();

2.Client端利用ClassWizard建立一个ClientDemo程序,注意选择SDI,支持ActiveX,将View类设为CFormView类, 从Project->Add File->Compont and Control, 在对话框中选Registered ActiveX,选择Microsoft WinSock Control ,点击Insert按钮,依照缺省设置即可。这样在工程文件中会出现mswinsockcontrol类。编辑ClientDemo程序的主对话框,可以看到在控件面版上出现mswinsockcontrol类的控件。将1个mswinsockcontrol类控件插入对话框,ID为IDC_WINSOCK1,生成变量m_socket,加入2个按钮,ID为ID_CONNECT,ID_SEND。
在ClientDemoView.h中,加入以下变量
 CString m_host;
 CString m_port;
 VARIANT vtHost;
 VARIANT vtPort;
 VARIANT vtData;
 VARIANT vtType;
 VARIANT vtMaxlen;
 VARIANT vtCommand;
 CString m_command;  
在ClientDemo.cpp的构造函数中加入:
       m_host=_T("ttcea228xgh");//在程序中应将“ttcea228xgh”该为自己的server名
    //的主机名
 m_port=_T("4000");
 m_command=_T("Client send");
 vtHost.vt=VT_BSTR;
 vtPort.vt=VT_BSTR;
 vtData.vt=VT_BSTR;
 vtType.vt=VT_ERROR;
 vtCommand.vt=VT_BSTR;
利用ClassWizar映射ID_CONNECT的动作:OnConnect(),在其中加入代码:
 m_socket.SetRemoteHost("ttcea228xgh");//在程序中应将“ttcea228xgh”该为server
           //的主机名
 m_socket.SetRemotePort(4000);
 vtHost.bstrVal=m_host.AllocSysString();
 vtPort.bstrVal=m_port.AllocSysString();
 m_socket.Connect(vtHost,vtPort);
利用ClassWizar映射ID_SEND的动作:OnSend(),在其中加入代码:

 vtCommand.bstrVal=m_command.AllocSysString();
 m_socket.SendData(vtCommand);
利用ClassWizar响应ID_WINSOCK1的DataArrival事件,OnDataArrivalWinsock1(long bytesTotal)
 CString m_MBody;
 vtMaxlen.vt=VT_I2;
 vtMaxlen.iVal=bytesTotal;

vtData.bstrVal=m_host.AllocSysString();
 vtType.bstrVal=m_port.AllocSysString();
 m_socket.GetData(&vtData,vtType,vtMaxlen);
 m_MBody=vtData.bstrVal;
 AfxMessageBox(m_MBody,MB_OK);
在DestroyWindow() 中加入:
 m_socket.Close();

三.使用
 在上述两个程序编译完成之后,首先运行server程序,点击listen按钮,server进入监听状态,然后运行client程序,点击connect按纽后连接成功后,点击client的send按钮,server会出现信息框,点击server的send按钮,client会出现信息框。

 上述程序只是一个非常简单的demo,许多功能,如client退出后重新连接,多个client的连接,未实现,如果有兴趣可以在此基础上进行完善。

                                                  tel:0312-5063275-312

posted on 2005-05-12 17:05  饽饽  阅读(1421)  评论()    收藏  举报