gsoap实现C#(wpf)客户端调用C++服务器端_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > gsoap实现C#(wpf)客户端调用C++服务器端

gsoap实现C#(wpf)客户端调用C++服务器端

 2013/11/4 17:35:38  小雨or小玄  博客园  我要评论(0)
  • 摘要:首先建立C++服务器端,这里需要使用到gsoap第一步:自己在桌面建立一个文件夹,命名为add,打开add文件夹,新建txt文档,命名为add.txt,拷贝下面的代码到add.txt,点击保存,然后修改add.txt后缀名为add.h//gsoapnsservicename:add//gsoapnsservicenamespace:http://localhost/add.wsdl//gsoapnsservicelocation:http
  • 标签:C# 实现 c++ 客户 服务器 客户端 服务

首先建立C++服务器端,这里需要使用到gsoap

第一步:自己在桌面建立一个文件夹,命名为add,打开add文件夹,新建txt文档,命名为add.txt,拷贝下面的代码到add.txt,点击保存,然后修改add.txt后缀名为add.h

//gsoap ns service name: add

//gsoap ns service namespace: http://localhost/add.wsdl

//gsoap ns service location: http://localhost

//gsoap ns service executable: add.cgi

//gsoap ns service encoding: encoded

//gsoap ns schema namespace: urn:add

int ns__add( int num1, int num2, int* sum );

第二步:从gsoap文件里面拷贝soapcpp2.exe到add文件夹里面,然后打开cmd命令行,输入:soapcpp2.exe add.h会生成许多文件,如图

 

第三步:新建Win32控制台应用程序,命名为:gsoapApplication_1_S;

添加wsock32.lib引用;

从add文件夹中拷贝如下图所示文件到该项目中,其中黑圈里面的两个项目来自gsoap拷贝:

第四步:修改gsoapApplication_1_S.cpp,其代码如下:

// gsoapApplication_1_S.cpp : 定义控制台应用程序的入口点。

//

#include "add.h"

#include "add.nsmap"

int main(int argc, char* argv[])

{  

int m, s;

/* master and slave sockets */    

struct soap add_soap;    

soap_init(&add_soap);    

//soap_set_namespaces(&add_soap, add_namespaces);    

m = soap_bind(&add_soap, NULL, 4567, 100);        

if (m < 0)        

{            

soap_print_fault(&add_soap, stderr);            

exit(-1);        

}                

fprintf(stderr, "Socket connection successful: master socket = %d\n", m);        

for ( ; ; )        

{            

s = soap_accept(&add_soap);            

if (s < 0)            

{                

soap_print_fault(&add_soap, stderr);                

exit(-1);            

}            

fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);                        

soap_serve(&add_soap);//该句说明该server的服务            

soap_end(&add_soap);         }

    return 0; }

//server端的实现函数与addmethod.h中声明的函数相同,但是多了一个当前的soap连接的参数

int ns__add(struct soap *add_soap, int num1, int num2, int *sum)

{    

*sum = num1 + num2;    

return SOAP_OK;

}

 第五步:编译成功,打开cmd命令行,输入gsoapApplication_1_S.exe 4567打开主服务,如图

第六步:打开IE浏览器,输入:http://localhost:4567 回车,如图所示,服务器端已经配置成功

 

配置C#(wpf)客户端,使用到wsdl.exe,在本地搜索一下,没有的话需要在网上下载一个,我是在网上下载的,下载很简单

第一步:拷贝wsdl.exe到add文件夹,打开vs自带的黑屏命令行工具,输入:wsdl /out add.wsdl回车,会生成.cs文件,文件里面包含多线程、代理等信息,如下图

 

第二步:新建WPF应用程序,命名为gsoapApplication_1_C_WPF,拷贝add.cs,add.wsdl两个文件到该工程中,修改add.cs文件代码的"http://localhost/add.cgi"为"http://localhost:4567/add.cgi",如图所示:

 

第三步:MainWindow.xaml界面代码如下:

<Window x:Class="gsoapApplication_1_C_WPF.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        

Title="MainWindow" Height="350" Width="525">    

<Grid>        

<GroupBox Header="Add" HorizontalAlignment="Left" Margin="52,43,0,0" VerticalAlignment="Top" Height="236" Width="412">            

<Grid>                

<Grid.RowDefinitions>                    

<RowDefinition/>                    

<RowDefinition/>                    

<RowDefinition/>                    

<RowDefinition/>                

</Grid.RowDefinitions>                

<Grid.ColumnDefinitions>                    

<ColumnDefinition/>                    

<ColumnDefinition/>                

</Grid.ColumnDefinitions>                

<Grid Grid.Row="0" Grid.Column="0">                    

<Label Content="number 1:" HorizontalAlignment="Right"  VerticalAlignment="Center"/>                 </Grid>                

<Grid Grid.Row="0" Grid.Column="1">                    

<TextBox Name="num1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Height="30"/>                

</Grid>                

<Grid Grid.Row="1" Grid.Column="0">                    

<Label Content="number 2:" HorizontalAlignment="Right"  VerticalAlignment="Center"/>                 </Grid>                

<Grid Grid.Row="1" Grid.Column="1">                    

<TextBox Name="num2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Height="30"/>                

</Grid>                

<Grid Grid.Row="2" Grid.Column="0">                    

<Label Content="result----:" HorizontalAlignment="Right"  VerticalAlignment="Center"/>                 </Grid>                

<Grid Grid.Row="2" Grid.Column="1">                    

<TextBox Name="result" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Height="30"/>                

</Grid>                

<Grid Grid.Row="3" Grid.Column="1">                    

<Button Name="submitAdd" Content="计算" Click="submitAdd_Click" Width="80" Height="30"/> </Grid>            

</Grid>                    

</GroupBox>

</Grid>

</Window>

 第四步:MainWindow.xaml.cs代码如下:

using System;

using System.Collections.Generic;

using System.Linq; using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

namespace gsoapApplication_1_C_WPF

{    

/// <summary>    

/// Interaction logic for MainWindow.xaml    

/// </summary>    

public partial class MainWindow : Window    

{        

public MainWindow()        

{            

InitializeComponent();                    

}

private void submitAdd_Click(object sender, RoutedEventArgs e)        

{            

add myadd = new add();            

int num1 = Convert.ToInt32(this.num1.Text);            

int num2 = Convert.ToInt32(this.num2.Text);            

int? sum = 0;            

bool sumSpecified = false;                         

myadd.Calladd(num1, num2,out sum, out sumSpecified);            

this.result.Text = sum.ToString();        

}    

}

}

 第五步:编译,生成,结果如图所示(不要忘了打开主服务哦~):

 

 

 

 

 

发表评论
用户名: 匿名