本文整理汇总了C++中QTcpSocket::peerAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpSocket::peerAddress方法的具体用法?C++ QTcpSocket::peerAddress怎么用?C++ QTcpSocket::peerAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpSocket
的用法示例。
在下文中一共展示了QTcpSocket::peerAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: recieveConnection
void Server::recieveConnection()
{
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
QString client_ip = clientConnection->peerAddress().toString();
quint32 client_ip_int = clientConnection->peerAddress().toIPv4Address();
emit write_message(tr("New connection from IP: %1").arg(client_ip));
if (sockets->contains(client_ip_int)) {
QTcpSocket *oldClientConnection = (QTcpSocket*) sockets->value(client_ip_int);
if (oldClientConnection && oldClientConnection->state() != QAbstractSocket::UnconnectedState) {
oldClientConnection->disconnectFromHost();
}
sockets->remove(client_ip_int);
}
sockets->insert(client_ip_int, clientConnection);
connect(clientConnection, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recieveData()));
connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
}
示例2: onNewConnection
void TCPSrc::onNewConnection()
{
while(m_tcpServer->hasPendingConnections()) {
QTcpSocket* connection = m_tcpServer->nextPendingConnection();
connect(connection, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
switch(m_sampleFormat) {
case FormatNFM:
case FormatSSB: {
quint32 id = (FormatSSB << 24) | m_nextSSBId;
MsgTCPSrcConnection* msg = MsgTCPSrcConnection::create(true, id, connection->peerAddress(), connection->peerPort());
m_nextSSBId = (m_nextSSBId + 1) & 0xffffff;
m_ssbSockets.push_back(Socket(id, connection));
msg->submit(m_uiMessageQueue, (PluginGUI*)m_tcpSrcGUI);
break;
}
case FormatS16LE: {
quint32 id = (FormatS16LE << 24) | m_nextS16leId;
MsgTCPSrcConnection* msg = MsgTCPSrcConnection::create(true, id, connection->peerAddress(), connection->peerPort());
m_nextS16leId = (m_nextS16leId + 1) & 0xffffff;
m_s16leSockets.push_back(Socket(id, connection));
msg->submit(m_uiMessageQueue, (PluginGUI*)m_tcpSrcGUI);
break;
}
default:
delete connection;
break;
}
}
}
示例3: recieveConnection
void Server::recieveConnection()
{
if (is_init_connection) return;
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
QString client_ip = clientConnection->peerAddress().toString();
quint32 client_ip_int = clientConnection->peerAddress().toIPv4Address();
emit write_message(tr("New connection from IP: %1").arg(client_ip));
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
if (client_ip_int == QHostAddress(remoteIPAddress).toIPv4Address() || is_config_mode) {
if (remote_server_socket && clientConnection != remote_server_socket) {
delete remote_server_socket;
}
remote_server_socket = clientConnection;
connect(clientConnection, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recieveData()));
connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
} else {
clientConnection->abort();
}
}
示例4: incomingConnection
void TcpServer::incomingConnection(qintptr socketDescriptor)
{
QTcpSocket *localSocket = new QTcpSocket;
localSocket->setSocketDescriptor(socketDescriptor);
if (!isLocal && autoBan && Common::isAddressBanned(localSocket->peerAddress())) {
emit debug(QString("A banned IP %1 attempted to access this server")
.arg(localSocket->peerAddress().toString()));
localSocket->deleteLater();
return;
}
//timeout * 1000: convert sec to msec
TcpRelay *con = new TcpRelay(localSocket,
timeout * 1000,
serverAddress,
ep,
isLocal,
autoBan,
auth);
conList.append(con);
connect(con, &TcpRelay::info, this, &TcpServer::info);
connect(con, &TcpRelay::debug, this, &TcpServer::debug);
connect(con, &TcpRelay::bytesRead, this, &TcpServer::bytesRead);
connect(con, &TcpRelay::bytesSend, this, &TcpServer::bytesSend);
connect(con, &TcpRelay::latencyAvailable,
this, &TcpServer::latencyAvailable);
connect(con, &TcpRelay::finished, this, [=]() {
if (conList.removeOne(con)) {
con->deleteLater();
}
});
con->moveToThread(threadList.at(workerThreadID++));
workerThreadID %= totalWorkers;
}
示例5: handleDisconnection
void NetMaster::handleDisconnection()
{
QTcpSocket * socket = (QTcpSocket*)sender();
m_sockets.removeAll(socket);
m_connectedClients.removeRow(m_connectedClients.match(
m_connectedClients.index(0, 0),
Qt::DisplayRole,
socket->peerAddress().toString()).at(0).row());
qDebug() << "Slave disconnected from" << socket->peerAddress().toString();
}
示例6: slotNewConnection
void Server::slotNewConnection()
{
QTcpSocket* pClientSocket = ptcpServer->nextPendingConnection();
connect(pClientSocket, SIGNAL(disconnected()), pClientSocket, SLOT(deleteLater()));
connect(pClientSocket, SIGNAL(readyRead()), this, SLOT(slotReadClient()));
QString str = "Server Response: Connected!!";
QString* pstr = &str;
sendToClient(pClientSocket, *pstr);
QString buf = "New client connected: Новий клієнт" + pClientSocket->peerAddress().toString();
emit signal_display(buf);
clients.insert(pClientSocket->peerAddress().toString(), pClientSocket);
}
示例7: handleConnection
void NetMaster::handleConnection()
{
QTcpSocket * socket = m_server->nextPendingConnection();
connect(socket, SIGNAL(disconnected()), this, SLOT(handleDisconnection()));
connect(socket, SIGNAL(readyRead()), this, SLOT(dataReady()));
m_sockets.append(socket);
m_connectedClients.appendRow(new QStandardItem(socket->peerAddress().toString()));
qDebug() << "New connection from" << socket->peerAddress().toString();
// Send initial packets:
NetPacket("INIT_DONE").writeOut(socket);
}
示例8: sendResponse
void Server::sendResponse() {
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
stringstream peerStream;
peerStream << clientConnection->peerAddress().toString().toStdString()
<< ":"
<< clientConnection->peerPort();
string peer = peerStream.str();
if (clientConnection->waitForReadyRead(10000)) {
clientConnection->readLine(readBuffer, BUFFER_LEN);
string message(readBuffer);
string deviceId = "";
if (message.length() > 4) {
deviceId = message.substr(4);
}
if (message.find("GET ", 0, 4) == 0) {
string response = processGetOpertion(peer, deviceId);
clientConnection->write(response.c_str());
} else if (message.find("ADD ", 0, 4) == 0) {
bool added = processAddOpertion(deviceId);
if (added) {
clientConnection->write("ADDED");
} else {
clientConnection->write("NOT ADDED");
}
}
}
clientConnection->disconnectFromHost();
}
示例9: process
/* perform the operations for a frame:
* - check to see if the connections are still alive (checkAlive)
* - this will emit connectionsChanged if there is a any change in the connection status
* - grab a text stream of the current model data ( stream << *subjectList )
* - put the text stream on the wire s->write(...)
*/
void MyServer::process()
{
stopProfile("Other");
working = true;
startProfile("checkAlive");
int alive = checkAlive();
stopProfile("checkAlive");
if(alive > 0)
{
startProfile("Serve");
count++;
QString buffer;
QTextStream stream(&buffer);
// The following operation is threadsafe.
startProfile("Fetch");
subjectList->read(stream, true);
stopProfile("Fetch");
startProfile("Wait");
listMutex.lock();
stopProfile("Wait");
// for each connection
for(QList<ServerConnection *>::iterator i = connections.begin(); i != connections.end(); i++)
{
QTcpSocket *s = (*i)->socket;
if(s->state() != QAbstractSocket::ConnectedState) continue;
QString d = QString("%1\nEND\r\n").arg(buffer);
startProfile("Write");
int written = s->write(d.toUtf8());
stopProfile("Write");
if(written == -1)
{
emit outMessage(QString(" Error writing to %1").arg(s->peerAddress().toString()));
}
else
{
s->flush();
}
}
listMutex.unlock();
stopProfile("Serve");
}
working = false;
startProfile("Other");
}
示例10: newConnection
void ServerSktTcp::newConnection()
{
QTcpServer* server = qobject_cast<QTcpServer*>(sender());
if (!server) return;
QTcpSocket* client = server->nextPendingConnection();
while (client)
{
Conn* conn = new Conn;
if (!conn)
{
client->deleteLater();
}
else
{
client->setProperty(PROP_CONN, qVariantFromValue((void*)conn));
conn->client = client;
conn->key = TK::ipstr(client->peerAddress(),client->peerPort(), true);
connect(client, SIGNAL(readyRead()), this, SLOT(newData()));
connect(client, SIGNAL(destroyed(QObject*)), this, SLOT(close(QObject*)));
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
connect(client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
setCookie(conn->key, conn);
}
client = server->nextPendingConnection();
}
}
示例11: removeClosedConn
/**
This method will be invoked by Qt framework when a client connection gets
disconnected.
*
@param: none
*
@return: none
*******************************************************************************/
void CuteHttpServer::removeClosedConn()
{
// who sent the Qt signal?
QTcpSocket* caller = dynamic_cast<QTcpSocket*>(sender());
vector<QTcpSocket*>::iterator iter;
QTcpSocket* conn = 0;
if(caller == 0)
{
// 1. invoked by Qt on shutdown!
// -- find first closing conn:
for(iter = m_connections.begin(); iter != m_connections.end(); iter++)
{
// empty slot?
if(*iter == 0) continue;
if((*iter)->state() == QAbstractSocket::UnconnectedState)
{
conn = *iter;
break;
}
}
}
else
{
// 2. normal case, client exited, TCP object called back
iter = find(m_connections.begin(), m_connections.end(), caller);
if(iter != m_connections.end())
{
conn = *iter;
assert(conn == caller);
}
}
// found?
if(conn != 0)
{
assert(iter != m_connections.end());
int connID = delSavedConn(conn);
//delete conn; --> NO-GO!, Qt will need it later in this event handler!
conn->deleteLater(); // Qt will delte it!
if(TR_WEBIF || INFO_LVL)
{
TRACE_INFO3("http_srv: client conn closed, connection's IpAddr/clientId=",
conn->peerAddress().toString(), connID);
if(TR_WEBIF) TRACE2("http_srv: opened conns=", m_connCount);
}
// reset request's references:
for_each(m_requests.begin(), m_requests.end(), ResetConnIdIf(connID));
}
else
{
TRACE_ERR("http_srv: conn. closing signalled but no closed conn. found!");
}
}
示例12: newRISRequest
void ListenRISRequests::newRISRequest()
{
QTcpSocket *tcpSocket = m_tcpRISServer->nextPendingConnection();
QString risRequestData;
INFO_LOG("Rebuda peticio de la IP " + tcpSocket->peerAddress().toString());
if (tcpSocket->waitForReadyRead(TimeOutToReadData))
{
risRequestData = QString(tcpSocket->readAll());
INFO_LOG("Dades rebudes: " + risRequestData);
}
else
{
INFO_LOG("No s'ha rebut dades, error: " + tcpSocket->errorString());
}
INFO_LOG("Tanco socket");
tcpSocket->disconnectFromHost();
INFO_LOG("Faig delete del socket");
delete tcpSocket;
if (!risRequestData.isEmpty())
{
processRequest(risRequestData);
}
}
示例13: connected
void MrimConnection::connected()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
Q_ASSERT(socket);
SystemIntegration::keepAlive(socket);
bool connected = false;
switch (socket->state())
{
case QAbstractSocket::ConnectedState:
connected = true;
break;
default:
break;
}
QString address = Utils::toHostPortPair(socket->peerAddress(),socket->peerPort());
if (!connected)
{
debug()<<"Connection to server"<<qPrintable(address)<<"failed! :(";
return;
}
else
{
debug()<<"Connected to server"<<qPrintable(address);
if (socket == p->IMSocket()) //temp
{
sendGreetings();
}
}
}
示例14: processDeviceCommandSocket
void ServerCore::processDeviceCommandSocket()
{
if (!serverStart)
{
return;
}
QTcpSocket *socket = deviceCmdServer->nextPendingConnection();
QObject::connect(socket, &QTcpSocket::readyRead, [=]
{
QByteArray byteArray = socket->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(byteArray);
QJsonObject jsonObj = jsonDoc.object();
QVariantMap retParamMap = prepareDataForDevice(jsonObj);
TCP_REPLY_TYPE retType = (TCP_REPLY_TYPE)retParamMap[JSON_KEY_RETURN_TYPE].toInt();
QString retString = JsonGenerator::GenerateJsonReply(retType, retParamMap);
QString peerAddress = socket->peerAddress().toString();
socket->write(retString.toLatin1());
bool isSuccess = socket->waitForBytesWritten();
Q_EMIT dataWrittenToDevice(peerAddress, retString);
socket->disconnectFromHost();
});
QObject::connect(socket, &QTcpSocket::disconnected, [=]
{
socket->deleteLater();
});
}
示例15: run
void MessageDispatcherThread::run()
{
QTcpSocket socket;
socket.setSocketDescriptor(m_descriptor);
quint64 dataSize = 0;
while(!m_doQuit)
{
socket.waitForReadyRead();
if (dataSize == 0) {
QDataStream stream(&socket);
stream.setVersion(QDataStream::Qt_4_6);
if (socket.bytesAvailable() < sizeof(quint64))
continue;
stream >> dataSize;
}
if (socket.bytesAvailable() < dataSize)
continue;
emit gotMessage(socket.readAll(), socket.peerAddress());
break;
}