PDA

View Full Version : I cannot connect with server



8Observer8
14th August 2013, 12:47
I cannot connect with server.

I create a server how this: http://www.youtube.com/watch?v=BSdKkZNEKlQ

I run the program. I see:

9415
Server started!

I run "cmd.exe" and write:


telnet

then:


open 0.0.0.0 1234

I see:

9416

MyFirstServer.pro


#-------------------------------------------------
#
# Project created by QtCreator 2013-08-14T15:00:25
#
#-------------------------------------------------

QT += core
QT += network
QT -= gui

TARGET = MyFirstServer
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
myserver.cpp

HEADERS += \
myserver.h


main.cpp


#include <QCoreApplication>
#include "myserver.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

MyServer mServer;

return a.exec();
}


myserver.h


#ifndef MYSERVER_H
#define MYSERVER_H

#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>

class MyServer : public QObject
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);

signals:

public slots:
void newConnection();

private:
QTcpServer *server;
};

#endif // MYSERVER_H


myserver.cpp


#include "myserver.h"

MyServer::MyServer(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);

connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));

if (!server->listen(QHostAddress::Any, 1234)) {
qDebug() << "Server could not start!";
}
else {
qDebug() << "Server started!";
qDebug() << "Address: " << server->serverAddress();
qDebug() << "Port: " << server->serverPort();
}
}

void MyServer::newConnection() {
QTcpSocket *socket = server->nextPendingConnection();

socket->write("hello client\r\n");
socket->flush();

socket->waitForBytesWritten(3000);

socket->close();
}


Thank you!

wysota
14th August 2013, 13:36
Do you understand what "0.0.0.0" means?

hansenmartin
14th August 2013, 14:39
Hi all,
If any one has any idea about the problem which is mentioned by 8Observer8. please help i have the same problem.

8Observer8
14th August 2013, 15:02
Do you understand what "0.0.0.0" means?

Yes, it's ip address.

wysota
14th August 2013, 15:19
Yes, it's ip address.

What kind of ip address? Do you understand what it points to? You will never be able to connect to 0.0.0.0.

8Observer8
14th August 2013, 15:38
> What kind of ip address? Do you understand what it points to?

I do not understand. It's new for me. How to know this ip address?

anda_skoa
14th August 2013, 15:54
You probably want to connect to 127.0.0.1, usually one of the addresses associated with the system's loopback device.

Since you listen to "any address" this one should be one of them.

0.0.0.0 is sometimes used to mean "any address" in the context of listening on a server socket or binding a receiving UDP socket. It is never used as an address for connecting to a server.

Cheers,
_

8Observer8
14th August 2013, 16:48
You probably want to connect to 127.0.0.1, usually one of the addresses associated with the system's loopback device.

Since you listen to "any address" this one should be one of them.

0.0.0.0 is sometimes used to mean "any address" in the context of listening on a server socket or binding a receiving UDP socket. It is never used as an address for connecting to a server.

Cheers,
_

Thank you very much!

Author used it in the video. How did I not notice...

9417