您的位置:首页 > 运维架构 > Linux

〖Linux〗Qt+gsoap开发客户端程序,服务端地址设定的字符串转换处理

2013-12-21 13:31 706 查看
之所以写出来,是由于经常因为这个问题屡屡丢面子..

一般情况下,QString转换成(char*),我们一般直接使用:

  char *str = qstr->text().toLatin1().data();

当然这也本身就一点问题也没有,假如得到的str为"123",那么str就点4个字符的空间,最后一个字符是'\0';

可就是这么一个'\0',在设定gsoap的server的时候,它并不需要,被坑了好几回了,..

  出错的信息大概都是这样子的:

Starting /media/Study/Workspace/Qt/build-zigbeeClient-Desktop-Release/zigbeeClient...
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://localhost:8888"><SOAP-ENV:Body><ns:temp SOAP-ENV:encodingStyle="http://www.w3.org/2003/05/soap-encoding"><subdev>49</subdev></ns:temp></SOAP-ENV:Body></SOAP-ENV:Envelope>
接着,程序就死在这里..


  或者是这样子的出错信息:

SOAP 1.2 fault: SOAP-ENV:Sender[no subcode]
"Host not found"
Detail: get host by name failed in tcp_connect()


解决方法其实很容易的,就把最后一个字符'\0'去掉了就好了,我是这么写的:

char *custserver = ui->serverlineEdit->text().toLatin1().data();
char *custservertmp = (char *)malloc(strlen(custserver));       //to avoid the end of '\0'
strncpy(custservertmp,custserver,strlen(custserver));           //to avoid the end of '\0'


解析,假如custserver为"127.0.0.1:4567",使用strlen(custserver)得到的长度是14,而实际custserver占用的空间为15;

那么,再定义一个char *custservertmp,分配空间为14,使用strncpy,把custserver前边的14个字符复制给custservertmp;

然后,设定好服务端的地址,gsoap+Qt客户端运行很OK。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐