Sockets String

The example that follows shows how to create a client connection to the server and send a JSON value from the client to the server.

// Sockets String Client Example (SocketsString_Client.ascript):
//
// This example shows how to use the AeroScript socket functions
// to implement a client program that sends data to be processed by
// a server program.
// The data sent to the server program from this program is a string
// that contains JSON.
// See the Sockets String Server example (SocketsString_Server.ascript) for
// the server program implementation.
//
// Run instructions:
// 1. Before you run the client program, run SocketsString_Server.ascript on Task 1.
// 2. Run SocketsString_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 write timeout (ms) when writing to a socket.
    SocketSetDataWriteTimeout($clientSocket, 1000)

    // Send a JSON value to the server.
    SocketWriteString($clientSocket, "{hello:world,goodbye:world}")

    // Close the socket connection.
    SocketClose($clientSocket)
end

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

// Sockets String Server Example (SocketsString_Server.ascript):
//
// This example shows how to use the AeroScript socket functions
// to implement a server program that receives and processes data from a client program.
// The data sent to this program from the client program is a string
// that contains JSON.
// See the Sockets String Client example (SocketsString_Client.ascript) for
// the client program implementation.
//
// Run instructions:
// 1. Before you run the client program, run SocketsString_Server.ascript on Task 1.
// 2. Run SocketsString_Client.ascript on Task 2.

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

program
	var $serverSocket as handle
	var $clientSocket as handle

	// 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)
		
		// Used to process data that comes from the client.
		var $name as string = ""
		var $value as string = ""
		var $isName as integer = true
		
		// Wait forward 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 a single character.
				var $character as string = SocketReadString($clientSocket, 1)
				
				// Start character of JSON, record name.
				if StringEquals($character, "{")
					$isName = true
					$name = ""
				// End of name, record value.
				elseif StringEquals($character, ":")
					$isName = false
					$value = ""
				// End of value or JSON, process value and record name.
				elseif StringEquals($character, ",") || StringEquals($character, "}")
					// Check to see that the name and value are valid strings.
					if StringIsValid($name) && StringIsValid($value)
						// Process value according to name.
						if StringEquals($name, "hello")
							AppMessageDisplay("Hello " + $value + "!")
						elseif StringEquals($name, "goodbye")
							AppMessageDisplay("Goodbye " + $value + "!")
						end
					end
						
					// Reset variables for the next name/value pair.
					$isName = true
					$name = ""
				// Everything else.
				else
					if $isName
						$name += $character
					else
						$value += $character
					end
				end
			end
		
			Dwell(0.001)
		end
	end

	// Close the server socket.
	SocketClose($serverSocket)

	// Ensure that the socket is closed.
	SocketClose($clientSocket)
end