TimeMachine

导航

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

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的连接,未实现,如果有兴趣可以在此基础上进行完善。

 
                                                         
马进
                                                  email:majinwj@21cn.com
                                                  tel:0312-5063275-312

posted on 2005-05-12 17:05  饽饽  阅读(1378)  评论(0编辑  收藏  举报