6.5 Simple sockets

A simple socket library is available for qBeta. Consider the following example:

The Client object connects to Localhost on port 3000, sends a message and awaits an answer.

Client: obj
   theServer: obj LIB.Socket.Socket                    -- Declaration of a Socket object
   "Client:\n".print
   theServer.init(3000)                               -- Open Socket on port 3000
   theServer.connect("localhost")                   -- Connect to localhost
   theServer.send("Hello world from client")        -- Send a message to the Server
   ("Client received: " + theServer.receive).print  -- Await answer from Server
   theServer.close                                  -- Close the Socket

The Server object has the following description:

Server: obj
   thisServer: obj LIB.Socket.Socket                      -- Declaration of a Socket object 
   aClient: ?Lib.Socket.Socket                          -- Socket variable for incoming connect
   "Server:\n".print
   thisServer.init(3000)                                -- Open Socket on port 3000
   thisServer.bind                                      -- Bind the Socket
   thisServer.listen                                    -- Listen for a connection
   aClient:= thisServer.accept                          -- Accept a connection
   ("Server:received: " + aClient.receive + "\n").print -- Receive a message from Client
   aClient.send("Got your message:-)");
   "Type a char to terminate server: ".print            -- To prevent the Server from
   put(_iget)                                           -- terminating too quickly

Hello world