views:

529

answers:

3

Hello,

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;
    }
}
A: 

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();
Budric
+1  A: 

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).

Lou Franco
A: 

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.)