本文整理汇总了C++中QTcpServer::serverError方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpServer::serverError方法的具体用法?C++ QTcpServer::serverError怎么用?C++ QTcpServer::serverError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpServer
的用法示例。
在下文中一共展示了QTcpServer::serverError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: proxyFactory
void tst_QTcpServer::proxyFactory()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy)
return;
QFETCH(QList<QNetworkProxy>, proxyList);
QFETCH(QNetworkProxy, proxyUsed);
QFETCH(bool, fails);
MyProxyFactory *factory = new MyProxyFactory;
factory->toReturn = proxyList;
QNetworkProxyFactory::setApplicationProxyFactory(factory);
QTcpServer server;
bool listenResult = server.listen();
// Verify that the factory was called properly
QCOMPARE(factory->callCount, 1);
QCOMPARE(factory->lastQuery, QNetworkProxyQuery(0, QString(), QNetworkProxyQuery::TcpServer));
QCOMPARE(listenResult, !fails);
QCOMPARE(server.errorString().isEmpty(), !fails);
// note: the following test is not a hard failure.
// Sometimes, error codes change for the better
QTEST(int(server.serverError()), "expectedError");
}
示例2: setSocketDescriptor
//----------------------------------------------------------------------------------
void tst_QTcpServer::setSocketDescriptor()
{
QTcpServer server;
QVERIFY(!server.setSocketDescriptor(42));
QCOMPARE(server.serverError(), QAbstractSocket::UnsupportedSocketOperationError);
#ifdef Q_OS_WIN
// ensure winsock is started
WSADATA wsaData;
QVERIFY(WSAStartup(MAKEWORD(2,0), &wsaData) == NO_ERROR);
#endif
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
QVERIFY(sock != INVALID_SOCKET);
sockaddr_in sin;
memset(&sin, 0, sizeof(sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = 0;
sin.sin_addr.s_addr = 0x00000000;
QVERIFY(::bind(sock, (sockaddr*)&sin, sizeof(sockaddr_in)) == 0);
QVERIFY(::listen(sock, 10) == 0);
QVERIFY(server.setSocketDescriptor(sock));
#ifdef Q_OS_WIN
WSACleanup();
#endif
}
示例3: waitForConnectionTest
//----------------------------------------------------------------------------------
void tst_QTcpServer::waitForConnectionTest()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy) {
QFETCH_GLOBAL(int, proxyType);
if (proxyType == QNetworkProxy::Socks5Proxy) {
QSKIP("Localhost servers don't work well with SOCKS5", SkipAll);
}
}
QTcpSocket findLocalIpSocket;
findLocalIpSocket.connectToHost(QtNetworkSettings::serverName(), 143);
QVERIFY(findLocalIpSocket.waitForConnected(5000));
QTcpServer server;
bool timeout = false;
QVERIFY(server.listen(findLocalIpSocket.localAddress()));
QVERIFY(!server.waitForNewConnection(1000, &timeout));
QCOMPARE(server.serverError(), QAbstractSocket::SocketTimeoutError);
QVERIFY(timeout);
ThreadConnector connector(findLocalIpSocket.localAddress(), server.serverPort());
connector.start();
#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
QVERIFY(server.waitForNewConnection(9000, &timeout));
#else
QVERIFY(server.waitForNewConnection(3000, &timeout));
#endif
QVERIFY(!timeout);
}
示例4: defined
//----------------------------------------------------------------------------------
void tst_QTcpServer::ipv6Server()
{
#if defined(Q_OS_SYMBIAN)
QSKIP("Symbian: IPv6 is not yet supported", SkipAll);
#endif
//### need to enter the event loop for the server to get the connection ?? ( windows)
QTcpServer server;
if (!server.listen(QHostAddress::LocalHostIPv6, 8944)) {
QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError);
return;
}
QVERIFY(server.serverPort() == 8944);
QVERIFY(server.serverAddress() == QHostAddress::LocalHostIPv6);
QTcpSocket client;
client.connectToHost("::1", 8944);
QVERIFY(client.waitForConnected(5000));
QVERIFY(server.waitForNewConnection());
QVERIFY(server.hasPendingConnections());
QTcpSocket *serverSocket = 0;
QVERIFY((serverSocket = server.nextPendingConnection()));
}
示例5: listenError
//----------------------------------------------------------------------------------
void tst_QTcpServer::listenError()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy) {
QFETCH_GLOBAL(int, proxyType);
if (proxyType == QNetworkProxy::Socks5Proxy) {
QSKIP("With socks5 we can not make hard requirements on the address or port", SkipAll);
}
}
QTcpServer server;
QVERIFY(!server.listen(QHostAddress("1.2.3.4"), 0));
QCOMPARE(server.serverError(), QAbstractSocket::SocketAddressNotAvailableError);
QCOMPARE(server.errorString().toLatin1().constData(), "The address is not available");
}
示例6: constructing
void tst_QTcpServer::constructing()
{
QTcpServer socket;
// Check the initial state of the QTcpSocket.
QCOMPARE(socket.isListening(), false);
QCOMPARE((int)socket.serverPort(), 0);
QCOMPARE(socket.serverAddress(), QHostAddress());
QCOMPARE(socket.maxPendingConnections(), 30);
QCOMPARE(socket.hasPendingConnections(), false);
QCOMPARE(socket.socketDescriptor(), -1);
QCOMPARE(socket.serverError(), QAbstractSocket::UnknownSocketError);
// Check the state of the socket layer?
}
示例7: invalidProxy
void tst_QTcpServer::invalidProxy()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy)
return;
QFETCH(int, type);
QFETCH(QString, host);
QFETCH(int, port);
QNetworkProxy::ProxyType proxyType = QNetworkProxy::ProxyType(type);
QNetworkProxy proxy(proxyType, host, port);
QTcpServer server;
server.setProxy(proxy);
bool listenResult = server.listen();
QVERIFY(!listenResult);
QVERIFY(!server.errorString().isEmpty());
// note: the following test is not a hard failure.
// Sometimes, error codes change for the better
QTEST(int(server.serverError()), "expectedError");
}
示例8: main
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
for(int i = 0 ; i < argc; i++){
qDebug() << argv[i] << endl;
}
if(argc >= 4)
{
//Getting first argument for application
QString arg = argv[1];
//Sending mode
if(arg == "send")
{
QTcpSocket *tcpSocket = new QTcpSocket(&a);
QString port = argv[3];
tcpSocket->connectToHost(QHostAddress(argv[2]), port.toInt());
if(!tcpSocket->waitForConnected(10*1000)){
qDebug() << "Connection cant be established to host: " << argv[2] << ", at port: " << port << endl;
}else{
//Sending mode = file
QString type(argv[4]);
if(type == "file")
{
QFile file(argv[5]);
if(!file.open(QFile::ReadOnly))
{
qDebug() << "Cannot open file" << endl;
}else
{
qDebug() << "File size in bytes: " << file.size() << endl;
int counter = 0;
//Divide file into chunks of 300KB
int chunkSize = 3 * 100000;
while(counter < file.size()){
if(!file.seek(counter)){
qDebug() << "Cant seek the file to : " << counter << endl;
}else{
QByteArray buffer = file.read(chunkSize);
if(!buffer.isEmpty()){
int bytesWritten = tcpSocket->write(buffer);
counter += bytesWritten;
if(!tcpSocket->waitForBytesWritten(10*1000))
{
qDebug() << "Error no bytes written" << tcpSocket->errorString() << endl;
}else
{
qDebug() << "Bytes for writting: " << buffer.size() << ", " << bytesWritten << " bytes written. " << endl;
}
}else{
qDebug() << "0 bytes read from file, error: " << file.errorString() << endl;
break;
}
}
}
}
//Sending mode = string
}else if(type == "string")
{
QByteArray data = argv[5];
int bytesWritten = tcpSocket->write(data);
if(!tcpSocket->waitForBytesWritten(10000))
{
qDebug() << "Error no bytes written" << tcpSocket->errorString() << endl;
}else
{
qDebug() << bytesWritten << " bytes written. " << endl;
}
}else{
qDebug() << "Unknown sending format " << endl;
}
}
tcpSocket->close();
delete tcpSocket;
//Receiving mode
}else if(arg == "receive")
{
QTcpServer *tcpServer = new QTcpServer(&a);
QString port = argv[3];
if(!tcpServer->listen(QHostAddress(QString(argv[2])),port.toInt())){
qDebug() << "Error, could not start listening, " << tcpServer->serverError() << endl;
}else{
QString fileName;
QString destinationPath;
//Getting name and path for the new file from user
bool tryAgain = true;
while(tryAgain){
qDebug() << "Enter filename for the new file (ex: picture.png) :";
QTextStream s(stdin);
fileName = s.readLine();
qDebug() << "Enter destination path: ";
QTextStream d(stdin);
destinationPath = d.readLine();
if (!fileName.isEmpty() && !destinationPath.isEmpty())
{
qDebug() << "The destination string: " << destinationPath + fileName;
tryAgain = false;
//.........这里部分代码省略.........