Hello, please tell me, I'm trying to create a "chat client" . I got to the point where I create a server and go to the client itself from the first computer.

But my problem is that when I connect the second computer to the created server (который запущен на первом компьютере) , the connection does not occur .

It turns out this situation:

(server running)

enter image description here

On the first computer:

enter image description here

On the second: But connecting (launching only the client), Connect does not occur. Port number and localhost remain the same when connected. To add a connection to the "server":

 void Widget::addConnection() { QTcpSocket* connection = server->nextPendingConnection(); connections.append(connection); //Them ket noi vao danh sach QBuffer *buffer = new QBuffer(this); //Tao bo dem du lieu cho connection nay buffer->open(QIODevice::ReadWrite); buffers.insert(connection, buffer); //Luu vao danh sach connect(connection, SIGNAL(disconnected()), this, SLOT(removeConnection())); connect(connection, SIGNAL(readyRead()), this, SLOT(receiveMessage())); } 

I do not know how best to show the code, so that it is easier to understand and to help you solve the problem. I'll post it like this:

Server:

https://www.dropbox.com/sh/vilapm4sr1laimi/AAD5G0USlLf5UayQq2zHb9ZUa?dl=0

Customer:

https://www.dropbox.com/sh/fx9jtzvtmeepzry/AABcDbvFU4ha4skHQP9AxK1la?dl=0

I collect on Qt 5.3 under Windows.

  • one
    On the second machine, you need to connect not to localhost , this is at least. At the same time, check if the correct port is open on both machines. - Streletz

1 answer 1

Your problem is that [localhost][1] (also known as 127.0.0.1) is the special address of the machine you are working on. So a localhost request on the client means trying to connect to the server on the same machine.

So in the client you need to specify the real address of your server, well, and the server itself, respectively, to connect to listen with the real address ( bind() when working with sockets; how this is solved in Qt - see for yourself).

  • Everything became clear, thanks! But you can ask where to get the real address of my server? I do not understand this, I would like to understand. If not difficult, please explain .. - Nikita Gusev