简体   繁体   中英

zero copy in Qt application in C++

here is the program that I wrote to implement zero copy in a Qt application but getting some errors written below:

//client.h
#ifndef CLIENT1_H
#define CLIENT1_H

#include <QObject>
#include <QString>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QtNetwork>
class Client: public QObject
{
Q_OBJECT
public:
  Client(QObject* parent = 0);
  ~Client();
  void start(QString address, quint16 port);
public slots:
  void startTransfer();
private:
  QTcpSocket client;
};

#endif // CLIENT1_H

//client.cpp
#include "client1.h"
#include <QtNetwork/QHostAddress>
#include <MSWSockDef.h>
#include <MSWSock.h>
Client::Client(QObject* parent): QObject(parent)
{
  connect(&client, SIGNAL(connected()),
          this, SLOT(startTransfer()));
}

Client::~Client()
{
  client.close();
}

void Client::start(QString address, quint16 port)
{
  QHostAddress addr(address);
  client.connectToHost(addr, port);
}

void Client::startTransfer()
{
    int TransmitFile( client,
                      hfile,
                      NULL,
                      NULL,
                      NULL,
                      NULL,
                      TF_USE_SYSTEM_THREAD
                    );
}

//main.cpp
#include <QCoreApplication>
#include <fstream>
#include <iostream>
#include "client1.h"
#include <QtNetwork/QtNetwork>
#include <Windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    HFILE hfile;
    //Creates an instance of ofstream, and opens example.txt
    ofstream a_file ( "example.txt" );
    // Outputs to example.txt through a_file
    a_file<<"This text will now be inside of example.txt";
    // Close the file stream explicitly
    hfile = a_file;
    //a_file.close();
    //hfile = a_file.open("example.txt");
    //hfile = a_file.open("example.txt",ios_base::app);
    Client client;
    client.start("127.0.0.1", 8888);

    return a.exec();
}

The errors are as below:

C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\MSWSock.
error: C2061: syntax error : identifier 'LPWSAMSG'

C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\MSWSock.h
error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int

C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\MSWSock.h
error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I know there are errors in transmitfile() api but those can be removed,what about the above errors?`enter code here?

您可能需要包含Winsock2.h,可以试试吗?

Try to remove Mswsockdef.h include.

...Mswsockdef.h header file which is automatically included in the Mswsock.h header file. The Mswsockdef.h header file should never be used directly.

Source

完成上述所有操作后,只需运行一次qmake,就可以了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM