Sockets Array

The example that follows shows how to create a client connection to the server and process array values from the server.

// Sockets Array Client Example (SocketsArray_Client.ascript):
//
// This example shows how to use the AeroScript socket functions
// to implement a client program that sends data to a server program and receives data from a server program.
// The data sent by this program asks the server program to return a
// subset of the values from an array in the server program.
// See the Sockets Array Server example (SocketsArray_Server.ascript) for
// the server program implementation.
//
// Run instructions:
// 1. Before you run the client program, run SocketsArray_Server.ascript on Task 1.
// 2. Run SocketsArray_Client.ascript on Task 2.

// The port on which the server listens for incoming client connections.
#define SERVER_PORT 8080

program
	// Create a client connection to the server.
	var $clientSocket as handle
	$clientSocket = SocketTcpClientCreate("localhost", SERVER_PORT, 10000)
	
	// Set a read timeout (ms) when reading from a socket.
	SocketSetDataReadTimeout($clientSocket, 1000)
	
	// Set a write timeout (ms) when writing to a socket.
	SocketSetDataWriteTimeout($clientSocket, 1000)
	
	// Send a request to the server specifying the array values to return from the server.
	var $startIndex as integer = 10
	var $endIndex as integer = 15
	SocketWriteString($clientSocket, "startIndex:" + IntegerToString($startIndex) + ",endIndex:" + IntegerToString($endIndex) + ",")
	
	// Read the response from the server.
	var $responseCode as integer = 0
	var $valuesReceived as integer = 0
	while true
		// Process response.
		if SocketGetReadBytesAvailable($clientSocket)
			// No response yet, wait for response code.
			while $responseCode == 0
				$responseCode = SocketReadInt32($clientSocket)
			end
		
			// (HTTP) 200 OK.
			if $responseCode == 200
				var $element as integer = SocketReadInt32($clientSocket)
				AppMessageDisplay("OK >> " + IntegerToString($element))
				
				// Increment values received.
				$valuesReceived++
				
				// Received the expected number of values. Break out of this loop.
				if $valuesReceived == ($endIndex-$startIndex+1)
					break
				end
			// (HTTP) 400 Bad Request. Display error then break out of this loop.
			elseif $responseCode == 400
				AppMessageDisplay("BAD REQUEST >>  " + SocketReadString($clientSocket))
				break
			end
		end
	end
	
	// Close the socket connection.
	SocketClose($clientSocket)
end

The example that follows shows how to accept a pending client connection to the server and process array values from the client.

// Sockets Array Server Example (SocketsArray_Server.ascript):
//
// This example shows how to use the AeroScript socket functions
// to implement a server program that receives data from a client
// program and sends data to a client program.
// The data sent by the client program asks this program to return a
// subset of the values from an array in this program.
// See the Sockets Array Client example (SocketsArray_Client.ascript) for
// the client program implementation.
//
// Run instructions:
// 1. Before you run the client program, run SocketsArray_Server.ascript on Task 1.
// 2. Run SocketsArray_Client.ascript on Task 2.

// The port on which the server listens for incoming client connections.
#define SERVER_PORT 8080
#define ARRAY_SIZE 100

program
	var $serverSocket as handle
	var $clientSocket as handle
	var $database[ARRAY_SIZE] as integer
	
	// Initialize the array with values from 0-99.
	var $i as integer
	for $i = 0 to ARRAY_SIZE-1
		$database[$i] = $i
	end
	
	// Open the port for listening.
	$serverSocket = SocketTcpServerCreate(SERVER_PORT)
	
	// Loop forever to keep connecting to other clients if a client connection closes.
	while true
		// Accept a client connection to read from.
		$clientSocket = SocketTcpServerAccept($serverSocket)
		
		// Set a read timeout (ms) when reading from a socket.
		SocketSetDataReadTimeout($clientSocket, 1000)
		
		// Set a write timeout (ms) when writing to a socket.
		SocketSetDataWriteTimeout($clientSocket, 1000)
		
		// State machine to make sure that the server has received $startIndex and $endIndex.
		var $processRequest as integer = 0
		var $startIndex as integer = 0
		var $endIndex as integer = 0
	
		// Wait forever for any data to be received until the connection closes.
		while true
			// Check if the connection closed. If so, break out of this loop.
			if !SocketTcpClientIsConnected($clientSocket)
				// Ensure that the socket is closed and clean up the socket.
				SocketClose($clientSocket)
				break
			end
			
			// Check to see if there are any bytes to read.
			while SocketGetReadBytesAvailable($clientSocket) > 0
				// Read the request sent from the client.
				var $request as string = SocketReadString($clientSocket, ",")
				
				// Parse the client's request.
				var $subStrings[2] as string
				StringSplit($request, [":"], $subStrings, 2)
				var $label as string = $subStrings[0]
				var $value as string = $subStrings[1]
				
				// Check what the request label is.
				if StringEquals($label, "startIndex") && ($processRequest == 0)
					$startIndex = StringToInteger($value)
					
					// $startIndex is out-of-range.
					if ($startIndex < 0) || ($startIndex >= ARRAY_SIZE)
						// Respond with (HTTP) 400 Bad Request.
						SocketWriteInt32($clientSocket, 400)
						SocketWriteString($clientSocket, "$startIndex is out-of-range!")
						SocketWriteUInt8($clientSocket, 0)
					// $startIndex is within-range.
					else
						$processRequest = 1
					end
				elseif StringEquals($label, "endIndex") && ($processRequest == 1)
					$endIndex = StringToInteger($value)
					
					// $endIndex is less than $startIndex.
					if $endIndex < $startIndex
						// Respond with (HTTP) 400 Bad Request.
						SocketWriteInt32($clientSocket, 400)
						SocketWriteString($clientSocket, "$endIndex (" + IntegerToString($endIndex) + ") is less than $startIndex (" + IntegerToString($startIndex) + ")!")
						SocketWriteUInt8($clientSocket, 0)
					// $endIndex is out-of-range.
					elseif ($endIndex < 0) || ($endIndex >= ARRAY_SIZE)
						// Respond with (HTTP) 400 Bad Request.
						SocketWriteInt32($clientSocket, 400)
						SocketWriteString($clientSocket, "$endIndex is out-of-range!")
						SocketWriteUInt8($clientSocket, 0)
					// $endIndex is within-range.
					else						
						$processRequest = 2
					end
				end
				
				// Send the requested array to the client as elements.
				if $processRequest == 2
					// Respond with (HTTP) 200 OK.
					SocketWriteInt32($clientSocket, 200)
					
					// Send the array values to the client.
					for $i = $startIndex to $endIndex
						SocketWriteInt32($clientSocket, $database[$i])
					end
					
					// Reset the state machine.
					$processRequest = 0
				end
			end
		end
	end
	
	// Close the server socket.
	SocketClose($serverSocket)
	
	// Ensure that the socket is closed.
	SocketClose($clientSocket)
end