I cannot connect with server.
I create a server how this: http://www.youtube.com/watch?v=BSdKkZNEKlQ
I run the program. I see:
28.png
Server started!
I run "cmd.exe" and write:
then:
I see:
29.png
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
#-------------------------------------------------
#
# 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
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QCoreApplication>
#include "myserver.h"
int main(int argc, char *argv[])
{
MyServer mServer;
return a.exec();
}
#include <QCoreApplication>
#include "myserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyServer mServer;
return a.exec();
}
To copy to clipboard, switch view to plain text mode
myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
{
Q_OBJECT
public:
explicit MyServer
(QObject *parent
= 0);
signals:
public slots:
void newConnection();
private:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
myserver.cpp
#include "myserver.h"
MyServer
::MyServer(QObject *parent
) :{
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Thank you!
Bookmarks