Does SocketSend always send the full buffer ?

 

Hi,

The documentation of SocketSend is really poor about the function behaviour : It says that the return value is the count of byte sent, but it does not say if it always 0 or the len of the buffer.

Is there situation where this value could be different, and the socket still open ?

And in this case, how would you send the remaining bytes ? I mean, how to to get a view of the original buffer offset by the count of bytes already sent ? (without copying it for obvious reasons).

 
hl037:

Hi,

The documentation of SocketSend is really poor about the function behaviour : It says that the return value is the count of byte sent, but it does not say if it always 0 or the len of the buffer.

Is there situation where this value could be different, and the socket still open ?

And in this case, how would you send the remaining bytes ? I mean, how to to get a view of the original buffer offset by the count of bytes already sent ? (without copying it for obvious reasons).

In the MQL5 documentation, the SocketSend() function indeed returns the number of bytes sent successfully. However, the behavior might differ based on various factors such as the underlying operating system, network conditions, and the state of the socket.

Here's a general scenario:

Success (Return value = Length of buffer): In ideal conditions, where the entire buffer is successfully sent, SocketSend() would return the length of the buffer. This indicates that all bytes were transmitted successfully.

Partial Send (Return value < Length of buffer): If the return value is less than the length of the buffer, it means that only a portion of the buffer was sent. This could happen due to network congestion, limitations on buffer size, or other factors. In this case, you'd need to send the remaining bytes starting from the position where the previous send operation left off.

To send the remaining bytes without copying the buffer, you can achieve this by using pointer arithmetic or by adjusting the pointer offset of your buffer. Here is an example for you:

int socketHandle = ...; // Obtain your socket handle
uchar buffer[] = { /* Your data */ }; // Define your buffer
int bufferSize = ArraySize(buffer);
int bytesSent = 0;
int remainingBytes = bufferSize;

while (remainingBytes > 0) {
    // Send remaining bytes starting from the position where the previous send operation left off
    int sent = SocketSend(socketHandle, buffer + bytesSent, remainingBytes);
    
    if (sent <= 0) {
        // Handle error, break the loop, or take appropriate action
        break;
    }
    
    // Update counters
    bytesSent += sent;
    remainingBytes -= sent;
}

So, 

  • buffer + bytesSent adjusts the buffer pointer to start from the position where the previous send operation left off.
  • remainingBytes keeps track of the number of bytes that are left to be sent.
  • The loop continues until all bytes are successfully sent or an error occurs.

By adjusting the pointer offset ( buffer + bytesSent ), you are now effectively sending the remaining bytes without copying the buffer.