Scripting Guide > Extending JMP > Use Sockets in JSL
发布日期: 09/18/2023

Use Sockets in JSL

Another tool that can be useful in establishing a live data feed is JSL Sockets. You can create two types of sockets using JSL:

Stream

Stream sockets create a reliable connection between JMP and another computer. The other computer might be running JMP, or it might be a vending machine, data collector, printer, or other device that is capable of socket communication. Some devices implement their interface as an HTTP web server.

Datagram

Datagram sockets create a less reliable connection between JMP and another computer. Datagrams are connectionless and information might arrive multiple times, not at all, and out of order. A datagram connection does not include all the overhead of a stream connection that does guarantee reliability. Because datagrams are connectionless, the destination address must be supplied each time (and for the same socket that can be different each time).

Once a socket is created, it can do two things: wait for a connection from another socket, or make a connection to another socket. Here is a simple example program that makes a connection to another computer’s web server to get some data:

tCall = Socket( );
tCall << Connect( "www.jmp.com", "80" );
tCall << Send( Char To Blob( "GET / HTTP/1.0~0d~0a~0d~0a", "ASCII~HEX" ) );
tMessage = tCall << Recv( 1000 );
text = Blob To Char( tMessage[3] );
Show( text );
tCall << Close( );

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 sixth line displays the data in the log.

The last line closes the socket. The web server has already closed the far end, so this socket either needs reconnecting or proper disposal (close).

See the Scripting Index in the Help menu for more examples. The JMP Samples/Scripts folder also contains several examples of scripts using sockets.

需要更多信息?有问题?从 JMP 用户社区得到解答 (community.jmp.com).