如何知道memcpy()函数在完成打包过程中成功复制了内存? [英] How to know the memcpy() function has successfully copied the memory during completing pakage?

查看:98
本文介绍了如何知道memcpy()函数在完成打包过程中成功复制了内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 tcp/ip ssh 隧道将包含 120000 的向量从我的计算机发送到服务器.每次,我发送 250 个 double 类型的值,服务器将它们发回给我以确保我正确发送了数据.我使用函数 read() 来接收服务器中的数据.正如我们所知, read() 不能总是一次收到所有 250 个值(250*8=2000 字节).因此,我使用函数 memcpy() 来保存接收到的数据,直到它达到 2000 字节.但是,memcpy 只能工作一次.

I send a vector containing 120000 from my computer to a server by tcp/ip ssh tunnel. Every time, I send 250 values of type double and the sever send me back them to ensure that I have sent the data correctly. I use the function read() to recieve the data in the server. As we know, read() cannot always recieve all the 250 values (250*8=2000bytes) in one time. Thus, I use the function memcpy() to save the recieved data until it reach 2000 bytes. However, the memcpy only work one times.

例如,我发送了 250 个值(2000 字节).服务器在第一次收到 1408 字节.我使用 memcpy() 将这 1406 个字节从缓冲区保存到一个数组中.服务器在第二次收到 594 字节.我使用 memcpy() 将这 592 个字节从缓冲区保存到同一个数组中.但是,我发现第二次,memcpy() 根据从服务器发送回我的计算机的值不起作用.

For example, I send 250 values (2000bytes). The server recieves 1408 bytes in the 1st time. I use memcpy() to save these 1406 bytes into a array from the buffer. The server recieves 594 bytes in the 2nd time. I use memcpy() to save these 592 bytes into the same array from the buffer. However, I find the 2nd time, memcpy() does not work according to the value send back from server to my computer.

server 中的代码 c++ 有两个目标:1. 每次接收250条数据.2. 每次都送回来.

The code c++ in server has two objectives: 1. recieve the 250 data every times. 2. send them back every times.

#include <stdio.h>
#include <iostream>
#include<vector>
#include <math.h>

extern "C"
void useCUDA();

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

using namespace std;



int main()
{

    vector<double> Y;

    int lread = 250, nMu = 4, ltotal = 120000;


    int sock = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  
    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
    serv_addr.sin_port = htons(54321); 
    connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));


    double* block_buffer_input;
    block_buffer_input = new double[lread];
    double* block_input;
    block_input = new double[lread];
    double* block_buffer_output;
    block_buffer_output = new double[lread];


    while (Y.size() < ltotal)
    {
        int nbyteread = 0;
        int nbytereadtimes = 0;

        while (nbyteread < 8 * lread)
        {

            nbytereadtimes = read(sock, reinterpret_cast<char*>(block_buffer_input), lread * sizeof(double));


            memcpy(block_input + nbyteread, block_buffer_input, nbytereadtimes);

            if (nbytereadtimes != 8 * lread && nbytereadtimes != 0)
                cout << Y.size() << ": " << nbytereadtimes << " " << block_input + nbyteread <<endl;


            nbyteread += nbytereadtimes;
        }

        Y.insert(Y.end(), &block_input[0], &block_input[lread]);

        cout << Y.size() << ": " << nbyteread << endl;


        int Sp = Y.size() - lread;
        for (int i = 0; i != lread; ++i)
        {
            block_buffer_output[i] = Y[Sp + i];
        }
        write(sock, (char*)block_buffer_output, lread * sizeof(double));



    }


    delete[] block_buffer_input;
    delete[] block_input;
    delete[] block_buffer_output;
    close(sock);


    return 0;
}

我想知道为什么第二次 memcpy() 不起作用.

I want to know why the memcpy() do not work in the 2nd time.

推荐答案

如果将 nbyteread 添加到指向双精度数组的指针,实际上是指 nbyteread 的地址 * sizeof(double)(nbyteread 元素的地址)

If you adding nbyteread to the pointer to an array of doubles you actually referring to address of nbyteread * sizeof(double) (address of nbyteread element)

memcpy(block_input + nbyteread, block_buffer_input, nbytereadtimes);

这篇关于如何知道memcpy()函数在完成打包过程中成功复制了内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆