Bind
bind - HLCALL 0x3E0C - Bind a local address to a socket.
Synopsis
Assembly language
ld a, (socket_fd) ; The socket handle, as returned by socket ld de, 2000 ; Port number ld hl, BIND ; Jump table entry point - 0x3E0C call HLCALL jr c, .handle_error ; On error, carry flag is set
C
#include <sys/socket.h> #include <sys/types.h> int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
bind() gives the socket sockfd the local address my_addr. In this implementation, only the port address is used for the local address, and other parameters in the sockaddr structure are currently ignored. Fields are provided for compatibility.
It is normally necessary to assign a local address before a SOCK_STREAM can accept incoming connections. The port number passed to bind is the port that will be listened on when the listen call is made.
Return value
On success, the assembly language interface returns with the carry flag cleared. On error, the accumulator contains the error number and the carry flag is set.
On success, the C interface returns zero. On error, -1 is returned, and errno is set to the error number.
Limitations
The port number is the only local address that is used, since the hardware only supports a single internet protocol address; in the C interface, all other fields in the sockaddr structure are ignored. C programmers should still avoid invalid values in case these values are used in the future. It is acceptable to zero out unused values.
Example
Assembly language
ld c, SOCK_STREAM ; A reliable stream - TCP ld hl, SOCKET ; Call table for socket() - 0x3E00 call HLCALL ; Trigger ROM page in bit 7, a ; Check for a negative return code (MSB set) jr nz, .handle_error ; failed to allocate a socket if the return code < 0 ld (sock_fd), a ; save socket handle ld de, 2000 ; Bind to port 2000 ld hl, BIND call HLCALL jr c, .handle_error ; local address is now our current IP address, port 2000.
C
int sockfd; struct sockaddr_in addr; sock=socket(AF_INET, SOCK_STREAM, 0); if(sock > 0) { addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); /* provided for compatibility */ addr.sin_port = htons(2000); /* htons is a no-op, but a macro is provided for compatibility */ if(bind(sockfd, &addr, sizeof(addr)) < 0) { handle_error("bind"); } }