From Spectrum
Jump to navigation Jump to search

sendto (IXCALL 0x3E18) - send a message via a socket


Assembly language

    ld a, (v_sockfd)
    ld hl, BUF_SOCKINFO
    ld de, BUF_DATA
    ld bc, BUF_DATA_LEN
    ld ix, SENDTO
    call IXCALL


    #include <sys/types.h>
    #include <sys/socket.h>

    int sendto(int sockfd, void *buf, size_t len, int flags, struct sockaddr_in *to, socklen_t tolen);


The routine sendto() is used to transmit a message via a socket. Typically, it is used for sending datagrams using a socket of type SOCK_DGRAM, for UDP communication.

The socket descriptor is passed in the sockfd parameter. The parameter buf points to a buffer to send, and the length of the data to be sent is specified by the len parameter. At present, the flags parameter is unused, and should be set to 0. The to parameter is a pointer to a struct sockaddr_in, which contains the address (local port, remote port, remote address) for the message being sent, and the length of this structure is passed in the parameter tolen. Since this implementation only handles addresses of type sockaddr_in, the tolen parameter is not used. However, it is good practise to set the parameter.

The assembly language interface takes the socket descriptor in the A register. The register pair HL contains the address of the socket information block, an 8 byte block specifying address parameters. The DE register pair contains the address of the memory buffer to send, and the BC register pair contains the length of the block of data to send.

The contents of memory that are passed in HL are 8 bytes of address information, formatted as follows:

bytes 0,1,2,3 - the remote host's IP address
bytes 4,5     - the remote host's port number
bytes 6,7     - the local host's port number

Return values

On success, the C function returns the number of bytes actually sent, or -1 if the call failed.

The assembly language interface returns the number of bytes actually sent in BC on success. On failure, the carry flag is set and A contains the error code.