首页| 论坛| 消息

标题:incomingConnection ( int socketDescriptor )
作者:hanb99
日期:2006-10-12 15:43
内容:

qt中的一个方法:
void QTcpServer::incomingConnection ( int socketDescriptor ):
The base implementation creates a QTcpSocket, sets the socket descriptor and then stores the QTcpSocket in an internal list of pending connections.
它的基本功能是创建一个QTcpSocket,设置socket descriptor 并且将QTcpSocket保存到一个内部列表中
(汗!!E文不好,大概是这个意思吧???),我想问一个我如果想用这个QTcpSocket怎么才能把它取出来。
另:
http://www.qtopia.org.cn/phpBB2/viewtopic.php?t=630
有没有高人指点一下这个问题,先谢谢了!!!


#1 [andelie 10-12 19:59]
刚好我也在研究类似的问题,我是这么解决的:
incomingConnection()是服务器端被客户端连接上后自动调用的函数。如果你想用tcpSocket的话可以用如下方法:用建立服务的server调用nextPendingConnection()这个方法可以得到tcpSocket。
#2 [hanb99 10-12 20:44]
[1 楼]::incomingConnection()是服务器端被客户端连接上后自动调用的函数。如果你想用tcpSocket的话可以用如下方法:用建立服务的server调用nextPendingConnection()这个方法可以得到tcpSocket。
-------------------------------------------
可以说的再清楚些吗?
客户端:
connect(sendButton, SIGNAL(clicked()), this, SLOT(connectServer()));
。。。。。
void Client::connectServer()
{ QString a;
a = "192.168.102.51";
tcpSocketClient->abort();
tcpSocketClient->connectToHost(QHostAddress(a), 10000);
}
服务器端:
void FortuneServer::incomingConnection(int socketDescriptor)
{
FortuneThread *thread = new FortuneThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
..................
void FortuneThread::run()
{
}
当线程调用后在run()的什么地方调用server呀,在FortuneThread::FortuneThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)(这个是线程的构造函数),它不用QTcpServer就自动的建完了socket,不用我再调用nextPendingConnection()这个了。写的再细些好吗???跪谢了!!!
#3 [andelie 10-13 06:58]
大家一起研究嘛。
我是这样理解的:在你建立服务器端的时候肯定是要用到listen去监听host和port的,通常是server->listen(host,port),然后用server->nextPendingConnection()就可以得到socket了。上面你写的代码里没有用QTcpServer就自动的建完了socket是因为程序在别的地方已经调用了。

回复 发表
主题 版块