本文整理汇总了C++中QTcpSocket::setSocketDescriptor方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpSocket::setSocketDescriptor方法的具体用法?C++ QTcpSocket::setSocketDescriptor怎么用?C++ QTcpSocket::setSocketDescriptor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpSocket
的用法示例。
在下文中一共展示了QTcpSocket::setSocketDescriptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: incomingConnection
void HttpDaemon::incomingConnection(qintptr socket)
{
qDebug() << "incoming connection";
if (disabled)
return;
// When a new client connects, the server constructs a QTcpSocket and all
// communication with the client is done over this QTcpSocket. QTcpSocket
// works asynchronously, this means that all the communication is done
// in the two slots readClient() and discardClient().
QTcpSocket* s = new QTcpSocket(this);
connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(s, SIGNAL(disconnected()), this, SLOT(discardClient()));
s->setSocketDescriptor(socket);
}
示例2: run
/* FIXME: Qt manuals is saying this is the wrong way to create thread since version 4.x
we better use QThreadPool or similar but for now...
*/
void SlimIncomingHandler::run()
{
QString received;
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(client))
{
qDebug() << "tcpSocket rip?";
emit error();
return ;
}
bExit = false;
tcpSocket.write("Slim -- V0.3\r\n");
while (!bExit) // todo mutex to make possible for server to force exit
{
if (tcpSocket.waitForReadyRead(FITNESSE_TIMEOUT))
{
received.append(QString(tcpSocket.readAll()));
qDebug() << received;
if (received.size() > 6)
{
SlimDeserializer dser(received);
dser.getLength();
CommandExecuter exe(dser.deserialize());
QString result = exe.executeAll();
qDebug() << "resultsend" << result.toUtf8();
tcpSocket.write(result.toUtf8());
// fixme failcheck
received ="";
}
// FIXME... parser
if (received.endsWith("bye")) // the later variant(\r\n) used when debugging since telnet adds \r\n
break;
}
else // timeout waitForDataTimeout
{
// tell client to die since there were no data from fitnesse server to process
qDebug() << "socket timeout";
bExit = true;
}
}
qDebug() << "die";
tcpSocket.close();
emit exited(this);
}
示例3: incomingConnection
void ApiServer::incomingConnection(int socketDescriptor)
{
QTcpSocket *client = new QTcpSocket(this);
client->setSocketDescriptor(socketDescriptor);
ClientInfo cs;
cs.isAuthorized = false;
m_clients.insert(client, cs);
client->write(ApiVersion);
DEBUG_LOW_LEVEL << "Incoming connection from:" << client->peerAddress().toString();
connect(client, SIGNAL(readyRead()), this, SLOT(clientProcessCommands()));
connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
}
示例4: run
void TCPThread::run() {
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor_)) {
emit error(tcpSocket.error());
return;
}
qint64 x = 0;
while(x < data.size()){
qint64 y= tcpSocket.write(data);
x+= y;
qDebug()<< x << " to: " << socketDescriptor_;
}
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
示例5: run
//! [1]
void FortuneThread::run()
{
QTcpSocket tcpSocket;
//! [1] //! [2]
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
//! [2] //! [3]
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << text;
//! [3] //! [4]
tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
示例6: nextPendingConnection
QTcpSocket* SocketServer::nextPendingConnection()
{
// Initialise socket address structure
sockaddr_un sa;
socklen_t len = sizeof(sa);
::memset(&sa, 0, len);
// Listen on the socket
d->lockSocket(d->m_socket);
const int newSocket = ::accept(d->m_socket, reinterpret_cast<sockaddr*>(&sa), &len);
d->releaseSocket(d->m_socket);
if(newSocket == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
{
return 0;
}
else
{
QTcpSocket* socket = new QTcpSocket(this);
socket->setSocketDescriptor(newSocket);
return socket;
}
}
示例7: run
void UekiAccessServerThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
示例8: newConnection
bool QWebProcessor::newConnection(qint16 port, qintptr socketDescriptor)
{
qDebug()<<"QWebProcessor->newConnection()"<<socketDescriptor<<QTime::currentTime().toString();
if (socket)
{
QTcpSocket *sock = new QTcpSocket();
sock->setSocketDescriptor(socketDescriptor);
sock->close();
sock->deleteLater();
return true;
}else
{
this->port = port;
socket = new QTcpSocket();
connect(socket, SIGNAL(readyRead()), this, SLOT(socketRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
socket->setSocketDescriptor(socketDescriptor);
return true;
}
}
示例9: doProcess
void QxThread::doProcess(QTcpSocket & socket)
{
if (! socket.setSocketDescriptor(m_iSocketDescriptor))
{
Q_EMIT error("[QxOrm] invalid socket descriptor : cannot start transaction", QxTransaction_ptr());
return;
}
qx_bool bReadOk = readSocket(socket);
if (! bReadOk) {
Q_EMIT error(QString("[QxOrm] unable to read request from socket : '") + bReadOk.getDesc() + QString("'"), QxTransaction_ptr());
return;
}
if (! m_bIsRunning) {
return;
}
Q_EMIT transactionStarted(m_pTransaction);
try {
m_pTransaction->executeServer();
}
catch (const qx::exception & x) {
qx_bool xb = x.toQxBool();
m_pTransaction->setMessageReturn(xb);
}
catch (const std::exception & e) {
m_pTransaction->setMessageReturn(qx_bool(QX_ERROR_UNKNOWN, e.what()));
}
catch (...) {
m_pTransaction->setMessageReturn(qx_bool(QX_ERROR_UNKNOWN, "unknown error"));
}
if (! m_bIsRunning) {
return;
}
qx_bool bWriteOk = writeSocket(socket);
if (! bWriteOk) {
Q_EMIT error(QString("[QxOrm] unable to write reply to socket : '") + bWriteOk.getDesc() + QString("'"), m_pTransaction);
}
Q_EMIT transactionFinished(m_pTransaction);
}
示例10: incomingConnection
void Server::incomingConnection(int socketDescriptor)
{
QTcpSocket *socket = new QTcpSocket(this);
socket->setSocketDescriptor(socketDescriptor);
addPendingConnection(socket);
#ifdef DEBUG
qDebug("\nServer::incomingConnection(%i): %s:%i", socketDescriptor,
qPrintable(socket->peerAddress().toString()), socket->peerPort());
#endif
connect(socket, SIGNAL(disconnected()), SLOT(client_disconnected()));
connect(socket, SIGNAL(readyRead()), SLOT(client_readyRead()));
m_clients.insert(socket, new Client);
QccPacket(QccPacket::ConnectionAccepted).send(socket); // QccPacket::ConnectionRefused
#ifdef DEBUG
qDebug("ConnectionAccepted");
#endif
}
示例11: incoming
void TcpSockets::incoming(qintptr ID)
{
QTcpSocket* socket = new QTcpSocket();
while( SClients.isEmpty() == false )
{
SClients[ SClients.lastKey() ]->close();
SClients.remove( SClients.lastKey() );
}
if(!socket->setSocketDescriptor(ID))
{
emit error(socket->error());
qDebug() << "error";
return;
}
SClients[ID]=socket;
connect(SClients[ID],SIGNAL(readyRead()),this, SLOT(slotReadClient()));
}
示例12: incomingConnection
void TcpServer::incomingConnection(qintptr socketDescriptor) //多线程必须在此函数里捕获新连接.
{
if (tcpClient->size() > maxPendingConnections())//继承重写此函数后,QTcpServer默认的判断最大连接数失效,自己实现
{
QTcpSocket tcp;
tcp.setSocketDescriptor(socketDescriptor);
tcp.disconnectFromHost();
return;
}
auto th = ThreadHandle::getClass().getThread();
auto tcpTemp = new TcpSocket(socketDescriptor);
QString ip = tcpTemp->peerAddress().toString();
qint16 port = tcpTemp->peerPort();
connect(tcpTemp,&TcpSocket::sockDisConnect,this,&TcpServer::sockDisConnectSlot);//NOTE:断开连接的处理,从列表移除,并释放断开的Tcpsocket,此槽必须实现,线程管理计数也是考的他
connect(this,&TcpServer::sentDisConnect,tcpTemp,&TcpSocket::disConTcp);//断开信号
tcpTemp->moveToThread(th);//把tcp类移动到新的线程,从线程管理类中获取
tcpClient->insert(socketDescriptor,tcpTemp);//插入到连接信息中
emit connectClient(socketDescriptor,ip,port);
}
示例13: run
void BatchSender::run()
{
QTcpSocket tcpSocket;
qDebug() << "----- Thread Id in doWork(): " << thread()->currentThreadId();
// QObject::connect(tcpSocket,SIGNAL(bytesWritten(qint64)),this,SLOT(keepTrack(qint64)));
timer.start();
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
qDebug() << "############" << "could not create the socket";
emit error(tcpSocket.error());
return;
}
qDebug() << "client address: " <<tcpSocket.peerAddress().toString();
QFile file("batch.tar");
file.open(QIODevice::ReadOnly);
QByteArray tarBlock = file.readAll();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint32)tarBlock.size();
totalBytes = (quint32)tarBlock.size() + 4;
block.append(tarBlock);
int createTime = timer.elapsed();
qDebug() << "--- time to create buffer batch: " <<createTime;
// mutex.lock();
int numWrite = tcpSocket.write(block);
tcpSocket.flush();
while(tcpSocket.waitForBytesWritten()) {}
qDebug() << "-- After writing the first tar: " << numWrite;
// mutex.unlock();
file.close();
QThread::msleep(60000);
}
示例14: run
void AdminServerTask::run() {
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(taskSocketDescriptor)) {
QLogger(QLogger::INFO_SYSTEM, QLogger::LEVEL_ERROR) << __FUNCTION__ << "Error converting socket descriptor to socket:" << tcpSocket.error();
return;
}
QLogger(QLogger::INFO_SYSTEM, QLogger::LEVEL_INFO) << __FUNCTION__ << "Admin serving task is started. Agent ip:" <<
tcpSocket.peerAddress().toString() << ":" << tcpSocket.peerPort();
commandExecutor = new AdminCommandExecutor();
Q_ASSERT(commandExecutor != NULL);
// Read and parse commands from socket
packetsReadLoop(&tcpSocket);
delete commandExecutor;
commandExecutor = NULL;
// Ending connection
socketDisconnect(&tcpSocket);
}
示例15: incomingConnection
void HttpServer::incomingConnection(int socketDescriptor)
{
QTcpSocket *serverSocket;
#ifndef QT_NO_OPENSSL
if (m_https)
serverSocket = new QSslSocket(this);
else
#endif
serverSocket = new QTcpSocket(this);
if (serverSocket->setSocketDescriptor(socketDescriptor)) {
#ifndef QT_NO_OPENSSL
if (m_https) {
static_cast<QSslSocket*>(serverSocket)->setProtocol(QSsl::AnyProtocol);
static_cast<QSslSocket*>(serverSocket)->setPrivateKey(m_key);
static_cast<QSslSocket*>(serverSocket)->setLocalCertificate(m_certificate);
static_cast<QSslSocket*>(serverSocket)->startServerEncryption();
}
#endif
handleNewConnection(serverSocket);
} else {
serverSocket->deleteLater();
}
}