The LDPL Network Server Library is an library for creating socket based servers in LDPL. It aims to make it very easy to develop, test and deploy network servers, with as little work as possible. This library requires LDPL 4.3 or greater.
You can install this library by hand or using LPM.
Open a terminal and write lpm install ldpl_net_server
. Once downloaded, include it in your LDPL project by adding the line:
using package ldpl_net_server
before the data
and procedure
sections of your source file. The library is ready to be used.
Include the library into your LDPL project by copying the folder ldpl_net_server to your project directory and then adding the line:
include "ldpl_net_server/ldpl_net_server.ldpl"
before the data
and procedure
sections of your source file. The library is
ready to be used.
This library adds five new statements to the language:
NET LISTEN ON <number>
- Use this statement to start your server on port number
<number>
. Do this after you've written all our code and set up the whole library, as this is a locking statement and anything after it won't be executed (because your server will be too busy listening for new data and connections).
- Use this statement to start your server on port number
NET START ON <number>
- Use this statement to start your server on port number
<number>
without blocking the execution of your program. You will need to useNET POLL
to listen for client activity. When your program exits, the socket is closed.
- Use this statement to start your server on port number
NET POLL
- Use this statement to listen for client activity when your server has been started
using
NET START ON <number>
. By default the call is nonblocking and returns instantly if there is no activity, but you can set a timeout or wait forever usingNET POLL FOR <number> SECONDS
orNET POLL BLOCKING
.
- Use this statement to listen for client activity when your server has been started
using
NET SEND <text> TO <socket number>
- Use this statement send the message
<text>
to the client identified by the socket number<socket number>
. The client will receive your message, easy as pie.
- Use this statement send the message
NET KICK CLIENT <socket number>
- Use this statement to disconnect the client identified by the socket number
<socket number>
.
- Use this statement to disconnect the client identified by the socket number
In order to work, the library requires you to declare three sub-procedures:
This sub-procedure will be called when a new client connects to your server. When called,
it will receive three values: a socket_number
identifying your client (you may want to
store this value somewhere), the ip
of the connecting client and its port
.
sub net_new_client
parameters:
socket_number is number
ip is text
port is number
procedure:
# Your code goes here
end sub
This sub-procedure will be called when a client that was connected to our server closes
the connection and disconnects. When called, it will receive three values: a socket_number
identifying the client that left, the ip
of the disconnecting client and its port
.
sub net_client_left
parameters:
socket_number is number
ip is text
port is number
procedure:
# Your code goes here
end sub
This sub-procedure will be called whenever a connected client sends a message to our server.
When called, it will receive two values: a socket_number
identifying the client that sent
the message and the message
itself.
sub net_new_message
parameters:
socket_number is number
message is text
procedure:
# Your code goes here
end sub
A simple template for a minimal echo server can be found in the net_template.ldpl file. Be sure to check it if you have any doubts about where to start writing your server.
This library is released under the MIT License.