【问题标题】:Data Transferring Problem from Client to Server in C++C++中从客户端到服务器的数据传输问题
【发布时间】:2019-10-02 11:52:27
【问题描述】:

我有一个使用 UDP 数据报向服务器发送数组数据的客户端。为了能够追踪我发送的数据,我用数字填充了我的数组。例如; 在客户端代码中我填buf[7] = 7;这个索引是为了表示分片的总数。我希望在服务器中也看到相同的。

但是,我的buf 在服务器中 没有得到这些值。 我必须在服务器中使用固定大小的字符数组 我无法更改它。 (我对数据进行了长时间的计算,因为与问题无关,所以我没有放)

我要解释一下服务器结构:

  • 我在排队。队列包含 3 个缓冲区。如果我获取数据太快并且 如果buf 已满且其数据仍在文件中,则应使用buf2 等等。这是为了优化目的。
  • 我使用一个线程,它将当前缓冲区数据写入到一个文件中 并行方式。我向线程发出信号,以便它知道何时开始 写作。

我怀疑线程,当我评论它时,收到功能卡住。 我怀疑类型的数组,因为我发送 char 指针数组但接收到固定大小的 char 数组。 (再次接收时我必须使用固定尺寸)

我只想看到正确传输的数据。当我看 服务器 我想在相同的索引上看到相同的数据。

这是我的简单客户端代码

void main(int argc, char* argv[]){
    WSADATA data;
    WORD version = MAKEWORD(2, 2);

    int wsOk = WSAStartup(version, &data);
    if (wsOk != 0)
    {
        cout << "Can't start Winsock! " << wsOk;
        return;
    }

    sockaddr_in server;
    server.sin_family = AF_INET; // AF_INET = IPv4 addresses
    server.sin_port = htons(...port no...); // Little to big endian conversion
    inet_pton(AF_INET, "...IP....", &server.sin_addr); // Convert from string to byte array

    SOCKET out = socket(AF_INET, SOCK_DGRAM, 0);
    int sendOk = 0;

    int *buf = new int[1450];

    //just fill it with ordered number to be able to trace from other side
    for (int i = 0; i < 1450; i++)
    {
        buf[i] = i;
    }

    buf[7] = 7; // this index represent TotalFragmentCount in UDP

    while (true) {

        for (int fragmentNumber = 1; fragmentNumber < 8; fragmentNumber++) {
            //I want to set this index to count fragment number when I look at server
            buf[9] = fragmentNumber;
            //I give it as char * array with reinterpret_cast, since sendto accepts pointer char array
            sendOk = sendto(out, reinterpret_cast<char*>(&buf), 1450, 0, (sockaddr*)& server, sizeof(server));
        }

    }

    if (sendOk == SOCKET_ERROR)
    {
        cout << "That didn't work! " << WSAGetLastError() << endl;
    }

    closesocket(out);
    WSACleanup();

}

这是我的服务器代码

void writeToFile(char buf[], vector<uint16_t> &intData, ofstream &file) {


    while (true) {

        if (signaled == 1) {
            thread_mutex.lock();
            //writing...
            for (const auto& e : intData) {
                file << e << "\n";
            }
            buffers.pop(); // pops front which is written by this time
            buffers.push(buf);
            thread_mutex.unlock();
            break;
        }
        else{
            continue;
        }
    }
}
// Main entry point into the server
void main()
{
    WSADATA data;
    WORD version = MAKEWORD(2, 2);

    int wsOk = WSAStartup(version, &data);
    if (wsOk != 0)
    {
        // Not ok! Get out quickly
        cout << "Can't start Winsock! " << wsOk;
        return;
    }

    // Create a socket, notice that it is a user datagram socket (UDP)
    SOCKET in = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    sockaddr_in serverHint;
    serverHint.sin_addr.S_un.S_addr = ADDR_ANY; // Us any IP address available on the machine
    serverHint.sin_family = AF_INET; // Address format is IPv4
    serverHint.sin_port = htons(4660); // Convert from little to big endian

    // Try and bind the socket to the IP and port
    if (bind(in, (sockaddr*)&serverHint, sizeof(serverHint)) == SOCKET_ERROR)
    {
        cout << "Can't bind socket! " << WSAGetLastError() << endl;
        return;
    }

    sockaddr_in client; // Use to hold the client information (port / ip address)
    int clientLength = sizeof(client); // The size of the client information


    char buf[1550] = { 0 }; //message gets here
    char buf2[1550]= { 0 };
    char buf3[1550]= { 0 };

    // Control buffer traffic with queue
    buffers.push(buf);
    buffers.push(buf2);
    buffers.push(buf3);

    std::thread thread_write(writeToFile, std::ref(buffers.front()), std::ref( intData), std::ref(file)); //front returns first element of the queue

    bytesIn = recvfrom(in, buffers.front(), 1550, 0, (sockaddr*)& client, &clientLength);
    cout << "";
    while (ReceivedFrameCount != 100)
    {
            ZeroMemory(&client, clientLength); // Clear the client structure

            // Wait for message
            bytesIn = recvfrom(in, buffers.front(), 1550, 0, (sockaddr*)& client, &clientLength);

            if (bytesIn == SOCKET_ERROR)
            {
                cout << "Error receiving from client " << WSAGetLastError() << endl;
                continue;
            }

            // Parse the byte array

            TotalFragmentCount = (uint16_t)(buf[6] << 8 | buf[7]);
            FrameFragmentNo = (uint16_t)(buf[8] << 8 | buf[9]);

            signaled = 1;


    }// end of while 


    // Close socket
    closesocket(in);

    file.close();
    WSACleanup();

    thread_write.join();
}

【问题讨论】:

    标签: c++ server client


    【解决方案1】:

    reinterpret_cast&lt;char*&gt;(&amp;buf) 不会做你认为的那样。应该是reinterpret_cast&lt;char*&gt;(buf)。而1450 应该是1450*sizeof(int),因为你有 1450 个 int,每个 int 超过一个字节。

    sendto 需要一个指向它将发送的数据的指针。如果您将它传递给&amp;buf(指向buf 的指针),那么您将发送指针buf。如果您将其传递给buf(指向缓冲区的指针),那么您正在发送缓冲区。


    当您构建一个整数数组时,您的计算机将它们作为字节存储在内存中:(也许 - 它是特定于平台的!)

    Byte  0: 0 \
    Byte  1: 0 |
    Byte  2: 0 | int 0
    Byte  3: 0 /
    Byte  4: 1   \
    Byte  5: 0   |
    Byte  6: 0   | int 1
    Byte  7: 0   /
    Byte  8: 2     \
    Byte  9: 0     |
    Byte 10: 0     | int 2
    Byte 11: 0     /
    

    当服务器将这些视为字节时,它将在索引 8 处看到数字 2,依此类推。如果您希望它匹配,那么也许客户端应该创建一个字节数组(字符,与服务器相同)而不是整数。

    【讨论】:

    • 我理解并应用了它,但它仍然不起作用,我应该在服务器中进行更改,大约 char 数组数组的大小为 1550
    • 什么不起作用?由于您的客户端正在发送整数,而您的服务器将它们视为字节,因此内存布局将不一样。当您将其读取为字符时,您的 7 可能位于索引 28 或 31 或 56 或 63 处。也许您也应该让客户端发送字符?
    • 但是客户端已经用这个reinterpret_cast&lt;char*&gt;发送字符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多