Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCP/IP server and client examples are working only same computer
Forum Updated to NodeBB v4.3 + New Features

TCP/IP server and client examples are working only same computer

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 875 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ozie
    wrote on 16 Mar 2021, 12:54 last edited by
    #1

    Hi everyone , I've been trying to make a server and a client applications that communicate each other. Actually its works like that I want but only same computer and using local ip. I need to communicate different computers. I 've read something that I should create a port for my computer's ip in modem. How can I do that ?

    Would be appreciated for your helps.

    Server.cpp file

    
    Server::Server(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::Server)
    {
        ui->setupUi(this);
    
    }
    
    Server::~Server()
    {
        delete ui;
    }
    
    void Server::on_pushButton_Start_clicked(){
        allClients=new QVector<QTcpSocket*>;
        chatserver=new QTcpServer();
        chatserver->setMaxPendingConnections(10);
        connect(chatserver,SIGNAL(newConnection()),this,SLOT(NewClientConnection()));
    
        /*///////////////////////////////////////////////////////////////////////////////////*/
        QString ipAddr;
        QList<QHostAddress> ipAddrList=QNetworkInterface::allAddresses();
        for (int i = 0; i < ipAddrList.size(); ++i) {
            if (ipAddrList.at(i) != QHostAddress::LocalHost && ipAddrList.at(i).toIPv4Address()) {
                ipAddr = ipAddrList.at(i).toString();
                break;
            }
    
        }
    
        if (ipAddr.isEmpty())
            ipAddr = QHostAddress(QHostAddress::LocalHost).toString();
        ui->textEdit_Message->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n").arg(ipAddr).arg(ui->textEdit_Port->toPlainText()));
            /*///////////////////////////////////////////////////////////////////////////////////*/
        if(chatserver->listen(QHostAddress::AnyIPv4,quint16(ui->textEdit_Port->toPlainText().toInt()))){
    
          //  ui->textEdit_Message->append("Server has started. Listening to port "+ui->textEdit_Port->toPlainText());
            qDebug() << "Server has started. Listening to port "+ui->textEdit_Port->toPlainText();
        }
        else{
    
            ui->textEdit_Message->setText("Server failed to start. Error: " + chatserver->errorString());
            qDebug() << "Server failed to start. Error: " + chatserver->errorString();
        }
    }
    
    
    
    void Server::on_pushButton_Send_clicked(){
    
        QString data =ui->textEdit_Send->toPlainText();
        SendMessageToClients(data);
        ui->textEdit_Message->append("<font color=\"Blue\">Server: ' "+ data + " ' </font>");
        ui->textEdit_Send->clear();
    }
    
    void Server::SendMessageToClients(QString message){
    
        if(allClients->size()>0)
    
            for (int i=0;i<allClients->size();i++ )
    
                if(allClients->at(i)->isOpen() && allClients->at(i)->isWritable())
    
                    allClients->at(i)->write(message.toUtf8());
    
    
    }
    
    void Server:: NewClientConnection(){
    
        QTcpSocket* Client = chatserver->nextPendingConnection();
        QString ipAddress=Client->peerAddress().toString();
        int port=Client->peerPort();
        connect(Client,&QTcpSocket::disconnected,this,&Server::SocketDisconnected);
        connect(Client, &QTcpSocket::readyRead,this, &Server::SocketReadReady);
        connect(Client, &QTcpSocket::stateChanged, this, &Server::SocketStateChanged);
    
        allClients->push_back(Client);
    
        ui->textEdit_Message->append("<font color=\"Green\">Client connected from " + ipAddress + ":" + QString::number(port)+"</font>");
        qDebug() << "Client connected from " + ipAddress + ":" + QString::number(port);
    
    }
    
    void Server::SocketDisconnected(){
    
        QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
        QString SocketIpAddress=Client->peerAddress().toString();
        int port=Client->peerPort();
        ui->textEdit_Message->append("<font color=\"Red\">Socket disconnected from " + SocketIpAddress + ":" + QString::number(port)+"</font>");
        qDebug() << "Socket disconnected from " + SocketIpAddress + ":" + QString::number(port);
        allClients->removeOne(Client);
    }
    
    void Server::SocketReadReady(){
    
        QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
        QString SocketIpAddress=Client->peerAddress().toString();
        int port=Client->peerPort();
        while(Client->canReadLine()){
    
            QString data =QString::fromUtf8(Client->readLine().trimmed());
            ui->textEdit_Message->append("<font color=\"Purple\"> ( " +SocketIpAddress + ":" + QString::number(port) + "): ' "+ data+" '</font>");
            qDebug() << "Message: " + data + " (" + SocketIpAddress + ":" + QString::number(port) + ")";
        }
    }
    
    void Server::SocketStateChanged(QAbstractSocket::SocketState state){
    
        QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
        QString SocketIpAddress=Client->peerAddress().toString();
        int port=Client->peerPort();
        QString desc;
    
       if(state==QAbstractSocket::UnconnectedState)
           desc="The socket is not connected.\n";
       else if (state == QAbstractSocket::HostLookupState)
           desc = "The socket is performing a host name lookup.\n";
       else if (state == QAbstractSocket::ConnectingState)
           desc = "The socket has started establishing a connection.\n";
       else if (state == QAbstractSocket::ConnectedState)
           desc = "A connection is established.\n";
       else if (state == QAbstractSocket::BoundState)
           desc = "The socket is bound to an address and port.\n";
       else if (state == QAbstractSocket::ClosingState)
           desc = "The socket is about to close (data may still be waiting to be written).\n";
       else if (state == QAbstractSocket::ListeningState)
           desc = "For internal use only.\n";
    
       ui->textEdit_Message->append("<font color=\"Orange\"> Socket state changed ( " + SocketIpAddress + ":" + QString::number(port) + "): " + desc+"</font>");
       qDebug() << "Socket state changed (" + SocketIpAddress + ":" + QString::number(port) + "): " + desc;
    
    }
    
    
    

    Client.cpp file

    Client::Client(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::Client)
    {
        ui->setupUi(this);
        connectedToHost=false;
    }
    
    Client::~Client()
    {
        delete ui;
    }
    
    
    void Client::on_pushButton_Connect_clicked(){
    
        if(!connectedToHost){
            socket =new QTcpSocket();
    
            connect(socket,&QTcpSocket::connected,this,&Client::SocketConnected);
            connect(socket,&QTcpSocket::disconnected,this,&Client::SocketDisconnected);
            connect(socket,&QTcpSocket::readyRead,this,&Client::SocketReadyRead);
    
            socket->connectToHost(ui->textEdit_IP->toPlainText(),quint16(ui->textEdit_Port->toPlainText().toInt()));
        }
        else{
            QString name = ui->textEdit_IP->toPlainText();
    
            socket->write(name.toUtf8() + " has left the chat room.\n");
            socket->disconnectFromHost();
        }
    
    }
    
    void Client::on_pushButton_Send_clicked(){
    
        QString message = ui->textEdit_Send->toPlainText();
        socket->write(message.toUtf8()+"\n");
        ui->textEdit_Message->append("<font color=\"Purple\">Client:"+message.toUtf8()+" '</font>");
        ui->textEdit_Send->clear();
    }
    
    
    void Client::SocketConnected(){
    
        qDebug() << "Connected to server.";
        QString name=socket->peerName();
        int port =socket->peerPort();
        ui->textEdit_Message->append("<font color=\"Green\">Connected to:" +name+":"+ QString::number(port)+"</font>");
        ui->pushButton_Connect->setText("Disconnect");
        connectedToHost=true;
    }
    
    void Client:: SocketDisconnected(){
    
        qDebug() << "Disconnected from server.";
        ui->textEdit_Message->append("<font color=\"Red\">Disconnected from server.</font>");
        ui->pushButton_Connect->setText("Connect");
        connectedToHost = false;
    }
    
    void Client::SocketReadyRead(){
    
        ui->textEdit_Message->append("<font color=\"Blue\">Server: ' "+socket->readAll()+ "'");
    }
    
    
    JonBJ Pablo J. RoginaP 2 Replies Last reply 16 Mar 2021, 13:09
    0
    • O ozie
      16 Mar 2021, 12:54

      Hi everyone , I've been trying to make a server and a client applications that communicate each other. Actually its works like that I want but only same computer and using local ip. I need to communicate different computers. I 've read something that I should create a port for my computer's ip in modem. How can I do that ?

      Would be appreciated for your helps.

      Server.cpp file

      
      Server::Server(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::Server)
      {
          ui->setupUi(this);
      
      }
      
      Server::~Server()
      {
          delete ui;
      }
      
      void Server::on_pushButton_Start_clicked(){
          allClients=new QVector<QTcpSocket*>;
          chatserver=new QTcpServer();
          chatserver->setMaxPendingConnections(10);
          connect(chatserver,SIGNAL(newConnection()),this,SLOT(NewClientConnection()));
      
          /*///////////////////////////////////////////////////////////////////////////////////*/
          QString ipAddr;
          QList<QHostAddress> ipAddrList=QNetworkInterface::allAddresses();
          for (int i = 0; i < ipAddrList.size(); ++i) {
              if (ipAddrList.at(i) != QHostAddress::LocalHost && ipAddrList.at(i).toIPv4Address()) {
                  ipAddr = ipAddrList.at(i).toString();
                  break;
              }
      
          }
      
          if (ipAddr.isEmpty())
              ipAddr = QHostAddress(QHostAddress::LocalHost).toString();
          ui->textEdit_Message->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n").arg(ipAddr).arg(ui->textEdit_Port->toPlainText()));
              /*///////////////////////////////////////////////////////////////////////////////////*/
          if(chatserver->listen(QHostAddress::AnyIPv4,quint16(ui->textEdit_Port->toPlainText().toInt()))){
      
            //  ui->textEdit_Message->append("Server has started. Listening to port "+ui->textEdit_Port->toPlainText());
              qDebug() << "Server has started. Listening to port "+ui->textEdit_Port->toPlainText();
          }
          else{
      
              ui->textEdit_Message->setText("Server failed to start. Error: " + chatserver->errorString());
              qDebug() << "Server failed to start. Error: " + chatserver->errorString();
          }
      }
      
      
      
      void Server::on_pushButton_Send_clicked(){
      
          QString data =ui->textEdit_Send->toPlainText();
          SendMessageToClients(data);
          ui->textEdit_Message->append("<font color=\"Blue\">Server: ' "+ data + " ' </font>");
          ui->textEdit_Send->clear();
      }
      
      void Server::SendMessageToClients(QString message){
      
          if(allClients->size()>0)
      
              for (int i=0;i<allClients->size();i++ )
      
                  if(allClients->at(i)->isOpen() && allClients->at(i)->isWritable())
      
                      allClients->at(i)->write(message.toUtf8());
      
      
      }
      
      void Server:: NewClientConnection(){
      
          QTcpSocket* Client = chatserver->nextPendingConnection();
          QString ipAddress=Client->peerAddress().toString();
          int port=Client->peerPort();
          connect(Client,&QTcpSocket::disconnected,this,&Server::SocketDisconnected);
          connect(Client, &QTcpSocket::readyRead,this, &Server::SocketReadReady);
          connect(Client, &QTcpSocket::stateChanged, this, &Server::SocketStateChanged);
      
          allClients->push_back(Client);
      
          ui->textEdit_Message->append("<font color=\"Green\">Client connected from " + ipAddress + ":" + QString::number(port)+"</font>");
          qDebug() << "Client connected from " + ipAddress + ":" + QString::number(port);
      
      }
      
      void Server::SocketDisconnected(){
      
          QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
          QString SocketIpAddress=Client->peerAddress().toString();
          int port=Client->peerPort();
          ui->textEdit_Message->append("<font color=\"Red\">Socket disconnected from " + SocketIpAddress + ":" + QString::number(port)+"</font>");
          qDebug() << "Socket disconnected from " + SocketIpAddress + ":" + QString::number(port);
          allClients->removeOne(Client);
      }
      
      void Server::SocketReadReady(){
      
          QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
          QString SocketIpAddress=Client->peerAddress().toString();
          int port=Client->peerPort();
          while(Client->canReadLine()){
      
              QString data =QString::fromUtf8(Client->readLine().trimmed());
              ui->textEdit_Message->append("<font color=\"Purple\"> ( " +SocketIpAddress + ":" + QString::number(port) + "): ' "+ data+" '</font>");
              qDebug() << "Message: " + data + " (" + SocketIpAddress + ":" + QString::number(port) + ")";
          }
      }
      
      void Server::SocketStateChanged(QAbstractSocket::SocketState state){
      
          QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
          QString SocketIpAddress=Client->peerAddress().toString();
          int port=Client->peerPort();
          QString desc;
      
         if(state==QAbstractSocket::UnconnectedState)
             desc="The socket is not connected.\n";
         else if (state == QAbstractSocket::HostLookupState)
             desc = "The socket is performing a host name lookup.\n";
         else if (state == QAbstractSocket::ConnectingState)
             desc = "The socket has started establishing a connection.\n";
         else if (state == QAbstractSocket::ConnectedState)
             desc = "A connection is established.\n";
         else if (state == QAbstractSocket::BoundState)
             desc = "The socket is bound to an address and port.\n";
         else if (state == QAbstractSocket::ClosingState)
             desc = "The socket is about to close (data may still be waiting to be written).\n";
         else if (state == QAbstractSocket::ListeningState)
             desc = "For internal use only.\n";
      
         ui->textEdit_Message->append("<font color=\"Orange\"> Socket state changed ( " + SocketIpAddress + ":" + QString::number(port) + "): " + desc+"</font>");
         qDebug() << "Socket state changed (" + SocketIpAddress + ":" + QString::number(port) + "): " + desc;
      
      }
      
      
      

      Client.cpp file

      Client::Client(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::Client)
      {
          ui->setupUi(this);
          connectedToHost=false;
      }
      
      Client::~Client()
      {
          delete ui;
      }
      
      
      void Client::on_pushButton_Connect_clicked(){
      
          if(!connectedToHost){
              socket =new QTcpSocket();
      
              connect(socket,&QTcpSocket::connected,this,&Client::SocketConnected);
              connect(socket,&QTcpSocket::disconnected,this,&Client::SocketDisconnected);
              connect(socket,&QTcpSocket::readyRead,this,&Client::SocketReadyRead);
      
              socket->connectToHost(ui->textEdit_IP->toPlainText(),quint16(ui->textEdit_Port->toPlainText().toInt()));
          }
          else{
              QString name = ui->textEdit_IP->toPlainText();
      
              socket->write(name.toUtf8() + " has left the chat room.\n");
              socket->disconnectFromHost();
          }
      
      }
      
      void Client::on_pushButton_Send_clicked(){
      
          QString message = ui->textEdit_Send->toPlainText();
          socket->write(message.toUtf8()+"\n");
          ui->textEdit_Message->append("<font color=\"Purple\">Client:"+message.toUtf8()+" '</font>");
          ui->textEdit_Send->clear();
      }
      
      
      void Client::SocketConnected(){
      
          qDebug() << "Connected to server.";
          QString name=socket->peerName();
          int port =socket->peerPort();
          ui->textEdit_Message->append("<font color=\"Green\">Connected to:" +name+":"+ QString::number(port)+"</font>");
          ui->pushButton_Connect->setText("Disconnect");
          connectedToHost=true;
      }
      
      void Client:: SocketDisconnected(){
      
          qDebug() << "Disconnected from server.";
          ui->textEdit_Message->append("<font color=\"Red\">Disconnected from server.</font>");
          ui->pushButton_Connect->setText("Connect");
          connectedToHost = false;
      }
      
      void Client::SocketReadyRead(){
      
          ui->textEdit_Message->append("<font color=\"Blue\">Server: ' "+socket->readAll()+ "'");
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 16 Mar 2021, 13:09 last edited by
      #2

      @ozie said in TCP/IP server and client examples are working only same computer:

      I 've read something that I should create a port for my computer's ip in modem. How can I do that ?

      Indeed as you say. The server-side needs to allow a port number through for the client to connect on. That means the router must allow that incoming port through, and also if you have a software firewall that must allow the port number through too. These are things you do in your router/OS/firewall/virus scanner, not things you do in Qt.

      1 Reply Last reply
      2
      • O ozie
        16 Mar 2021, 12:54

        Hi everyone , I've been trying to make a server and a client applications that communicate each other. Actually its works like that I want but only same computer and using local ip. I need to communicate different computers. I 've read something that I should create a port for my computer's ip in modem. How can I do that ?

        Would be appreciated for your helps.

        Server.cpp file

        
        Server::Server(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::Server)
        {
            ui->setupUi(this);
        
        }
        
        Server::~Server()
        {
            delete ui;
        }
        
        void Server::on_pushButton_Start_clicked(){
            allClients=new QVector<QTcpSocket*>;
            chatserver=new QTcpServer();
            chatserver->setMaxPendingConnections(10);
            connect(chatserver,SIGNAL(newConnection()),this,SLOT(NewClientConnection()));
        
            /*///////////////////////////////////////////////////////////////////////////////////*/
            QString ipAddr;
            QList<QHostAddress> ipAddrList=QNetworkInterface::allAddresses();
            for (int i = 0; i < ipAddrList.size(); ++i) {
                if (ipAddrList.at(i) != QHostAddress::LocalHost && ipAddrList.at(i).toIPv4Address()) {
                    ipAddr = ipAddrList.at(i).toString();
                    break;
                }
        
            }
        
            if (ipAddr.isEmpty())
                ipAddr = QHostAddress(QHostAddress::LocalHost).toString();
            ui->textEdit_Message->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n").arg(ipAddr).arg(ui->textEdit_Port->toPlainText()));
                /*///////////////////////////////////////////////////////////////////////////////////*/
            if(chatserver->listen(QHostAddress::AnyIPv4,quint16(ui->textEdit_Port->toPlainText().toInt()))){
        
              //  ui->textEdit_Message->append("Server has started. Listening to port "+ui->textEdit_Port->toPlainText());
                qDebug() << "Server has started. Listening to port "+ui->textEdit_Port->toPlainText();
            }
            else{
        
                ui->textEdit_Message->setText("Server failed to start. Error: " + chatserver->errorString());
                qDebug() << "Server failed to start. Error: " + chatserver->errorString();
            }
        }
        
        
        
        void Server::on_pushButton_Send_clicked(){
        
            QString data =ui->textEdit_Send->toPlainText();
            SendMessageToClients(data);
            ui->textEdit_Message->append("<font color=\"Blue\">Server: ' "+ data + " ' </font>");
            ui->textEdit_Send->clear();
        }
        
        void Server::SendMessageToClients(QString message){
        
            if(allClients->size()>0)
        
                for (int i=0;i<allClients->size();i++ )
        
                    if(allClients->at(i)->isOpen() && allClients->at(i)->isWritable())
        
                        allClients->at(i)->write(message.toUtf8());
        
        
        }
        
        void Server:: NewClientConnection(){
        
            QTcpSocket* Client = chatserver->nextPendingConnection();
            QString ipAddress=Client->peerAddress().toString();
            int port=Client->peerPort();
            connect(Client,&QTcpSocket::disconnected,this,&Server::SocketDisconnected);
            connect(Client, &QTcpSocket::readyRead,this, &Server::SocketReadReady);
            connect(Client, &QTcpSocket::stateChanged, this, &Server::SocketStateChanged);
        
            allClients->push_back(Client);
        
            ui->textEdit_Message->append("<font color=\"Green\">Client connected from " + ipAddress + ":" + QString::number(port)+"</font>");
            qDebug() << "Client connected from " + ipAddress + ":" + QString::number(port);
        
        }
        
        void Server::SocketDisconnected(){
        
            QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
            QString SocketIpAddress=Client->peerAddress().toString();
            int port=Client->peerPort();
            ui->textEdit_Message->append("<font color=\"Red\">Socket disconnected from " + SocketIpAddress + ":" + QString::number(port)+"</font>");
            qDebug() << "Socket disconnected from " + SocketIpAddress + ":" + QString::number(port);
            allClients->removeOne(Client);
        }
        
        void Server::SocketReadReady(){
        
            QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
            QString SocketIpAddress=Client->peerAddress().toString();
            int port=Client->peerPort();
            while(Client->canReadLine()){
        
                QString data =QString::fromUtf8(Client->readLine().trimmed());
                ui->textEdit_Message->append("<font color=\"Purple\"> ( " +SocketIpAddress + ":" + QString::number(port) + "): ' "+ data+" '</font>");
                qDebug() << "Message: " + data + " (" + SocketIpAddress + ":" + QString::number(port) + ")";
            }
        }
        
        void Server::SocketStateChanged(QAbstractSocket::SocketState state){
        
            QTcpSocket* Client = (QTcpSocket*)QMainWindow::sender();
            QString SocketIpAddress=Client->peerAddress().toString();
            int port=Client->peerPort();
            QString desc;
        
           if(state==QAbstractSocket::UnconnectedState)
               desc="The socket is not connected.\n";
           else if (state == QAbstractSocket::HostLookupState)
               desc = "The socket is performing a host name lookup.\n";
           else if (state == QAbstractSocket::ConnectingState)
               desc = "The socket has started establishing a connection.\n";
           else if (state == QAbstractSocket::ConnectedState)
               desc = "A connection is established.\n";
           else if (state == QAbstractSocket::BoundState)
               desc = "The socket is bound to an address and port.\n";
           else if (state == QAbstractSocket::ClosingState)
               desc = "The socket is about to close (data may still be waiting to be written).\n";
           else if (state == QAbstractSocket::ListeningState)
               desc = "For internal use only.\n";
        
           ui->textEdit_Message->append("<font color=\"Orange\"> Socket state changed ( " + SocketIpAddress + ":" + QString::number(port) + "): " + desc+"</font>");
           qDebug() << "Socket state changed (" + SocketIpAddress + ":" + QString::number(port) + "): " + desc;
        
        }
        
        
        

        Client.cpp file

        Client::Client(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::Client)
        {
            ui->setupUi(this);
            connectedToHost=false;
        }
        
        Client::~Client()
        {
            delete ui;
        }
        
        
        void Client::on_pushButton_Connect_clicked(){
        
            if(!connectedToHost){
                socket =new QTcpSocket();
        
                connect(socket,&QTcpSocket::connected,this,&Client::SocketConnected);
                connect(socket,&QTcpSocket::disconnected,this,&Client::SocketDisconnected);
                connect(socket,&QTcpSocket::readyRead,this,&Client::SocketReadyRead);
        
                socket->connectToHost(ui->textEdit_IP->toPlainText(),quint16(ui->textEdit_Port->toPlainText().toInt()));
            }
            else{
                QString name = ui->textEdit_IP->toPlainText();
        
                socket->write(name.toUtf8() + " has left the chat room.\n");
                socket->disconnectFromHost();
            }
        
        }
        
        void Client::on_pushButton_Send_clicked(){
        
            QString message = ui->textEdit_Send->toPlainText();
            socket->write(message.toUtf8()+"\n");
            ui->textEdit_Message->append("<font color=\"Purple\">Client:"+message.toUtf8()+" '</font>");
            ui->textEdit_Send->clear();
        }
        
        
        void Client::SocketConnected(){
        
            qDebug() << "Connected to server.";
            QString name=socket->peerName();
            int port =socket->peerPort();
            ui->textEdit_Message->append("<font color=\"Green\">Connected to:" +name+":"+ QString::number(port)+"</font>");
            ui->pushButton_Connect->setText("Disconnect");
            connectedToHost=true;
        }
        
        void Client:: SocketDisconnected(){
        
            qDebug() << "Disconnected from server.";
            ui->textEdit_Message->append("<font color=\"Red\">Disconnected from server.</font>");
            ui->pushButton_Connect->setText("Connect");
            connectedToHost = false;
        }
        
        void Client::SocketReadyRead(){
        
            ui->textEdit_Message->append("<font color=\"Blue\">Server: ' "+socket->readAll()+ "'");
        }
        
        
        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on 16 Mar 2021, 13:23 last edited by
        #3

        @ozie said in TCP/IP server and client examples are working only same computer:

        I 've read something that I should create a port for my computer's ip in modem. How can I do that ?

        I'd suggest you check any of the networking examples so you can have a better understanding of the concepts/how to approach client/server Qt applications.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on 17 Mar 2021, 07:48 last edited by
          #4

          You have to be a little bit more specific. Does your software work with different computers on the same local network? Here, you would use local IPs. I don't see any specific problem here. Only the operating system might have firewall settings preventing this. (In this case it would be important to state which OS you are using.)

          If you are trying to reach another computer over the internet, you only have the IP of the device (usually the router) directly connected to the internet. You have to configure the router to forward this port to the correct internal IP address. This is different for every router and some simpler routers don't support this option. Check with the manual for your router. There could be something like "port forwarding". Or on some systems there is only the option to specify a "demilitarized zone (DMZ)", which means that all ports are forwarded to a single IP.

          1 Reply Last reply
          0

          1/4

          16 Mar 2021, 12:54

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved