The first line creates a socket and gives it a reference name (tCall). By default, a stream socket is created. You can designate the type of socket to create with an optional argument: socket(STREAM) or socket(DGRAM).
The second line connects the tCall socket to port 80 (which is generally the HTTP port) of the JMP website.
The third line sends a GET request to the JMP web server; this message tells the JMP web server to send the JMP home page back. The / that follows the word GET should be the path to the page to be opened. A / opens the root page.
The fourth line receives up to 1000 bytes from the JMP web server and stores a list of information in tMessage. Each socket call returns a list. The first element of the list is the name of the call, and the second is a text message, which might be ok or a longer diagnostic message. Additional elements, if present, are specific to the call. In this case, the third element in the list is the data received.
The fifth line converts the binary information received into a character string. tMessage[3] is the third item in the list returned by Recv; it is the data from the JMP web server.
The JMP Samples/Scripts folder contains several examples of scripts using sockets.
Before creating and using a socket, you might need to retrieve information about the end that you want to connect to. GetAddrInfo( ) and GetNameInfo( ) each takes an address argument and an optional port argument and returns a list of information. For example:
Once you have created a socket with socket( ), there are many messages that you can send to it.
Connects to a listening socket. Returns a list: {"connect", "ok"} if the connection was successful; or an error if not (for example, {"connect", "CONNREFUSED: The attempt to connect was forcefully rejected. "}).
receives a STREAM message. The data comes back in a list, along with some other information. Recv takes a required numeric argument that specifies the number of bytes to accept.
controls the socket’s blocking behavior. By default, JMP sockets block if no data is available; the socket does not return control to the JSL program until data is available. This makes scripts easy to write, but not particularly robust if the remote end of the connection fails to supply the data. A socket that is set for non-blocking behavior always returns immediately, either with an ok return code and some data, or with a “WOULDBLOCK: ...” return code, which means if it were a blocking socket, it would have to wait (block progress of the next JSL statement) until data became available.
Important: Background operations that use a JSL callback avoid this issue; a socket used in a background recv, recvfrom, or accept is set to non-blocking and is polled during wait statements and when JMP is otherwise idle.
Ioctl returns a list. For example, {"ioctl", "ok"}, or {"ioctl", "NOTCONN: The socket is not connected. "} if the socket has not been bound (see bind, below) or connected already.
tells the server socket what address the client socket listens on. Bind associates a port on the local machine with the socket. This is required before a socket can Listen. (See below). Bind is not usually used on sockets that connect; the operating system selects an unused port for you. Bind is needed for a server because anyone that wants to connect to the server needs to know what port is being used. A common port is 80, the HTTP port. Bind returns a list. For example, {"bind", "ok"}, or {"bind", "ADDRNOTAVAIL: The specified address is not available from the local machine. "} if you try binding to a name that is not on your machine. Another socket can connect to this socket if it knows your machine name and the number.
tells the server socket to listen for connections. A listening socket is listening for connections from other sockets. You need only to put the socket into listen mode once. Accept (see below) is used to accept a connection from another socket. Listen returns a list. For example, {"listen", "ok"}, or {"listen", "INVAL: The socket is (or is not, depending on context) already bound to an address. or, Listen was not invoked prior to accept. or, Invalid host address. or, The socket has not been bound with Bind. "} if your bind call did not succeed.
tells the server socket to accept a connection and return a new connected socket. Accept returns a list stating what happened, and if successful, a new socket that is connected to the socket at the other end. For example, the returned list might be {"Accept", "ok", "localhost", socket( )}. In this case localhost is the name of the machine that just connected, and the fourth argument is a socket that you can use to send or recv a message.
asks about the other end of a connection. GetPeerName returns a list with information about the other end’s socket: {"getpeername", "ok", "", "4087"}. If this is a server socket, you can discover the address and port of the client that connected. If this is a client socket, you re-discover the name and port of the server that you used in the connect request.
asks about this end of a connection. GetSockName returns a list with information about this socket: {"getsockname", "ok", "localhost", "httpd"}. If this is a client socket, you can discover the port the operating system assigned. If this is a server socket, you already know this information from a bind you already made.