qt(跨平台)网络编程问题

troyyuan 发布于 2013/12/06 17:19
阅读 1K+
收藏 0
最新在捣鼓一个开源的聊天工具,在跨平台网络编程时,遇到了问题,求指点!!!
问题:
使用TCP协议, 在 windows平台下, 用 QT 写的client 与 linux平台下, 用 c 写的server, 进行 数据收发 时的异常现象?(能够建立连接)

源代码 在 https://github.com/yuankang

问题1: client发 server收 的数据不一样?
产生次问题的原因是, qt在发送数据的时候,在数据的前面添加了4个字节的数据, server接收的时候要把数据前移四个字节,

从 "server收 处理 发送消息给 client" 标红色的内存打印可以证实. 为什么要这个样子发数据呢???


问题2: server发 client收到的显示不出来?
这是为什么呢???是我代码写的有问题吗???
/****************** client发 ******************/
void login::sendMessage()
{
 QByteArray block;
 QDataStream out(&block, QIODevice::WriteOnly);
 //设置数据流版本,这里要和服务器端相同
 out.setVersion(QDataStream::Qt_5_0);
 //依次写入请求类型,用户名,密码等信息
 out<<":2:troy:troy:::";
 out.device()->seek(0);
 out<<quint16(block.size() - sizeof(quint16));
 tcpSocket.write(block);
}
/****************** server收 处理 发送消息给 client ******************/
[22.17:02:26.503]:cs->src/cs_server.c->main:00305 --> cs start 0.0.0.0 8888
[22.17:02:39.965]:cs->src/cs_server.c->cs_accept:00141 --> accept client 5 from 192.168.20.20 port 6165
[22.17:02:40.007]:cs->src/cs_server.c->cs_routine:00185 --> receive 20 bytes from 5. //接收到了20个字节的数据,实际发送的是15个字节的数据 :2:troy:troy:::
[22.17:02:40.007]:cs->src/cs_server.c->cs_routine:00188 --> rwbuf->rbuf.len=20, rwbuf->rbuf.data=0x18fb3c0, BUF= 00 12 00 10 3A 32 3A 74 72 6F 79 3A 74 72 6F 79 3A 3A 3A 00
[22.17:02:40.007]:cs->src/cs_server.c->cs_routine:00191 --> rwbuf->rbuf.len=16, rwbuf->rbuf.data=:2:troy:troy:::
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00020 --> ***********************************
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00021 --> req->req_type=2
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00022 --> req->name=troy
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00023 --> req->passwd=troy
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00024 --> req->buddy_name=(nil)
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00025 --> req->content=(nil)
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00026 --> req->datetime=(nil)
[22.17:02:40.007]:cs->src/cs_sqlite.c->request_dump:00027 --> ***********************************
[22.17:02:40.007]:cs->src/cs_sqlite.c->sql_login:00305 --> query_line=select * from users where name='troy' and passwd='troy'
[22.17:02:40.008]:cs->src/cs_sqlite.c->sql_login:00321 --> query_line=update users set fd=5 where name='troy'
[22.17:02:40.010]:cs->src/cs_sqlite.c->sql_login:00333 --> query_line=select troy.name, users.fd from troy,users where troy.name=users.name
[22.17:02:40.010]:cs->src/cs_sqlite.c->sql_login:00341 --> wbuf->len=7, wbuf->data=:zyy--1
[22.17:02:40.010]:cs->src/cs_sqlite.c->sql_login:00345 --> wbuf->len=8, wbuf->data=:zyy--1*
[22.17:02:40.010]:cs->src/cs_sqlite.c->sql_login:00350 --> query_line=select * from troy_offline
[22.17:02:40.010]:cs->src/cs_sqlite.c->sql_login:00358 --> wbuf->len=8, wbuf->data=:zyy--1*
[22.17:02:40.010]:cs->src/cs_sqlite.c->sql_login:00363 --> query_line=delete from troy_offline
[22.17:02:40.013]:cs->src/cs_sqlite.c->sql_login:00381 --> user troy login success.
[22.17:02:40.013]:cs->src/cs_server.c->cs_routine:00205 --> send :zyy--1* 8 bytes to 5. //发送8个字节的数据 :zyy--1*
[22.17:02:42.109]:cs->src/cs_server.c->cs_routine:00180 --> close 5 client success.


/****************** client收 *************************/

void login::receiveMessage()
{
 QDataStream in(&tcpSocket);
 //设置数据流版本,这里要和服务器端相同
 in.setVersion(QDataStream::Qt_5_0);
 if (nextBlockSize == 0) {
 //判断接收的数据是否有两字节,如果没有,则返回继续接收数据
 qDebug() << tcpSocket.bytesAvailable();
 if (tcpSocket.bytesAvailable() < sizeof(quint16))
 return;
 in >> nextBlockSize;
 }
 qDebug() << nextBlockSize;
 QString response;
 in >> response;
 qDebug() << response;
 qDebug() << response.data();
 //ui->messageLabel->setText(response);
}
加载中
0
x
xingkongws
大神,我也在做这个,,能不能把你的代码给我看看的呢
0
DeathNote2014
DeathNote2014

大神,请问你知道如何用socket实现在互联网通信吗?或者局域网内跨网段通信

troyyuan
troyyuan
Linux Socket Programming.pdf
返回顶部
顶部