tags:

views:

92

answers:

1

I am writing a proxy application for iSCSI to do some diagnostic work (think Fiddler for iSCSI) - I'm trying to capture data one packet at a time, I don't want to read arbitrary sizes and end up getting all of one iSCSI packet and half of another - I really want to see the same kind of data as Wireshark would display for example. In doing this, I'm using Socket.ReceiveMessageFrom().

However, one of the parameters is called "endpoint", and I'm not quite sure what to do with it. Any clues? Here's my code, can you tell me if I'm completely off base:

Tuple<byte[], int> readOnePacket(TcpClient conn) {
    var flags = SocketFlags.None;
    EndPoint endpoint = null;   /*** You can't set this to null!! ***/
    byte[] buffer = new byte[10 * 0x100000];
    int offset = 0;
    int bytes_received;

    do {
        IPPacketInformation packet_information;
        bytes_received = conn.Client.ReceiveMessageFrom(buffer, offset, BufferSize,
                                                        ref flags, ref endpoint, out packet_information);

        if (flags == SocketFlags.Partial) {
            // We only want to transfer full packets
            offset = bytes_received;
            continue;
        }                        
    } while (false);

    return new Tuple<byte[], int>(buffer, bytes_received + offset );
}
+1  A: 

It is not TcpClient.ReceiveMessageFrom(), but Socket.ReceiveMessageFrom()

If you take a look at the documentation, you will read the following:

An EndPoint, passed by reference, that represents the remote server.

Edit: Setting it to null is, indeed, a bad idea.

Bertrand Marron
Question updated
Paul Betts
You need to follow the documentation, and use "EndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);" and pass "endpoint" to the function. Socket.ReceiveMessageFrom will modify this object and you will see the object after the call and understand what it is for.
Lex Li