
Originally Posted by
prof.ebral
If the test tool I showed shows your port as open, then you have a problem in the code. Can you show us some of your code? Perhpas anda_soka's suggestion has relevence to your problem.
The test tool said the port is closed when I entered my external IP address and my local IPv4 address.
Here's the server code:
main.cpp:
#include <QtCore/QCoreApplication>
#include "myserver.h"
int main(int argc, char *argv[]){
myserver server;
server.startServer();
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "myserver.h"
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
myserver server;
server.startServer();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
myserver.h:
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QtNetwork/QTcpServer>
#include <QDebug>
#include <iostream>
#include "mythread.h"
{
Q_OBJECT
public:
explicit myserver
(QObject *parent
= 0);
void startServer();
protected:
void incomingConnection(int socketDescriptor);
};
#endif // MYSERVER_H
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QtNetwork/QTcpServer>
#include <QDebug>
#include <iostream>
#include "mythread.h"
class myserver : public QTcpServer
{
Q_OBJECT
public:
explicit myserver(QObject *parent = 0);
void startServer();
protected:
void incomingConnection(int socketDescriptor);
};
#endif // MYSERVER_H
To copy to clipboard, switch view to plain text mode
myserver.cpp:
#include "myserver.h"
myserver
::myserver(QObject *parent
) :{
}
void myserver::startServer(){
qDebug() << "Server failed to start";
}
else{
qDebug() << "Server started";
}
}
void myserver::incomingConnection(int socketDescriptor){
mythread *thread = new mythread(socketDescriptor, this);
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); //deletes the thread after its finished being used
thread->start();
}
#include "myserver.h"
myserver::myserver(QObject *parent) :
QTcpServer(parent)
{
}
void myserver::startServer(){
if(!this->listen(QHostAddress::Any,1234)){
qDebug() << "Server failed to start";
}
else{
qDebug() << "Server started";
}
}
void myserver::incomingConnection(int socketDescriptor){
mythread *thread = new mythread(socketDescriptor, this);
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); //deletes the thread after its finished being used
thread->start();
}
To copy to clipboard, switch view to plain text mode
mythread.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QtNetwork/QTcpSocket>
#include <QDebug>
{
Q_OBJECT
public:
explicit mythread
(int id,
QObject *parent
= 0);
void run();
public slots:
void disconnected();
void readyRead();
signals:
private:
int socketDescriptor;
};
#endif // MYTHREAD_H
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QtNetwork/QTcpSocket>
#include <QDebug>
class mythread : public QThread
{
Q_OBJECT
public:
explicit mythread(int id, QObject *parent = 0);
void run();
public slots:
void disconnected();
void readyRead();
signals:
void error(QTcpSocket::SocketError socketError);
private:
QTcpSocket *socket;
int socketDescriptor;
};
#endif // MYTHREAD_H
To copy to clipboard, switch view to plain text mode
mythread.cpp:
#include "mythread.h"
mythread
::mythread(int id,
QObject *parent
) :{
this->socketDescriptor = id;
}
void mythread::run(){
if(!socket->setSocketDescriptor(this->socketDescriptor)){
emit error(socket->error());
return;
}
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
qDebug() << "client" << socketDescriptor << "connected";
exec(); //stop thread from closing
}
void mythread::readyRead(){
qDebug() << "client" << socketDescriptor << "sent" << data;
if(data == "J"){
socket->write("hello");
}
}
void mythread::disconnected(){
qDebug() << "client" << socketDescriptor << "disconnected";
socket->deleteLater();
exit(0);
}
#include "mythread.h"
mythread::mythread(int id, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = id;
}
void mythread::run(){
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor)){
emit error(socket->error());
return;
}
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
qDebug() << "client" << socketDescriptor << "connected";
exec(); //stop thread from closing
}
void mythread::readyRead(){
QByteArray data = socket->readAll();
qDebug() << "client" << socketDescriptor << "sent" << data;
if(data == "J"){
socket->write("hello");
}
}
void mythread::disconnected(){
qDebug() << "client" << socketDescriptor << "disconnected";
socket->deleteLater();
exit(0);
}
To copy to clipboard, switch view to plain text mode
Bookmarks