Publication date: 08/13/2020

Sockets

skt<<Accept(<callback, timeout>)

Description

Tells the server socket to accept a connection and return a new connected socket.

Returns

A list of up to four items. The first is a string that echoes the command ("accept"). The second is a string, either "ok" or an error. The third is a string that specifies the name of the machine that just connected. The fourth is a reference to the socket that you can send more messages.

Optional Arguments

callback

Specifies the name of a function to receive the data.

timeout

If you use a callback, timeout specifies how long the function should wait for an answer. For a server socket, 0 is an acceptable value because a server should not shut down because no one has connected to it recently.

skt<<bind(localhost, port)

Description

Associates a port on the local machine with the socket.

Returns

A list of two strings. The first string is the command name (“bind”) and the second is “ok” if successful or an error.

Required Arguments

localhost

Specifies the quoted local machine. You cannot bind to another machine.

port

Specifies the port that should be used.

skt<<Close()

Description

Closes a socket.

Returns

A list of two strings. The first string is the command name (“close”) and the second is “ok” if successful.

skt<<Connect(socketname, port)

Description

Connects to a listening socket.

Returns

A list of two strings. The first string is the command name (“connect”) and the second is “ok” for a successful connection or an error sent back by the other socket.

Arguments

socketname

Specifies the name of the other socket. If you are connecting to a web server, this is the web address (the name is preferred to the IP address).

port

Specifies the port of the other socket to connect through.

skt<<GetPeerName()

Description

Retrieves the address and port of the socket at the other end of the connection.

Returns

A list of four strings. The first echoes the command ("getpeername"). The second is either “ok” or an error. The third and fourth are the address and the port.

skt<<Get Sock Name()

Description

Retrieves the address and port of the socket at this end of the connection.

Returns

A list of four strings. The first echoes the command ("getsockname"). The second is either “ok” or an error. The third and fourth are the address and the port.

skt<<ioctl(FIONBIO, Boolean)

Description

Controls the socket’s blocking behavior.

Returns

A list of two strings. The first string is the command name (“ioctl”) and the second is “ok” if successful or an error.

Arguments

FIONBIO, 1

FIONBIO means Non-Blocking I/O. If true, turns on the behavior and the argument.

skt<<Listen()

Description

Tells the server socket to listen for connections.

Returns

A list of two strings. The first echoes the command ("listen") and the second is "ok" or an error message.

skt<<recv(n, <callback, timeout>)

skt<<recvfrom(n, <callback, timeout>)

Description

Receives either a stream message (recv) or a datagram message (recvfrom) from the other socket. If the two optional arguments are used, the data is not received immediately. Instead, the data is received when the function callback is called.

Returns

A list of three strings. The first string is the command name (“recv” or “recvto”). The second is “ok” if successful or an error message if not. The third string is the data that was received. If a callback function is used, a fourth element is the socket that was used in the original recv or recvfrom message.

Required Argument

n

Specifies the number of bytes to receive from the other socket.

Optional Arguments

callback

Specifies the name of a function to receive the data.

timeout

If you use a callback, timeout specifies how long the function should wait for an answer.

skt<<Send(stream)

skt<<SendTo(dgram)

Description

Sends the data in the argument to the other socket. Send sends a stream and sendto sends a datagram.

Returns

A list of three strings. The first string is the command name (“send” or “sendto”). The second is “ok” if successful or an error message if not. The third string is any portion of the stream that could not be sent, or empty if all the data was sent correctly.

Arguments

stream

Specifies the command to send to the other socket.

dgram

Specifies the command to send to the other socket.

Note

Either argument might need to contain binary data. JMP represents non-printable ASCII characters with a tilde (~) followed by the hexadecimal number. For example,

skt<<send(("GET / HTTP/1.0~0d~0a~0d~0a");

sends a “get request” to an HTTP server.

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