Hi,

I am quite new using QT and I am trying to implement a TPC-IP server- client. The server has to read every 0.1sec a line of a file with latitude and longitude and send it to the client. But right now I can connect server and client but the client receive nothing. Could someone help me?? Thanks in advance.

SERVER
main.cpp
Qt Code:
  1. #include <iostream>
  2. #include <QCoreApplication>
  3. #include "server.h"
  4.  
  5. int main(int argc, char *argv[]){
  6. QCoreApplication app(argc, argv);
  7. QString ipaddress = QString(argv[1]);
  8. int port = atoi(argv[2]);
  9. Server* server = new Server("127.0.0.1", 1234);
  10. int retVal = app.exec();
  11. return retVal;
  12. }
To copy to clipboard, switch view to plain text mode 

server.cpp

Qt Code:
  1. include "server.h"
  2. #include <iostream>
  3. #include <QTcpServer>
  4. #include <QTcpSocket>
  5. #include <QTimer>
  6. #include <QDateTime>
  7. #include <QSettings>
  8. #include "readdata.h"
  9.  
  10. Server::Server(QString ipAddress, int port, QObject *parent) :
  11. QObject(parent), mTcpServer(0), mTcpSocket(0)
  12. {
  13. mTcpServer = new QTcpServer(this);
  14. connect(mTcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
  15. if (!mTcpServer->listen(QHostAddress(ipAddress), port)) {
  16. std::cout << "Unable to start the server: " << mTcpServer->errorString().toStdString() << std::endl;
  17. return;
  18. }
  19.  
  20. std::cout << "The server is running on\n\nIP: "<< ipAddress.toStdString()
  21. << "\nport: " << mTcpServer->serverPort() << "\n\nRun the Client now.\n" << std::endl;
  22. }
  23.  
  24. void Server::newConnectionSlot()
  25. {
  26. mTcpSocket = mTcpServer->nextPendingConnection();
  27. connect(mTcpSocket, SIGNAL(disconnected()),
  28. mTcpSocket, SLOT(deleteLater()));
  29.  
  30. // setup timer to send data at a given interval
  31. mSendTimer = new QTimer(this);
  32. connect(mSendTimer, SIGNAL(timeout()),
  33. this, SLOT(sendSlot()));
  34. mSendTimer->start(40);
  35. }
  36.  
  37. void Server::sendSlot()
  38. {
  39. if(!mTcpSocket)
  40. return;
  41.  
  42. readPathData("/home/jorge/Desktop/INStest.kml", &latitude, &longitude);
  43.  
  44. QByteArray block;
  45. QDataStream out(&block, QIODevice::WriteOnly);
  46. out.setVersion(QDataStream::Qt_4_0);
  47. out << (quint16)0;
  48.  
  49. for(int i=0; i < latitude.size(); i++){
  50. positionTest.append(QString::number(latitude[i]) + " " + QString::number(longitude[i]) + /*" " + QString::number(height[i])+*/ "\n");
  51. data << positionTest[i];
  52. out << data.at(qrand() % data.size());
  53. out.device()->seek(0);
  54. out << (quint16)(block.size() - sizeof(quint16));
  55. qint64 written_lat = mTcpSocket->write(block, block.size());
  56. }
  57. }
To copy to clipboard, switch view to plain text mode 

server.h
Qt Code:
  1. #ifndef SERVER_H_
  2. #define SERVER_H_
  3.  
  4. #include <QObject>
  5. #include <QVector>
  6. #include <QStringList>
  7.  
  8. QT_BEGIN_NAMESPACE
  9. class QTcpServer;
  10. class QNetworkSession;
  11. class QTcpSocket;
  12. class QTimer;
  13. QT_END_NAMESPACE
  14.  
  15. class Server : public QObject
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. Server(QString ipAddress, int port, QObject *parent = 0);
  21.  
  22. private slots:
  23. void newConnectionSlot();
  24. void sendSlot();
  25.  
  26. private:
  27. QTcpServer *mTcpServer;
  28. QTcpSocket *mTcpSocket;
  29. QTimer *mSendTimer;
  30.  
  31.  
  32. QVector<double> latitude;
  33. QVector<double> longitude;
  34. QVector<double> height;
  35. QString latitudeTest;
  36. QString longitudeTest;
  37. QString heightTest;
  38. QVector<QString> positionTest;
  39.  
  40. };
  41.  
  42. #endif /* SERVER_H_ */
To copy to clipboard, switch view to plain text mode 

readData.cpp
Qt Code:
  1. #include <QFile>
  2. #include <QTextStream>
  3. #include "readdata.h"
  4. #include "QPoint"
  5.  
  6. void readPathData(QString filename, QVector<double>* latitude, QVector<double>* longitude)
  7. {
  8. QFile file;
  9. file.setFileName(filename);
  10. file.open(QIODevice::ReadOnly);
  11. QByteArray line = file.readLine(); //Read two first lines of the .kml file
  12. line = file.readLine();
  13.  
  14. while (!file.atEnd()) {
  15. QByteArray line = file.readLine();
  16. if(line[0] != '<')
  17. {
  18. double lon;
  19. double lat;
  20. double hei;
  21.  
  22. QList<QByteArray> values = line.split(',');
  23.  
  24. hei = atof(values.at(2).constData());
  25. lat = atof(values.at(1).constData());
  26. lon = atof(values.at(0).constData());
  27.  
  28. if (latitude)
  29. latitude->append(lat);
  30.  
  31.  
  32. if (longitude)
  33. longitude->append(lon);
  34. }
  35. }
  36. file.close();
  37. }
To copy to clipboard, switch view to plain text mode 

readData.h
Qt Code:
  1. #include <QString>
  2. #include <QVector>
  3.  
  4. #ifndef READDATA
  5. #define READDATA
  6.  
  7. void readPathData(QString filename, QVector<double>* latitude, QVector<double>* longitude/*, QVector<double>* height*/);
  8.  
  9. #endif // READDATA
To copy to clipboard, switch view to plain text mode 

CLIENT
main.cpp
Qt Code:
  1. #include <iostream>
  2. #include <QCoreApplication>
  3.  
  4. #include "client.h"
  5.  
  6. int main(int argc, char *argv[]){
  7. QCoreApplication app(argc, argv);
  8. QString ipaddress = QString(argv[1]);
  9. int port = atoi(argv[2]);
  10. Client* client = new Client("127.0.0.1", 1234);
  11. int retVal = app.exec();
  12. }
To copy to clipboard, switch view to plain text mode 

client.cpp
Qt Code:
  1. #include "client.h"
  2. #include <iostream>
  3. #include <QTcpSocket>
  4. #include <QSettings>
  5. #include <QDateTime>
  6.  
  7. Client::Client(QString ipAddress, int port, QObject *parent):
  8. QObject(parent), mTcpSocket(0), mIpAddress(ipAddress), mPort(port)
  9. {
  10. mTcpSocket = new QTcpSocket(this);
  11. connect(mTcpSocket, SIGNAL(readyRead()),
  12. this, SLOT(readSlot()));
  13. connect(mTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
  14. this, SLOT(displayErrorSlot(QAbstractSocket::SocketError)));
  15.  
  16. std::cout << "Connecting to ip: " << mIpAddress.toStdString() << " port: " << mPort << std::endl;
  17. mTcpSocket->connectToHost(mIpAddress, mPort);
  18. }
  19.  
  20. void Client::readSlot()
  21. {
  22. static qint64 starttime = QDateTime::currentMSecsSinceEpoch();
  23. static int frames = 0;
  24.  
  25. qint64 blockSize = 1440000; //in bytes
  26.  
  27. QDataStream in(mTcpSocket);
  28.  
  29. if (blockSize == 0) {
  30. if (mTcpSocket->bytesAvailable() < (int)sizeof(quint32))
  31. return;
  32. in >> blockSize;
  33. }
  34.  
  35. if (mTcpSocket->bytesAvailable() < 38)
  36. return;
  37.  
  38. QString nextPos;
  39. in >> nextPos;
  40. currentPos = nextPos;
  41.  
  42. char* data = (char*) malloc(blockSize);
  43. qint64 bytesRead = mTcpSocket->read(data, blockSize);
  44. free(data);
  45. qDebug() << "Data" << data;
  46. qDebug() << "Position" << currentPos;
  47.  
  48. }
  49.  
  50. void Client::displayErrorSlot(QAbstractSocket::SocketError socketError)
  51. {
  52. switch (socketError) {
  53. case QAbstractSocket::RemoteHostClosedError:
  54. break;
  55. case QAbstractSocket::HostNotFoundError:
  56. std::cout << "The host was not found. Please check the "
  57. "host name and port settings."<< std::endl;
  58. break;
  59. case QAbstractSocket::ConnectionRefusedError:
  60. std::cout << "The connection was refused by the peer. "
  61. "Make sure the fortune server is running, "
  62. "and check that the host name and port "
  63. "settings are correct."<< std::endl;
  64. break;
  65. default:
  66. std::cout << "The following error occurred: " << mTcpSocket->errorString().toStdString() << std::endl;
  67. break;
  68. }
  69. }
To copy to clipboard, switch view to plain text mode 

client.h
Qt Code:
  1. #ifndef CLIENT_H_
  2. #define CLIENT_H_
  3.  
  4. #include <QObject>
  5. #include <QAbstractSocket>
  6.  
  7. QT_BEGIN_NAMESPACE
  8. class QTcpSocket;
  9. QT_END_NAMESPACE
  10.  
  11. class Client : public QObject
  12. {
  13. Q_OBJECT
  14. public:
  15. Client(QString ipAddress, int port, QObject *parent=0);
  16.  
  17. private slots:
  18. void readSlot();
  19. void displayErrorSlot(QAbstractSocket::SocketError);
  20.  
  21. private:
  22. QTcpSocket *mTcpSocket;
  23. QString mIpAddress;
  24. int mPort;
  25. QString currentPos;
  26. };
  27.  
  28. #endif /* CLIENT_H_ */
To copy to clipboard, switch view to plain text mode