The following example is a simple client script that connects to a server socket at IP address 192.8.100.10 and port number 20248. The client then waits for the server to send the string Hello world!, after which the client sends the string Hi! back to the server, and finally hangs up.
/* Initialization. */
OPTIONS "C_CALL WINFUNC"
LIBRARY rexxsock
/* Tell RexxSock that we want it to raise USER 10 condition for any
* error (besides SYNTAX errors of course). We also want SockErrNo
* set (instead of ErrNo).
* NOTE: The RexxSock functions may also raise SYNTAX if you pass bad arguments
* or the DLL runs out of memory to interface with REXX. So we should CATCH
* SYNTAX and HALT too if we don't want our script to ever abort from errors.
*/
SockInit(10, 1)
/* Set the port and IP address of the server to which we'll connect. We
* assume that the server has an IP address of 192.8.100.10 and a socket
* using port number 20248. We must store this information in a stem
* variable, and then we will pass the name of this stem variable to
* SockConnect. SockConnect will retrieve this information and connect
* to that server. We can use any stem variable name we wish. Here we
* use "server.!", but we must set two tails, named port and addr.
*/
server.!port = 20248
server.!addr = '192.8.100.10'
/* Connect to the server and return the new socket in our variable
* named "socket". You can use any name you want for that variable.
* You pass the desired name as the first argument to SockConnect.
* Note also that we pass (as the second argument) the name of
* the stem variable we set above. Finally, we pass the string
* 'IPPROTO_TCP' as the third arg to indicate we want TCP/IP protocol.
*/
SockConnect('socket', 'server.!', 'IPPROTO_TCP')
SAY 'Connected to server at address' server.!addr
/* Now we expect the server to send us "Hello World!" so
* let's go get that message.
*/
/* Wait for some data to be received from the server. We omit the
* timeout arg, so we wait forever. If we pass the timeout arg,
* SockSelect will raise the USER condition for a time out. We
* need to first fill in a stem variable where the tail 0 tells how
* many sockets to wait for (ie, 1 here), and then the remaining
* tails are those sockets. We must pass that stem name as the
* first arg to SockSelect. Here, we use a stem variable named
* "reads.", but you could use any name you like. Note that we
* store the socket SockConnect gave us.
*/
reads.0 = 1
reads.1 = socket
timedout = SockSelect('reads.')
/* Read upto the next 512 bytes of data from the server. SockRecv
* returns the number of bytes actually read, and stuffs those bytes
* into some variable of our choice. Here, we use a variable named
* "chunk", and pass that name to SockRecv. SockRecv will raise
* USER condition if the socket has been shutdown/closed by the server,
* or if some other error occurs.
*/
bytes = SockRecv(socket, 'chunk', 512)
/* Display what we received. */
SAY chunk
/* Send "Hi!" back to the server. */
SockSend(socket, 'Hi!')
/* Close the socket. RexxSock will tell the server we're closing
* the connection.
*/
SockClose(socket, 1)
SAY 'Socket closed'
RETURN
/* ==========================================================
* We told RexxSock to raise the USER 10 condition if there is
* ever any error (other than a SYNTAX error). We CATCH USER
* 10 here, so REXX will jump here if it is raised. So, if
* we're here, then some RexxSock function has had an error.
*
* CONDITION('D') reports the name of the RexxSock function that
* had the error. SIGL is the line number in our script where
* we had the error. The variable named "SockErrNo" is set to an
* RexxSock Error name such as "EINPROGRESS" or "ENETDOWN" etc.
* We can pass this variable to the RexxSock function SockErrMsg
* to get an appropriate error message or call SockPSock_Errno
* to display a message box.
*/
CATCH USER 10
SockPSock_Errno()
RETURN
CATCH SYNTAX
CONDITION('M')
RETURN