Publication date: 07/08/2024

Messages for Sockets

Once you have created a socket with Socket( ), there are many messages that you can send to it.

connect

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. "}).

close

Closes the connection when you are finished with it. Returns a list (for example, {"Close", "ok"}).

send

Sends a STREAM message to the other end of the socket.

sendto

Sends a DGRAM message to the other end of the socket.

recv

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.

recvfrom

Receives a DGRAM message.

ioctl

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.

bind

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.

listen

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.

accept

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.

getpeername

Asks about the other end of a connection. GetPeerName returns a list with information about the other end’s socket: {"getpeername", "ok", "127.0.0.1", "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.

getsockname

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.

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).