Go to the first, previous, next, last section, table of contents.


]listenOnSocket

Control-struct: ]listenOnSocket
file: job.t
package: muf
status: tentative
{  [ :socket socket
     :port 4201
     :protocol :stream            ( optional )
     :addressFamily :internet    ( optional )
     :interfaces :any             ( optional )
   | ]listenOnSocket
}

The ]listenOnSocket function sets the Muq server to listen for network connections on the given unix port number.

The :socket parameter must be a socket with a socket$S.standardOutput set to a messageStream. A typical choice of messageStream is @$s.standardInput (the input message stream for the job issuing the ]listenOnSocket request).

If :protocol is :stream (or omitted), each connection accepted will result in the creation of a Socket bound to the connection, which will be placed in the messageStream socket$S.standardOutput. The presumption is that some job is reading this message stream and forking off a job for each connection accepted.

If :protocol is :datagram, socket$S.type will be set to :udp and the socket will return datagrams read from that port.

The :addressFamily parameter must currently always be :internet if provided.

The :interfaces parameter must currently always be :any if provided.

The :port parameter must be an integer specifying the unix port on which to listen. See section Choosing a port number.

Here is a complete short example of listening on a socket. Normally the work done in the example would involve three jobs, one for the listening socket, one for the server (forked by the listening job) and one for the client, probably on another machine. Here we do everything inline in one job to keep things simple; See the nanomud for a more realistic example.

( Create a socket listening      )
( for connections on port 32123: )
makeSocket --> *listen*
makeMessageStream --> *listen-output*
*listen-output* --> *listen*$S.standardOutput
[ :socket *listen*
  :port 32123
  :protocol :stream
| ]listenOnSocket

( Open a client connection: )
makeSocket --> *client*
makeMessageStream --> *client-input*
makeMessageStream --> *client-output*
*client-input* --> *client*$S.standardInput
*client-output* --> *client*$S.standardOutput
[ :socket *client*
  :port 32123
  :protocol :stream
| ]openSocket

( Accept the connection, creating server: )
*listen-output* readStreamLine --> *server* --> *opcode*
( *server* is the new socket; *opcode* is "new" )
makeMessageStream --> *server-input*
makeMessageStream --> *server-output*
*server-input* --> *server*$S.standardInput
*server-output* --> *server*$S.standardOutput

( Send a line from client to server: )
"This is a test\n" *client-input* writeStream

( Read line at server end: )
*server-output* readStreamLine --> *who* --> *line*
( *who* == *server*  and *line* is our line of text )

( Send a line from server to client: )
"This is not a test\n" *server-input* writeStream

( Read line at client end: )
*client-output* readStreamLine --> *who* --> *line*
( *who* == *client* and *line* is our line of text )

( Close all ports: )
[ :socket *client* | ]closeSocket
[ :socket *server* | ]closeSocket
[ :socket *listen* | ]closeSocket


Go to the first, previous, next, last section, table of contents.