linux 到 windows C++ 字节数组

发布于 2024-07-24 08:51:27 字数 1494 浏览 6 评论 0原文

我必须在 C++ 中复制以下 Java 功能才能将数据从 Linux 获取到 Windows。 Winsock2 是最好的选择吗?

另外,有什么参考代码可以推荐吗?

TIA, 乙

import java.nio.ByteBuffer;

public class MessageXdr {

    private ByteBuffer buffer;
    private int size;

    // taille max corps de message
    private static final int T_MAX_CORPS_MSG = 16384;

    public MessageXdr() {
        buffer = ByteBuffer.allocate(4 * T_MAX_CORPS_MSG);
        size =0;
    }

    public MessageXdr(byte[] array)
    {
        ByteBuffer tmpBuffer = ByteBuffer.wrap(array);
        buffer = tmpBuffer.asReadOnlyBuffer();
        size = array.length;
    }

    public int getSize()
    {
        return size;
    }

    public int getPosition()
    {
        return buffer.position();
    }

    public byte[] getArray()
    {
        return buffer.array();
    }

    public void resetBuffer()
    {
        size = 0;
        buffer.rewind();
    }

    public int readInt()
    {
        int retour = buffer.getInt();
        return retour;
    }

    public long readUnsignedInt()
    {
        ByteBuffer tmp = ByteBuffer.allocate(8);
        tmp.putInt(0);
        tmp.putInt(buffer.getInt());

        return tmp.getLong(0);
    }

    public float readFloat()
    {
        float retour = buffer.getFloat();
        return retour;
    }

    public void writeInt(int v)
    {
        buffer.putInt(v);
        size+=4;
    }

    public void writeFloat(float v)
    {
        buffer.putFloat(v);
        size+=4;
    }
}

I have to replicate the following Java functionality in C++ to get data from Linux to Windows. Is Winsock2 the best way to go?.

Also, any reference code to suggest?

TIA,
B

import java.nio.ByteBuffer;

public class MessageXdr {

    private ByteBuffer buffer;
    private int size;

    // taille max corps de message
    private static final int T_MAX_CORPS_MSG = 16384;

    public MessageXdr() {
        buffer = ByteBuffer.allocate(4 * T_MAX_CORPS_MSG);
        size =0;
    }

    public MessageXdr(byte[] array)
    {
        ByteBuffer tmpBuffer = ByteBuffer.wrap(array);
        buffer = tmpBuffer.asReadOnlyBuffer();
        size = array.length;
    }

    public int getSize()
    {
        return size;
    }

    public int getPosition()
    {
        return buffer.position();
    }

    public byte[] getArray()
    {
        return buffer.array();
    }

    public void resetBuffer()
    {
        size = 0;
        buffer.rewind();
    }

    public int readInt()
    {
        int retour = buffer.getInt();
        return retour;
    }

    public long readUnsignedInt()
    {
        ByteBuffer tmp = ByteBuffer.allocate(8);
        tmp.putInt(0);
        tmp.putInt(buffer.getInt());

        return tmp.getLong(0);
    }

    public float readFloat()
    {
        float retour = buffer.getFloat();
        return retour;
    }

    public void writeInt(int v)
    {
        buffer.putInt(v);
        size+=4;
    }

    public void writeFloat(float v)
    {
        buffer.putFloat(v);
        size+=4;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2024-07-31 08:51:27

如果允许您使用 MFC 类 (CSocket),它可能会更接近 Java 中的代码。

http://msdn.microsoft.com/en-us /library/wxzt95kb(VS.80).aspx

否则,Winsock2 就可以了(MFC 类只是在其实现中使用它)。

If you are allowed to use the MFC classes (CSocket), it might be closer to the code you have in Java.

http://msdn.microsoft.com/en-us/library/wxzt95kb(VS.80).aspx

Otherwise, Winsock2 is fine (the MFC classes just use that in their implementation).

久随 2024-07-31 08:51:27

我还没有使用过它,但是当涉及到编组更复杂的数据结构时,我会研究序列化部分的提升。

对于实际的数据传输,winsock2 是 Windows 中的基本套接字 api,所有其他 api 都是基于它构建的(嗯,不知道 Windows 7)。但是,研究 boost 可以为您提供一些您不知道的独立于平台的东西不必弄清楚两次。 但根据我的经验,套接字是复杂的野兽,所以无论如何你都必须弄清楚很多事情......

并避免使用 MFC 中的 CSocket,这是有史以来最糟糕的实现。 (即使有人说他们纠正了一些不当行为,但这也是不值得的。)

I haven't worked with it yet, but when it comes to marshalling more complex data structures i would look into boost for the serialization part.

For the actual data transmission, winsock2 is the basic socket api in windows, all other api's are built on it (well, don't know about Windows 7) .But again, looking into boost could provide you with something platform independent you don't have to figure out twice. But from my experience, sockets are complex beasts, so you will have to figure out a lot anyway...

And avoid the CSocket from MFC, that's the worst implementation ever. (Even if some say that they fixed some of it's misbehaviours, it's just not worth it.)

蝶…霜飞 2024-07-31 08:51:27

严格字节数组不需要从 Linux 到 Windows 或其他系统的任何转换。 但是,如果您正在处理整数和浮点数...

我个人会使用 Poco::BinaryWriter 和 Poco::BinaryReader
http://pocoproject.org/docs/Poco.BinaryWriter.html

using namespace Poco;
using namespace std;
std::ofstream myFile("path", ios::in | ios::binary);
BinaryWriter writer(myFile, BIG_ENDIAN_BYTE_ORDER);
writer << 10.0f; 
writer << 10000; 
//etc etc
myFile.close();

现在阅读

std::ifstream myFile("path", ios::in | ios::binary);
BinaryReader reader(myFile, BIG_ENDIAN_BYTE_ORDER);
int intVariable;
float floatVariable;
reader >> floatVariable;
reader >> intVariable;
//etc etc
myFile.close();

Strict byte arrays don't need any translation from linux to windows or other systems. If you are dealing with integers and floats however...

Personally I would use Poco::BinaryWriter and Poco::BinaryReader
http://pocoproject.org/docs/Poco.BinaryWriter.html

using namespace Poco;
using namespace std;
std::ofstream myFile("path", ios::in | ios::binary);
BinaryWriter writer(myFile, BIG_ENDIAN_BYTE_ORDER);
writer << 10.0f; 
writer << 10000; 
//etc etc
myFile.close();

Now to read

std::ifstream myFile("path", ios::in | ios::binary);
BinaryReader reader(myFile, BIG_ENDIAN_BYTE_ORDER);
int intVariable;
float floatVariable;
reader >> floatVariable;
reader >> intVariable;
//etc etc
myFile.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文