关闭

Gsoap开发之结构体数据输入(对Server而言)

标签: linux结构gsoap
593人阅读 评论(0) 收藏 举报
分类:

头文件Netinterface.h如下:

/**for ip change**/
struct ns__ipAddrEx
{
    char *ns__ipSrc;
    char *ns__ipDst;
};


struct ns__ipAddrExResponse
{
};
/**for ip change**/
int ns__PosSetting(struct ns__ipAddrEx* ipSetting_In,  struct ns__ipAddrExResponse* ipR_Out);

接口为
int ns__PosSetting(struct ns__ipAddrEx* ipSetting_In,  struct ns__ipAddrExResponse* ipR_Out);  第一个形参为输入(相对Server而言,下同),第二个形参为输出结构体。


找到soapcpp2执行文件,新建一个工程目录后进入,在terminal里执行$ ./soapcpp2 -c Netinterface.h  

-c 是生成纯C的SOAP代码架构的意思,这些可以参看soapcpp2命令说明。 OK,生成架构后即可开始码代码了。

Server端:

#define  myTable   "/etc/ipaddr"
#define  IP_LEN    4
#define  IP_BUF    100
#define  IPADDR_LEN   16

int
main (int argc, char *argv[])
{
	int m, s;
	struct soap mySoap;
	soap_init (&mySoap);

	mySoap.bind_flags = SO_REUSEADDR;
	mySoap.fget = http_get;
	soap_set_namespaces(&mySoap, namespaces);


	soap_set_recv_logfile(&mySoap, "./log/S_RECV.log");
	soap_set_sent_logfile(&mySoap, "./log/S_SENT.log");
	soap_set_test_logfile(&mySoap, "./log/S_TEST.log");


	if (argc < 2)
	{
		  printf ("usage: %s <server_port> \n", argv[0]);
		  exit (1);
	}
	else
	{
		  m = soap_bind (&mySoap, NULL, atoi (argv[1]), 100);
		  if (m < 0)
		  {
			  soap_print_fault (&mySoap, stderr);
			  exit (-1);
		  }
		  fprintf (stderr, "Socket connection successful: master socket = %d\n",m);
		  for (;;)
		  {
			  s = soap_accept (&mySoap);
			  if (s < 0)
			  {
				  soap_print_fault (&mySoap, stderr);
				  exit (-1);
			  }
			  fprintf (stderr,
					  "Socket connection successful: slave socket = %d\n", s);
			  soap_serve (&mySoap);    //该句说明该server的服务
			  soap_end (&mySoap);
		  }
	}
	return 0;
}
<pre name="code" class="cpp">int
ns__PosSetting(struct soap *mySoap, struct ns__ipAddrEx* ipSetting_In,  struct ns__ipAddrExResponse* ipR_Out)
{
	   int result = -1;

	   if(ipSetting_In->ns__ipSrc != NULL && ipSetting_In->ns__ipDst)
	   {
		   result = SOAP_OK;
		   printf("__PosSetting: srcIp--%s,  dstIp--%s\n", ipSetting_In->ns__ipSrc, ipSetting_In->ns__ipDst);
	   }

	   else
	   {

		   printf("server: PosSetting\n");
	   }

	   return result;
}





Client端:

<pre name="code" class="cpp">int
PosSetting(const char *server, struct ns__ipAddrEx* ipSetting_In,  struct ns__ipAddrExResponse* ipR_Out)
{
	struct soap mySoap;                         /* 创建mySoap()结构体 */
	int result = 0;
	soap_init (&mySoap);                        /* soap_init()*/
	soap_set_namespaces(&mySoap, namespaces);

	ipSetting_In->ns__ipSrc = soap_malloc(&mySoap, 16);
	memset(ipSetting_In->ns__ipSrc, '\0', 16);
	snprintf(ipSetting_In->ns__ipSrc, 16, "10.0.0.22");

	ipSetting_In->ns__ipDst = soap_malloc(&mySoap, 16);
	memset(ipSetting_In->ns__ipDst, '\0', 16);
	snprintf(ipSetting_In->ns__ipDst, 16, "10.0.0.21");

#ifdef  __DEBUG
	soap_set_recv_logfile(&mySoap, "./log/C_RECV.log");
	soap_set_sent_logfile(&mySoap, "./log/C_SENT.log");
	soap_set_test_logfile(&mySoap, "./log/C_TEST.log");
#endif

	soap_call_ns__PosSetting (&mySoap, server, "", ipSetting_In, ipR_Out); /* 调用soap_call_ns__PosSetting() */
	if (mySoap.error)
    {
			printf ("soap error:%d,%s,%s\n", mySoap.error,
          *soap_faultcode (&mySoap), *soap_faultstring (&mySoap));
			result = mySoap.error;
    }
	else
	{
		printf("PosSetting OK\n");

	}

	soap_end (&mySoap);                         /* 释放内存空间 */
	soap_done (&mySoap);
	return result;
}


最后,分别执行Server及Client端应用即可。

PS: 要善于利用wireshark网络抓包工具,对协议包进行分析,另外也要会用gsoap提供的debug log, 从里面可以看出很多问题。 我的一个内存出错问题还有结构体传送异常问题也都是通过它找到答案的。
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13655次
    • 积分:306
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    最新评论