Module ce_socket

Description

Socket library.

This library contains functions which create TCP/IP socket servers and clients.

Function Index

Exported Functions
asynch_accept/2Used by server_loop/8 to handle the listen socket.
client/4Waits for a server to become available, connects to it, and returns a socket.
client/6Implements a generic socket client.
couple/1See couple/2.
couple/2Couples a socket to a process.
coupler/2Used by couple/2 to achieve socket/process coupling.
server/5See server/6.
server/6Implements a generic TCP/IP socket server.
server_setup/7Used by server/6 to handle the listen socket.
Internal Documented Functions
server_loop/8Called by server_setup/7 to handle the listen socket.

Exported Functions

asynch_accept/2

asynch_accept(Parent::pid(), LSock::listen_socket()) -> never_returns

Used by server_loop/8 to handle the listen socket. This function waits for a connection using gen_tcp:accept/1 and notifies the parent process which spawned it when a connection happens, allowing the parent to process other messages in the meantime. This function should not be called directly by users of this module.

client/4

client(address(), port(), options(), Interval::interval()) -> {ok, socket()} | {error, Reason}

Waits for a server to become available, connects to it, and returns a socket. This function tries to contact the server repeatedly, every Interval milliseconds.

client/6

client(module(), function(), args(), address(), port(), options()) -> pid() | {error, Reason}

Implements a generic socket client. Connects to a socket with the given options on the given port and spawns Module:Function(Socket, Args...) to handle the connection.

couple/1

couple(socket()) -> pid()

Equivalent to couple(socket(), self()).

couple/2

couple(socket(), pid()) -> pid()

Couples a socket to a process. Messages can be sent to the process and will be picked up on the other end of the socket (which is also presumably coupled similarly.) Likewise, messages sent from the other end of the connection will be delivered to the owner. In essence this allows two Erlang sessions to communicate with each other in the same manner as communicating with any other process (that is, by sending Erlang messages,) without the nodes necessarily even being in the same distribution, or indeed any distribution at all. The socket should be opened in binary mode.

coupler/2

coupler(socket(), pid()) -> ok

Used by couple/2 to achieve socket/process coupling. Translates tcp messages to Erlang terms and vice versa. This function should not be called directly by users of this module.

server/5

server(Mod::atom(), Func::atom(), Args::[term()], Port::integer(), Opts::[option()]) -> pid() | {error, reason()}

Equivalent to server(Mod, Func, Args, Port, Opts, 1000000000).

server/6

server(Mod::atom(), Func::atom(), Args::[term()], Port::integer(), Opts::[option()], MaxCon::integer()) -> pid() | {error, reason()}

Implements a generic TCP/IP socket server. Opens a socket with the given options on the given port and spawns Module:Function(Socket, Args...) to handle each incoming connection, up to the specified maximum number of connections.

server_setup/7

server_setup(Mod::atom(), Func::atom(), Args::[term()], LSock::listen_socket(), Port::integer(), Opts::[option()], MaxCon::integer()) -> never_returns

Used by server/6 to handle the listen socket. This function should not be called directly by users of this module.

Documented Internal Functions

server_loop/8

server_loop(Mod::atom(), Func::atom(), Args::[term()], LSock::listen_socket(), Port::integer(), Opts::[option()], MaxCon::integer(), Accepter::pid()) -> never_returns

Called by server_setup/7 to handle the listen socket. This function waits around for something to happen: either for a message to arrive from the previous asynch_accept/2 (meaning there is now one more connection), or for the exit signal of an existing socket process (meaning there is now one less connection.) In the former case, a new asynchronous accept call is set up by looping back to server_setup/7.