valdemar593
27th May 2011, 15:37
Hello everyone. I'm trying to write a chat using QTcpSocket and QTcpServer, but strange things are happening. In the first example QTcpServer emits newConnection(), but QTcpSocket remains in connecting state without emiting error(...). In the second example I receive SIGSEGV when trying connectToHost() . What is more, FortuneClient/Server (single threaded) example runs just fine. In both examples network sessiong is not required. Being discouraged I examined the Fortune code and discovored that it is just the same concerning establishing connection. Here are both examples.
FIRST
//CLIENT
ChatClient::ChatClient(QObject *parent)
: QObject(parent) {
tcpSocket = new QTcpSocket(this);
QNetworkConfigurationManager manager;
if (QNetworkConfigurationManager::NetworkSessionRequi red
& manager.capabilities()) {
qDebug() << "Network session required";
}
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(error(QAbstractSocket::SocketError)));
connect(tcpSocket, SIGNAL(connected()),
this, SLOT(requestForID()));
connect(tcpSocket, SIGNAL(readyRead()),
this, SLOT(receiveMessage()));
tcpSocket->connectToHost("192.168.0.100", PORT);
}
...
void ChatClient::error(QAbstractSocket::SocketError error) {
qDebug() << "Socket error" << error;
}
void ChatClient::requestForID() {
qDebug() << "Connected, requesting for ID";
QByteArray segment;
QDataStream out(&segment, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0 << ID;
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
tcpSocket->write(segment);
}
//SERVER
ChatServer::ChatServer(QObject *parent)
: QObject(parent) {
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(/*QHostAddress::Any, PORT*/)) {
qDebug() << "Unable to start the server"
<< tcpServer->errorString();
}
qDebug() << "Server port" << tcpServer->serverPort();
connect(tcpServer, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
void ChatServer::processConnection() {
qDebug() << "Incoming connection";
QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
/*connect(clientSocket, SIGNAL(readyRead()),
this, SLOT(readData()));
readData(clientSocket);
connect(clientSocket, SIGNAL(disconnected()),
clientSocket, SLOT(deleteLater()));*/
QByteArray segment;
QDataStream out(&segment, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0 << (quint16)Message
<< "Successfully connected";
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
clientSocket->write(segment);
clientSocket->disconnectFromHost();
}
SECOND
//CLIENT
Client::Client() {
QTcpSocket *clientSocket = new QTcpSocket(this);
connect(clientSocket, SIGNAL(connected()),
this, SLOT(connectionEstablished()));
connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(error()));
}
void Client::establishConnection() {
// throws SIGSEGV
clientSocket->connectToHost(QHostAddress("192.168.0.100"), 6178);
}
void Client::connectionEstablished() {
qDebug() << "Connection established";
}
void Client::error() {
qDebug() << clientSocket->errorString();
}
//SERVER
Server::Server() {
QNetworkConfigurationManager manager;
if (manager.capabilities() &
QNetworkConfigurationManager::NetworkSessionRequir ed) {
qDebug() << "Network session required";
} else {
sessionOpened();
}
connect(server, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
void Server::processConnection() {
qDebug() << "Incoming connection";
}
void Server::sessionOpened() {
server = new QTcpServer(this);
if (!server->listen(QHostAddress::Any, 6178)) {
qDebug() << QString("Unable to start the server: %1")
.arg(server->errorString());
return;
}
qDebug() << "The server is running...";
}
Any ideas?
FIRST
//CLIENT
ChatClient::ChatClient(QObject *parent)
: QObject(parent) {
tcpSocket = new QTcpSocket(this);
QNetworkConfigurationManager manager;
if (QNetworkConfigurationManager::NetworkSessionRequi red
& manager.capabilities()) {
qDebug() << "Network session required";
}
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(error(QAbstractSocket::SocketError)));
connect(tcpSocket, SIGNAL(connected()),
this, SLOT(requestForID()));
connect(tcpSocket, SIGNAL(readyRead()),
this, SLOT(receiveMessage()));
tcpSocket->connectToHost("192.168.0.100", PORT);
}
...
void ChatClient::error(QAbstractSocket::SocketError error) {
qDebug() << "Socket error" << error;
}
void ChatClient::requestForID() {
qDebug() << "Connected, requesting for ID";
QByteArray segment;
QDataStream out(&segment, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0 << ID;
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
tcpSocket->write(segment);
}
//SERVER
ChatServer::ChatServer(QObject *parent)
: QObject(parent) {
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(/*QHostAddress::Any, PORT*/)) {
qDebug() << "Unable to start the server"
<< tcpServer->errorString();
}
qDebug() << "Server port" << tcpServer->serverPort();
connect(tcpServer, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
void ChatServer::processConnection() {
qDebug() << "Incoming connection";
QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
/*connect(clientSocket, SIGNAL(readyRead()),
this, SLOT(readData()));
readData(clientSocket);
connect(clientSocket, SIGNAL(disconnected()),
clientSocket, SLOT(deleteLater()));*/
QByteArray segment;
QDataStream out(&segment, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0 << (quint16)Message
<< "Successfully connected";
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
clientSocket->write(segment);
clientSocket->disconnectFromHost();
}
SECOND
//CLIENT
Client::Client() {
QTcpSocket *clientSocket = new QTcpSocket(this);
connect(clientSocket, SIGNAL(connected()),
this, SLOT(connectionEstablished()));
connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(error()));
}
void Client::establishConnection() {
// throws SIGSEGV
clientSocket->connectToHost(QHostAddress("192.168.0.100"), 6178);
}
void Client::connectionEstablished() {
qDebug() << "Connection established";
}
void Client::error() {
qDebug() << clientSocket->errorString();
}
//SERVER
Server::Server() {
QNetworkConfigurationManager manager;
if (manager.capabilities() &
QNetworkConfigurationManager::NetworkSessionRequir ed) {
qDebug() << "Network session required";
} else {
sessionOpened();
}
connect(server, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
void Server::processConnection() {
qDebug() << "Incoming connection";
}
void Server::sessionOpened() {
server = new QTcpServer(this);
if (!server->listen(QHostAddress::Any, 6178)) {
qDebug() << QString("Unable to start the server: %1")
.arg(server->errorString());
return;
}
qDebug() << "The server is running...";
}
Any ideas?