1

这是我为在 Qt 应用程序中实现零拷贝而编写的程序,但出现以下错误:

//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();
}

错误如下:

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

我知道transmitfile() api有错误,但是可以删除,上面的错误怎么办?`在这里输入代码?

4

3 回答 3

1

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

于 2013-06-19T08:04:06.787 回答
1

尝试删除Mswsockdef.h包含。

...Mswsockdef.h 头文件,它自动包含在 Mswsock.h 头文件中。永远不要直接使用 Mswsockdef.h 头文件。

来源

于 2013-06-19T08:05:13.607 回答
1

完成上述所有操作后,只需运行一次 qmake,它就会起作用。

于 2013-07-18T09:47:15.083 回答