Coder Social home page Coder Social logo

ldpl-socket's Introduction


The LDPL Socket Library allows you to open, close, write to, and read from network sockets in LDPL.

It's mostly made for clients - if you want to write a server, please see the library that this one is heavily based on: Lartu's https://www.github.com/lartu/ldpl-net-server

Requires LDPL 4.3 or greater.

๐Ÿงฐ Installation

You can install this library by hand or using LPM.

๐Ÿ“ฆ Installing using LPM

Open a terminal and write lpm install ldpl_socket. Once downloaded, include it in your LDPL project by adding the line:

using package ldpl_socket

before the data and procedure sections of your source file. The library is ready to be used.

โœ‹๐Ÿป Installing by hand

Include the library into your LDPL project by copying the folder ldpl_socket to your project directory and then adding the line:

include "ldpl_socket/ldpl_socket.ldpl"

before the data and procedure sections of your source file. The library is ready to be used.

๐Ÿ“š Documentation

This library adds a few new statements to the language:

  • SOCKET CONNECT TO <hostname> AT <port> IN <number variable>
    • Use this statement to open a new socket connection. Currently only TCP connections are supported. If the connection is successfully opened, your <number var> will be set to a "socket number" that's >= 0. Otherwise, ERRORCODE will be set to a negative number.
  • SOCKET CLOSE <socket number>
    • Once a socket has been opened, use this statement to close it.
  • SOCKET SEND <text> TO <socket number>
    • Use this to send messages to an open connection. ERRORCODE will be set to a negative number if the send call failed.
  • SOCKET SEND <text> TO <socket number> BYTES IN <number var>
    • Same as SOCKET SEND but stores the number of bytes sent in <number var>.
  • SOCKET READ FROM <socket number> IN <text variable>
    • This statement should be used to check for messages on a socket connection opened with SOCKET CONNECT TO <hostname> PORT <port>. <text variable> will be set to anything received from the socket, which may not be an entire "message" in whatever protocol you're using. ERRORCODE will be set to 1 if the connection is closed. Note: By default this is a blocking call. Use the next statements to change that.
  • SOCKET READ FROM <socket number> IN <text variable> BYTES IN <number var>
    • Same as SOCKET READ but stores the number of bytes received in <number var>.
  • SOCKET SET BLOCKING <socket number>
  • SOCKET SET NONBLOCKING <socket number>
    • By default all socket reads are blocking, but if you want to do a nonblocking call (or ensure you're using a blocking one) then these statements are for you. Basically they set/unset O_NONBLOCK on the underlying socket. In LDPL, ERRORCODE will be set to < 0 if there is an error or (most common in nonblocking mode) no data was available to be read. So, check for that.

๐Ÿ‘ทโ€โ™‚๏ธ Example

There's an example echo client in examples/echo-client.ldpl. Use it with Lartu's demo echo server by cloning that project and running the template:

git clone https://github.com/lartu/ldpl-net-server
cd ldpl-net-server
ldpl net_template.ldpl
./net_template-bin

Then, back in ldpl-socket land, build and run the client:

make echo
./echo-client 

You should now be in a little echo repl:

~ Connected to localhost:8888 (socket:3)
< Hello there!
> Well, I never!
< Well, I never!
> ...or have I?
< ...or have I?

๐Ÿ“œ License

This library is released under the MIT License.

ldpl-socket's People

Contributors

lartu avatar xvxx avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

lartu

ldpl-socket's Issues

URL support

Hi there! Does this company support connecting URLs instead of plain IPs? I had some problems with this when I wrote the LDPL IRC Bot library, and solved it using this.

If your library doesn't support URLs, please consider using this add-on. I will try to upload it as a library asap, but you can get it from that link atm.

onDisconnect

Having a way to check if a socket has disconnected would be very nice. Maybe something like an onDisconnect subprocedure or a socket is connected $ method.

What do you think?

Non-blocking sockets

If I'm not wrong, socket read from $ in $ is blocking. This is not good for apps that depend on constant data reading from the socket, like an IRC client. I think that a socket read from $ in $ without blocking or something like that statement should be added to the library (returning "" when there is no data to be read).

What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.