%accept() Function

The %accept() function extracts the first connection on the queue of pending connections, creates a new socket, and allocates a new file descriptor.

Syntax

code = %accept(fd, &address, &port)

Parameter(s)

fd

File descriptor of the local socket returned by a previous call to the FlashBASIC C function %socket().

address

Originating address of the incoming call.

For IPv4 implementations: The address argument must be an integer.

For IPv6 implementations: The address argument must be a char[xx] where xx specifies a buffer size of at least 46.

port

Originating port number or the incoming call.

Description

To compile successfully, the statement cfunction socket.builtin must be included in the source code.

Upon successful completion, the new file descriptor is returned in code and the address and port FlashBASIC variables are updated.

In the case of an error, a value of -1 is returned and the FlashBASIC system(0) function is set to the value of errno.

A legal integer value must be assigned to the port variable and a legal integer value (IPv4) or char array (IPv6)must be assigned to the address variable before the call.

Example(s)

IPv4 Example:

cfunction socket.builtin

include dm,bp,includes sysid.inc

include dm,bp,unix.h socket.h

* Create a socket

fd=%socket(af$inet, sock$stream, 0)

* Bind the socket to a local Ethernet port.

* Use default address.

if %bind(fd, af$inet, inaddr$any, 1024)<0 then

crt ’bind failed’; stop

end

* Wait for incoming connection

%listen(fd, 1)

* Accept a connection

address=0; port=0

rd2=%accept(fd, &address, &port)

crt "Called by address ":address:", port #":port

* Read data from the data link

%recv(fd2, buffer, 1024)

%closesocket(fd2)

%closesocket(fd)

IPv6 Example:

cfunction socket.builtin

include dm,bp,includes sysid.inc

include dm,bp,unix.h socket.h

* Create a socket

fd=%socket(af$inet6, sock$stream, 0)

* Bind the socket to a local Ethernet port.

* Use default address.

if %bind(fd, af$inet6, inaddr$any, 1024)<0 then

   crt "bind failed"; stop

end

* Wait for incoming connection

fd2sten(fd, 1)

* Accept a connection

char address[46]

port=0

fd2=%accept(fd, &address, &port)

crt "Called by address ":address:", port #":port

* Read data from the data link

%recv(fd2, buffer, 1024)

%closesocket(fd2)

%closesocket(fd)

See Also

%bind() Function, %close() Function, %gethostid() Function, %listen() Function, %socket() Function