linux 到 windows C++ 字节数组
我必须在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果允许您使用 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).
我还没有使用过它,但是当涉及到编组更复杂的数据结构时,我会研究序列化部分的提升。
对于实际的数据传输,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.)
严格字节数组不需要从 Linux 到 Windows 或其他系统的任何转换。 但是,如果您正在处理整数和浮点数...
我个人会使用 Poco::BinaryWriter 和 Poco::BinaryReader
http://pocoproject.org/docs/Poco.BinaryWriter.html
现在阅读
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
Now to read