Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c/c++ windows api socket wrappers

namespace wrappers {
    ssize_t sender_wrapper(SOCKET &sock, char *buff, size_t size) {
        size_t sent = 0;
        ssize_t result = 0;

        do {
            result = send(sock, buff, size, 0);
            if (result > 0) {
                buff += result;
                size -= (size_t) result;
            } else if (result == 0) {
                // remote disconnected
            } else {
                // error
            }
        } while (result > 0 && size > 0);
        return result;
    }
    ssize_t receive_wrapper(SOCKET &sock, char *buff, size_t size) {
        size_t recved = 0;
        ssize_t result = 0;

        do {
            result = recv(sock, buff, size, 0);
            if (result > 0) {
                buff += result;
                size -= (size_t) result;
            } else if (result == 0) {
                return -1;
            } else {
                return -1;
                // error
            }
        } while (result > 0 && size > 0);

        return result;
    }
}
 
PREVIOUS NEXT
Tagged: #windows #api #socket #wrappers
ADD COMMENT
Topic
Name
3+8 =