本文整理汇总了C++中QTcpSocket::localPort方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpSocket::localPort方法的具体用法?C++ QTcpSocket::localPort怎么用?C++ QTcpSocket::localPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpSocket
的用法示例。
在下文中一共展示了QTcpSocket::localPort方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: deleteClientSocket
// ----------------------------------------------------------------------
void DServer::deleteClientSocket()
{
QTcpSocket* pClientSocket = (QTcpSocket*)sender();
pClientSockets.remove(pClientSockets.indexOf(pClientSocket));
pClientSocket->disconnect();
pClientSocket->deleteLater();
m_ptxt->append(QString::number( pClientSocket->localPort())+" is disconnected");
}
示例2: NewConnection
// ----------------------------------------------------------------------
/*virtual*/ void DServer::slotNewConnection()
{
QTcpSocket* pClientSocket = m_ptcpServer->nextPendingConnection();
connect(pClientSocket, SIGNAL(disconnected()), this, SLOT(deleteClientSocket()) );
connect(pClientSocket, SIGNAL(readyRead()), this, SLOT(slotReadClient()) );
pClientSockets.append(pClientSocket);
sendMessageToSocket(pClientSocket, "Server Response: Connected!");
m_ptxt->append(QString::number( pClientSocket->localPort())+" is Connected");
emit NewConnection();
}
示例3: getServerSocketState
/**
*函数介绍:跟踪server端socket状态
*输入参数:服务端socket状态
*返回值:无
*/
void TcpServer::getServerSocketState(QAbstractSocket::SocketState socketState)
{
QTcpSocket *serverSocket = qobject_cast<QTcpSocket *>(sender());
QString clientIp = serverSocket->peerAddress().toString();
int clientPort = serverSocket->peerPort();
QString serverIp = this->serverAddress().toString();
int serverPort = this->serverPort();
//测试代码 是否和上面的一样的Ip和port???
QString serverIp1 = serverSocket->localAddress().toString();
int serverPort1 = serverSocket->localPort();
//测试代码
switch (socketState)
{
case QAbstractSocket::UnconnectedState:
qDebug() << "服务端:UnconnectedState:The socket is not connected.";
//Global::systemLog->append(QString(tr("网络服务端状态:断开连接状态,未连上客户端Socket.客户端IP:%1")).arg(clientIp), QString(tr("与客户端计算机断开连接,客户端IP:%1,port:%2,UnconnectedState:The socket is not connected.")).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
case QAbstractSocket::HostLookupState:
qDebug() << "服务端:HostLookupState:The socket is performing a host name lookup.";
//Global::systemLog->append(QString(tr("网络服务端状态:正在查询计算机中......客户端IP:%1")).arg(clientIp), QString(tr("正在查询客户端计算机IP:%1,port:%2,HostLookupState:The socket is performing a host name lookup.")).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
case QAbstractSocket::ConnectingState:
qDebug() << "服务端:ConnectingState:The socket has started establishing a connection.";
//Global::systemLog->append(QString(tr("网络服务端状态:正在创建网络连接......客户端IP:%1")).arg(clientIp), QString(tr("正在创建网络连接,客户端计算机IP:%1,port:%2,ConnectingState:The socket has started establishing a connection.")).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
case QAbstractSocket::ConnectedState:
qDebug() << "服务端:ConnectedState:A connection is established.";
//Global::systemLog->append(QString(tr("网络服务端状态:TCP连接创建成功.客户端IP:%1")).arg(clientIp), QString(tr("网络已经连接成功,客户端计算机IP:%1,port:%2,ConnectedState:A connection is established.")).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
case QAbstractSocket::BoundState:
qDebug() << "服务端:BoundState:The socket is bound to an address and port (for servers).";
//Global::systemLog->append(QString(tr("网络服务端状态:服务端已绑定TCP Socket.服务端IP:%1,port:%2.")).arg(serverIp).arg(serverPort), QString(tr("网络已经连接成功,本地服务端IP:%1,port:%2,客户端计算机IP:%3,port:%4,BoundState:The socket is bound to an address and port (for servers).")).arg(serverIp).arg(serverPort).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
case QAbstractSocket::ClosingState:
qDebug() << "服务端:ClosingState:The socket is about to close (data may still be waiting to be written).";
//Global::systemLog->append(QString(tr("网络服务端状态:即将关闭一个TCP Socket.客户端IP:%1")).arg(clientIp), QString(tr("网络即将关闭,远程计算机IP:%1,port:%2,ClosingState:The socket is about to close (data may still be waiting to be written).")).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
case QAbstractSocket::ListeningState:
qDebug() << "服务端:ListeningState:For internal use only.";
//Global::systemLog->append(QString(tr("网络服务端状态:Socket侦听状态.客户端IP:%1")).arg(clientIp), QString(tr("网络侦听状态,远程计算机IP:%1,port:%2")).arg(clientIp).arg(clientPort), SystemLog::INFO);
break;
default:
qDebug()<<"服务端:网络其他状态";
//Global::systemLog->append(QString(tr("网络服务端状态:其他状态.客户端IP:%1")).arg(clientIp), QString(tr("网络其他状态,远程计算机IP:%1,port:%2")).arg(clientIp).arg(clientPort), SystemLog::INFO);
}
serverSocket = 0;
}
示例4: executeClient
void QxTransaction::executeClient(IxService * pService, const QString & sMethod)
{
if ((pService == NULL) || sMethod.isEmpty()) { qAssert(false); return; }
if (pService->getServiceName().isEmpty()) { pService->setMessageReturn(qx_bool(QX_ERROR_SERVICE_NOT_SPECIFIED, "[QxOrm] empty service name")); return; }
pService->registerClass();
QTcpSocket socket;
QString serverName = QxConnect::getSingleton()->getIp();
long serverPort = QxConnect::getSingleton()->getPort();
socket.connectToHost(serverName, serverPort);
if (! socket.waitForConnected(QxConnect::getSingleton()->getMaxWait()))
{ pService->setMessageReturn(qx_bool(QX_ERROR_SERVER_NOT_FOUND, "[QxOrm] unable to connect to server")); return; }
if (m_sTransactionId.isEmpty())
{ setTransactionId(QUuid::createUuid().toString()); }
setIpSource(socket.localAddress().toString());
setPortSource(socket.localPort());
setIpTarget(serverName);
setPortTarget(serverPort);
setServiceName(pService->getServiceName());
setServiceMethod(sMethod);
setTransactionBegin(QDateTime::currentDateTime());
setInputParameter(pService->getInputParameter_BaseClass());
qx_bool bWriteOk = writeSocket(socket);
if (! bWriteOk) { pService->setMessageReturn(qx_bool(QX_ERROR_SERVICE_WRITE_ERROR, QString("[QxOrm] unable to write request to socket : '") + bWriteOk.getDesc() + QString("'"))); return; }
qx_bool bReadOk = readSocket(socket);
if (! bReadOk) { pService->setMessageReturn(qx_bool(QX_ERROR_SERVICE_READ_ERROR, QString("[QxOrm] unable to read reply from socket : '") + bReadOk.getDesc() + QString("'"))); return; }
pService->setOutputParameter(getOutputParameter());
pService->setMessageReturn(getMessageReturn());
setTransactionEnd(QDateTime::currentDateTime());
socket.disconnectFromHost();
if (socket.state() != QAbstractSocket::UnconnectedState)
{ socket.waitForDisconnected(QxConnect::getSingleton()->getMaxWait()); }
}
示例5: main
//.........这里部分代码省略.........
addy = info.addresses().at(0);
address = addy.toString();
}
}
QHostAddress theAddress(address);
if (QAbstractSocket::IPv6Protocol == theAddress.protocol())
{
QDEBUG() << "Valid IPv6 address.";
ipv6 = true;
}
QByteArray sendData = sendPacket.HEXtoByteArray(dataString);
QByteArray recvData;
recvData.clear();
int bytesWriten = 0;
int bytesRead = 0;
if(tcp) {
QTcpSocket sock;
if(ipv6) {
sock.bind(QHostAddress::AnyIPv6, bind);
} else {
sock.bind(QHostAddress::AnyIPv4, bind);
}
sock.connectToHost(addy, port);
sock.waitForConnected(1000);
if(sock.state() == QAbstractSocket::ConnectedState)
{
OUTIF() << "TCP (" <<sock.localPort() <<")://" << address << ":" << port << " " << dataString;
bytesWriten = sock.write(sendData);
sock.waitForBytesWritten(1000);
//OUTIF() << "Sent:" << Packet::byteArrayToHex(sendData);
if(wait) {
sock.waitForReadyRead(wait);
recvData = sock.readAll();
bytesRead = recvData.size();
QString hexString = Packet::byteArrayToHex(recvData);
if(quiet) {
o << "\n" << hexString;
} else {
o << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT);
o << "\nResponse HEX:" << hexString;
o << "\nResponse ASCII:" << Packet::hexToASCII(hexString);
}
}
sock.disconnectFromHost();
sock.waitForDisconnected(1000);
sock.close();
OUTPUT();
return bytesWriten;
} else {
OUTIF() << "Error: Failed to connect to " << address;
OUTPUT();
return -1;
}
示例6: main
int main(int argc, char* argv[])
{
core::system::initHook();
try
{
initializeLang();
if (useChromiumDevtools())
{
// use QTcpSocket to find an open port. this is unfortunately a bit racey
// but AFAICS there isn't a better solution for port selection
QByteArray port;
QTcpSocket* pSocket = new QTcpSocket();
if (pSocket->bind())
{
quint16 port = pSocket->localPort();
desktopInfo().setChromiumDevtoolsPort(port);
core::system::setenv("QTWEBENGINE_REMOTE_DEBUGGING", safe_convert::numberToString(port));
pSocket->close();
}
}
// initialize log
core::system::initializeLog("rdesktop",
core::system::kLogLevelWarning,
desktop::userLogPath());
// ignore SIGPIPE
Error error = core::system::ignoreSignal(core::system::SigPipe);
if (error)
LOG_ERROR(error);
// attempt to remove stale lockfiles, as they can impede
// application startup
error = removeStaleOptionsLockfile();
if (error)
LOG_ERROR(error);
// set application attributes
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
// prepare command line arguments
static std::vector<char*> arguments(argv, argv + argc);
// enable viewport meta (allows us to control / restrict
// certain touch gestures)
static char enableViewport[] = "--enable-viewport";
arguments.push_back(enableViewport);
#ifndef NDEBUG
// disable web security for development builds (so we can
// get access to sourcemaps)
static char disableWebSecurity[] = "--disable-web-security";
arguments.push_back(disableWebSecurity);
#endif
// disable chromium renderer accessibility by default (it can cause
// slowdown when used in conjunction with some applications; see e.g.
// https://github.com/rstudio/rstudio/issues/1990)
if (core::system::getenv("RSTUDIO_ACCESSIBILITY").empty())
{
static char disableRendererAccessibility[] = "--disable-renderer-accessibility";
arguments.push_back(disableRendererAccessibility);
}
#ifdef Q_OS_LINUX
// workaround for Qt 5.10.1 bug "Could not find QtWebEngineProcess"
// https://bugreports.qt.io/browse/QTBUG-67023
// https://bugreports.qt.io/browse/QTBUG-66346
static char noSandbox[] = "--no-sandbox";
arguments.push_back(noSandbox);
#endif
#ifdef Q_OS_MAC
// don't prefer compositing to LCD text rendering. when enabled, this causes the compositor to
// be used too aggressively on Retina displays on macOS, with the side effect that the
// scrollbar doesn't auto-hide because a compositor layer is present.
// https://github.com/rstudio/rstudio/issues/1953
static char disableCompositorPref[] = "--disable-prefer-compositing-to-lcd-text";
arguments.push_back(disableCompositorPref);
// disable gpu rasterization for certain display configurations.
// this works around some of the rendering issues seen with RStudio.
//
// https://bugs.chromium.org/p/chromium/issues/detail?id=773705
// https://github.com/rstudio/rstudio/issues/2093
//
// because the issue seems to only affect certain video cards on macOS
// High Sierra, we scope that change to that particular configuration
// for now (we can expand this list if more users report issues)
core::Version macVersion(QSysInfo::productVersion().toStdString());
if (macVersion.versionMajor() == 10 &&
macVersion.versionMinor() == 13)
{
core::system::ProcessResult processResult;
core::system::runCommand(
"/usr/sbin/system_profiler SPDisplaysDataType",
core::system::ProcessOptions(),
&processResult);
//.........这里部分代码省略.........
示例7: main
//.........这里部分代码省略.........
proxy_env = qgetenv("http_proxy");
// If http_proxy environment is defined in linux then proxy server is configured.
if (!proxy_env.isEmpty()) {
QNetworkProxy::setApplicationProxy(QNetworkProxy());
}
#endif
// Display the spash screen
QSplashScreen *splash = new QSplashScreen();
splash->setPixmap(QPixmap(":/splash.png"));
splash->show();
app.processEvents(QEventLoop::AllEvents);
quint16 port = 0L;
// Find an unused port number. Essentially, we're just reserving one
// here that Flask will use when we start up the server.
// In order to use the socket, we need to free this socket ASAP.
// Hence - putting this code in a code block so the scope of the socket
// variable vanishes to make that socket available.
{
#if QT_VERSION >= 0x050000
QTcpSocket socket;
#if QT_VERSION >= 0x050900
socket.setProxy(QNetworkProxy::NoProxy);
#endif
socket.bind(0, QTcpSocket::ShareAddress);
#else
QUdpSocket socket;
socket.bind(0, QUdpSocket::ShareAddress);
#endif
port = socket.localPort();
}
// Generate a random key to authenticate the client to the server
QString key = QUuid::createUuid().toString();
key = key.mid(1, key.length() - 2);
// Generate the filename for the log
logFileName = homeDir + (QString("/.%1.%2.log").arg(PGA_APP_NAME).arg(exeHash)).remove(" ");
// Start the tray service
TrayIcon *trayicon = new TrayIcon(logFileName);
if (!trayicon->Init())
{
QString error = QString(QWidget::tr("An error occurred initialising the tray icon"));
QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error);
exit(1);
}
// Fire up the webserver
Server *server;
bool done = false;
while (done != true)
{
server = new Server(port, key, logFileName);
if (!server->Init())
{
splash->finish(NULL);