简体   繁体   English

创建临时文件时出错(创建客户端/服务器应用程序,客户端使用 DirectSound 从服务器流式传输 WAV 文件)

[英]Error creating temp file (Creating a client/server application that the client streams WAV files from the server using DirectSound )

For a project for my university I have to create a client/server application where the client and the server communicate through sockets and the client streams WAV files from the server using DirectSound API对于我大学的一个项目,我必须创建一个客户端/服务器应用程序,其中客户端和服务器通过 sockets 进行通信,客户端使用 DirectSound API 从服务器流式传输 WAV 文件

when I am trying to play a WAV file I am getting this error "Error creating temp file"当我尝试播放 WAV 文件时出现此错误“创建临时文件时出错”

that is the code for creating the temp file那是创建临时文件的代码

// Receive the list of files from the server
    char buffer[256] = { 0 };
    int bytes_received = recv(socket_client, buffer, sizeof(buffer), 0);
    if (bytes_received == SOCKET_ERROR) {
        std::cout << "Error receiving file list" << std::endl;
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }
    std::string file_list(buffer, bytes_received);

    std::cout << "Available files: " << std::endl << file_list << std::endl;

    // Ask the user to choose a file
    std::string chosen_file;
    std::cout << "Enter the name of the file you want to play: ";
    std::cin >> chosen_file;

    // Send the chosen file to the server
    if (send(socket_client, chosen_file.c_str(), chosen_file.size(), 0) == SOCKET_ERROR) {
        std::cout << "Error sending chosen file" << std::endl;
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }
    // Receive the file size from the server
    int file_size;
    if (recv(socket_client, reinterpret_cast<char*>(&file_size), sizeof(file_size), 0) == SOCKET_ERROR) {
        std::cout << "Error receiving file size" << std::endl;
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

    // Create a temporary file to store the received data
    std::string file_path = "path/to/temp/file.wav";
    std::ofstream temp_file(file_path, std::ios::binary);
    if (!temp_file.is_open()) {
        std::cout << "Error creating temp file" << std::endl;
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

    // Receive the file from the server in chunks
    const int CHUNK_SIZE = 1024;
    char chunk[CHUNK_SIZE];
    int total_bytes_received = 0;
    while (total_bytes_received < file_size) {
        bytes_received = recv(socket_client, chunk, CHUNK_SIZE, 0);
        if (bytes_received == SOCKET_ERROR) {
            std::cout << "Error receiving file" << std::endl;
            temp_file.close();
            closesocket(socket_client);
            WSACleanup();
            return 1;
        }
        temp_file.write(chunk, bytes_received);
        total_bytes_received += bytes_received;
    }
    temp_file.close();
    std::cout << "File received: " << chosen_file << std::endl;

The error "Error creating temp file" suggests that there is an issue with creating a temporary file to store the received WAV file before it is played.错误“Error creating temp file”表明在播放之前创建临时文件来存储接收到的 WAV 文件时存在问题。 This error could be caused by a number of things, such as a lack of permissions to write to the directory where the temporary file is being created, or a problem with the file path specified for the temporary file.此错误可能由多种原因引起,例如缺少写入正在创建临时文件的目录的权限,或者为临时文件指定的文件路径有问题。

It's possible that the temporary file is not being created because the client is not receiving the file size correctly.可能因为客户端未正确接收文件大小而未创建临时文件。 You can check if the value of the variable 'file_size' is correct.您可以检查变量“file_size”的值是否正确。 If the recv() function is not able to retrieve the correct file size, the rest of the code will not work as expected.如果 recv() function 无法检索到正确的文件大小,则代码的 rest 将无法按预期工作。

It's also possible that the client is not able to receive the file list from the server, hence the error message "Error receiving file list".客户端也可能无法从服务器接收文件列表,因此出现错误消息“接收文件列表时出错”。 Check the value of 'bytes_received' variable to confirm that the value is not equal to SOCKET_ERROR.检查“bytes_received”变量的值以确认该值不等于 SOCKET_ERROR。

Additionally, it would be helpful to check for the return value of the send() function in order to make sure that the client is sending the chosen file's name correctly to the server.此外,检查 send() 的返回值 function 以确保客户端将所选文件的名称正确发送到服务器将很有帮助。

You can add some debugging statements in your code to help you understand what might be causing the error.您可以在代码中添加一些调试语句,以帮助您了解可能导致错误的原因。 You can also check the windows event viewer for more information about the error.您还可以检查 windows 事件查看器以获取有关该错误的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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