Hello World via TCP/IP
With just seven lines of code you can send and receive the Hello World message in the same session, between workspaces on the same machine, between machines on the same network or any machine connected to the internet.
These examples are written in APL+WIN to work on the local machine therefore they both refer to the ip address of the local host 127.0.0.1. They can be run as functions or simply typed in the same order as they appear below and executed sequentially in an active session.
To run between machines on a network or over the internet simply replace the ip address with that of the server machine in both functions and choose any port number outside that used by common services.
These functions are the essential bare bones and require more flesh putting on them to create robust communications systems but they serve to demonstrate the possibilities.
Run as functions in the same session this is what you should see:
TcpServer TcpClient Hello World
∇ TcpServer;socket;rc ⍝Create a socket socket←¯1↑⎕ni 'Socket' ⍝Bind the socket on port 3000 and the ip address of the server machine rc←socket ⎕ni 'Bind' 3000 '127.0.0.1' ⍝Put the socket in listening mode rc←socket ⎕ni 'Listen' ⍝Create event handler to accept connection from client and return Hello World rc←socket ⎕ni 'onAcceptNotify' 'rc←3⊃⎕ni "Accept" ◊ 0 0⍴rc ⎕ni "Send" "Hello World" "char"' ∇
∇ TcpClient;socket;hostaddress;service;rc ⍝Create a socket socket←¯1↑⎕ni 'Socket' ⍝Create event handler to read incomming messages rc←socket ⎕ni 'onReadNotify' '3⊃⎕ni "Recv" "char"' ⍝Connect the socket on port 3000 to the ip address of the server machine rc←socket ⎕ni 'Connect' 3000 '127.0.0.1' ∇