Articles Archive
Articles Search
Director Wiki
 

Fun with TCP/IP Sockets

August 23, 2005
by Steve Hagenlock

Sockets in Director

The relationship between Director and TCP/IP has extended far back to the early days, when an XCMD from the Hypercard world allowed for the creation of some simple sockets. When Macromedia released the Multiuser XTRA and Server combination, many doors were opened including the ability for the Multiuser XTRA to open connecting, text-based sockets with several types of socket servers, provided that they didn’t implement binary protocols. We’ve often used the Multiuser XTRA in the quest to allow Director to converse with the outside world, but clearly there are limitations to what can be done with the Macromedia networking solutions including the fact that the Multiuser XTRA has been put to rest as far as development is concerned and neither it, nor the Flash XML Socket can create listening socket servers for general use. (The MU XTRA can create a listening socket as long as the Multiuser protocol is used.) Fortunately, there are options available if you need to do more than the Multiuser XTRA or a Flash XML Socket can accomplish.

Third-party solutions

In the solution that we’ll be discussing, we put to use two products which work in combination to extend and enable Director to work with a library for creating and managing Winsock sockets.

The WinsockQ ActiveX Component for Sockets, is provided by Datawizard (http://www.datawizard.net). This product is primarily aimed at consumers of ActiveX and COM automation technology, such as Visual Basic developers at a cost of $199.00. It is necessary that each computer which will use an ActiveX control have that control registered with the Windows OS. Usually, the act of registering an ActiveX control with the OS is the job of your installation program that you create for your product, or in the case of developer installations, that is handled for you during the install. This is surely not the only solution available for manipulating sockets using ActiveX, however I’ve found it to work well for my needs.

To interface with this socket library, we employ the VBScript XTRA from Xtramania (http://www.xtramania.com). The VBScript XTRA licensing has an option for use with a single control and costs $75.00 in this configuration. The VBScript XTRA allows for the implementation of a diverse set of products from the world of ActiveX/COM automation. Many different solutions can be assembled with the help of this product. Some of the uses of the networking technique that we are discussing require the ability to deal with data in a binary form. The VBScript XTRA provides this functionality in the form of a Binary Wrapper, which handles translation and storage of Lingo variables into a Binary Wrapper instance.

Anatomy of a simple echo server

A simple, “Hello World” style program can be created for testing this technique. Let’s dissect a Parent Script which creates a simple echo server. An echo server will echo back anything that it receives. Thus, if you create a telnet session with a socket with an echo server running behind it, you’ll see what you type being echoed back to your telnet session’s output.

The socket creation process works like this:

At this point, we have a functioning listening socket server up and waiting for connections on port 1700. The working Lingo code for this Parent Script is below:

property pSocketLib -- <object> VBScript Object
property pSocket -- <object> Socket Instance
property pSocketConnections -- <Prop List> for storing active sockets
property pPort -- <Integer> Port number
property pProtocol -- <Symbol> TCP or UDP Socket type
property vb -- <object> VBScript XTRA base instance

on new me
  pPort = 1700
  pProtocol = #tcp
  
  --Number of Backlogged connection requests which can sit in Queue.
  lBacklog = 10
  
  --Default Ethernet adapter, assumes a single adapter system
  lAdapter = - 1
  --Create the Socket Manager
  me .mInit(pProtocol,lAdapter,lBacklog, pPort)
  
  --Assign this parent script as the event handler for callbacks.
  pSocketLib.EventsHandler = me
  
  --[integer sock_handle:[integer sock_handle, boolean accept]]
  pSocketConnections = [:]
  return me
end


--
--Socket creation
on mInit me, lProtocol, lAdapter, lBacklog, pPort
  vb = xtra ( "vbScriptXtra")
  pSocketLib = vb.createObject( "WinsockQ.cWinsockQ")
  pSocketLib.Activate( TRUE)
  pSocket = pSocketLib.AddListener(pPort, lProtocol, lAdapter, lBacklog)
end

--
-- Shutdown method
on mDestroy me
  pSocketLib.CloseSock(pSocket)
  pSocketLib.Activate( FALSE)
  pSocketLib = 0
end

----------Methods, these are wrapper methods for the ActiveX component
-- Send data to all connected sockets, each socket instance is stored
-- in the pSocketConnections property list
on SendData me, lData
  repeat with sock in pSocketConnections
    so = sock.SocketHandle
    pSocketLib.SendData(so, lData)
  end repeat
end

----------Events, these are all callbacks from the ActiveX Component
-- Called upon a request for connection
on ConnectionRequest me, args
  --args : [#SocketHandle:Integer socket handle,
  -- #Accept: Boolean accept]
  -- NOTE: Change the #Accept value to FALSE to refuse the connection.
  lSock = args
  pSocketConnections. addProp(args.SocketHandle,args)
  return args
end

-- Called when a socket is closed.
on ConnectionTerminated me, args
  pSocketConnections. deleteProp(args.SocketHandle)
end

-- Called when data has arrived, process it according to the rules of
-- an echo server, which is send that data back to the client.
on DataArrival me, args
  --args : [#SocketHandle:Integer socket handle,
  --#data:#obj-VBScriptXtra Instance Binary Data,
  --#retrieved:Integer - Retrieved char count]
  --Echo data back to all connections
  me .SendData(args.data. String)
end

This Parent Script implements the following:

To see the results of this script working, you could instantiate the parent script then open a DOS command line and use Telnet to connect to the socket

  C:\Telnet 127.0.0.1 1700

If you type into the Telnet window, you will see what you type, this occurs because the socket object is echoing what you typed back to you.

Real-world Solutions

The echo server we created is neat, but not very useful, let’s take a look at some things that you can do using this technique.

Conclusion

For certain types of applications, industrial strength, standards-based communication tools are helpful, and often necessary. Vanilla Director projects have not had the ability to play the role of a tcp/ip socket server without caveats and limitations. Using techniques such as these allows for a wider range of functionality available to developers of Windows projects by taking advantage of the multitude of software available in the ActiveX/COM market and harnessing their power through the application of the VBScript XTRA.

Steve Hagenlock, a denizen of Lawrence, Kansas , is the CTO of Popstar Networks and senior software architect for Opera Glass Networks, where he develops Kiosk, Digital Signage and PocketPC solutions for Retail, Public Space and Live Events. He has been abusing Director since 3.13 was shiny and new.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.