For this exercise a program is open a socket to localhost on port 256 and send the message "hello socket world" before closing the socket.

Catching any exceptions or errors is not required.




/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.net.
 
runSample(arg)
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
  parse arg host':'port':'message
  if host    = '' then host    = 'localhost'
  if port    = '' then port    = 256
  if message = '' then message = 'hello socket world'
  sendToSocket(host, port, message)
  return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method sendToSocket(host, port, message) public static
  do
    sokt = Socket(host, port)
    soks = sokt.getOutputStream()
    soks.write((String message).getBytes())
    soks.flush()
    sokt.close()
  catch ix = IOException
    ix.printStackTrace()
  end
  return
 