Connect

From Spectrum
Jump to navigation Jump to search

connect (HLCALL 0x3E0F) - initiate a connection on a socket

Synopsis

Assembly language

ld a, (sock_fd)
ld de, DEST_IP_BUF
ld bc, DEST_PORT
ld hl, CONNECT
call HLCALL

C

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

int connect(int sockfd, struct sockaddr *serv_addr, socklen_t addrlen);

Description

The connect() call connects the socket referred to by the file descriptor sockfd to the address specified in serv_addr. The only address format currently supported is the struct sockaddr_in IPv4 address and port.

When using the assembly language call, the DE register pair points to the start of the 4 byte buffer in memory containing the remote host's IPv4 address, as a 4 byte big endian value. The BC register pair contains the 16 bit port number for the remote host. The connect call translates the port number into network byte order; BC should contain the port number in the normal Z80 little endian format.

Return value

On success, the C connect() call returns 0, and on error, returns -1.

The assembly language call returns with the accumulator set to zero on success. On error, the carry flag is set and the accumulator is set to the error number.