Solo Predictor Example Connection Code

From Eigenvector Research Documentation Wiki
Revision as of 14:08, 15 June 2009 by imported>Jeremy
Jump to navigation Jump to search

The following provides some standard code pieces which can be used to make connections to Solo_Predictor. These examples may not be appropriate for all applications (e.g. none of these examples are asynchronous calls. They all 'block' the program execution until Solo_Predictor returns a result.) In addition, additional error checking and input/output parsing is required in most cases. Lots of other code examples can be found on the web by searching for sockets and the language of interest.

C# - Socket Connection

The following C-sharp example expects three variables: ServerIP as a string indicating the server's IP address, Port as an integer giving the server's port number, and command as a string command to send. It returns Solo_Predictor's output in a variable called outputString. This code requires the following "using" declarations:

using System;
using System.Net.Sockets;
using System.ComponentModel;

Note that no object or function declarations are given in this code but would generally be required. This function also sets the ReceiveTimeout property on the socket to 2000 milliseconds. This setting can be adjusted as required by the application. The total time of most calls to Solo_Predict is 1/2 second or less (testing on a moderately featured system in 2007 indicated application of a PLS model to a 200 point vector took an average of 0.2 seconds including I/O overhead.)

TcpClient socketForServer;
try
{
  //make connection to server
  socketForServer = new TcpClient(ServerIO,Port);
}
catch
{
  outputString  = "ERROR: Failed to connect to server";
  return;
}
//get stream and stream reader/writer
NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
 
try
{
  //send command to server
  streamWriter.WriteLine(command);
  streamWriter.Flush();
  //wait for and retrieve response
  string outputString;
  socketForServer.ReceiveTimeout = 2000;
  outputString = streamReader.ReadToEnd();
}
catch
{
  outputString = "ERROR: Exception reading from Server";
}
networkStream.Close();  // tidy up

VB – ActiveX

See the EigenvectorTools Visual Basic example.

Matlab – Socket Connection

This code example runs in Matlab and makes use of Java commands to create a socket connection, send a message, and retrieve the response. It assumes that an input message exists in the variable msg as a string command to send and the server ip and port are in the variables srv and port.

import java.io.\*;
import java.net.\*;

%Create socket connection and socket reader and writers
clientSocket = java.net.Socket(srv,port);
iStream_client = clientSocket.getInputStream;
iReader_client = InputStreamReader(iStream_client);
outStream_client = clientSocket.getOutputStream;

%create buffers for socket
clientOut = PrintWriter(outStream_client, true);
clientIn = BufferedReader(iReader_client);

%send message to Solo_Predictor
clientOut.println(java.lang.String(msg));
clientOut.flush;

%wait for reply ready
starttime = now;
while \~clientIn.ready
  if (now-starttime)>60/60/60/24;
    error('No response from server')
  end
end

%read in reply and store in cell array
rcv = {};
while clientIn.ready;
  rcv{end+1} = char(readLine(clientIn));
end

%concatenate string reply with linefeeds
if length(rcv)>1;
  rcv = sprintf('%s\n',rcv{:});
else
  rcv = rcv{1};
end