Solo Predictor Example Connection Code: Difference between revisions

From EVRI Test Wiki 02192024
Jump to navigation Jump to search
imported>Jeremy
No edit summary
imported>Benjamin
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 4: Line 4:
===C# - Socket Connection===
===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:
Although C# can easily use the [[EigenvectorTools|EigenvectorTools .NET application to connect into Solo_Predictor]], it may be useful to have a socket connection approach. The following demonstrates how to do this.
 
The example code 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;
Line 17: Line 19:
{
{
   //make connection to server
   //make connection to server
   socketForServer = new TcpClient(ServerIO,Port);
   socketForServer = new TcpClient(ServerIP,Port);
}
}
catch
catch
Line 45: Line 47:
networkStream.Close();  // tidy up
networkStream.Close();  // tidy up
</pre>
</pre>
===VB – ActiveX===
See the [[EigenvectorTools#Visual_Basic|EigenvectorTools Visual Basic example]].


===Matlab – Socket Connection===
===Matlab – Socket Connection===
Line 55: Line 53:


<pre>
<pre>
import java.io.\*;
import java.io.*;
import java.net.\*;
import java.net.*;
 
% Example of how to set parameters. The msg command simply retrieves the loaded dataset in this simple example.
% srv  = java.lang.String('127.0.0.1');
% port = 2211;
% msg  = 'input=''C:\Data\proj1\spectrum1.spc'';:xml;input;';


%Create socket connection and socket reader and writers
%Create socket connection and socket reader and writers
Line 74: Line 77:
%wait for reply ready
%wait for reply ready
starttime = now;
starttime = now;
while \~clientIn.ready
while ~clientIn.ready
   if (now-starttime)>60/60/60/24;
   if (now-starttime)>60/60/60/24;
     error('No response from server')
     error('No response from server')

Latest revision as of 11:50, 28 July 2017

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

Although C# can easily use the EigenvectorTools .NET application to connect into Solo_Predictor, it may be useful to have a socket connection approach. The following demonstrates how to do this.

The example code 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(ServerIP,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

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.*;

% Example of how to set parameters. The msg command simply retrieves the loaded dataset in this simple example.
% srv  = java.lang.String('127.0.0.1');
% port = 2211;
% msg  = 'input=''C:\Data\proj1\spectrum1.spc'';:xml;input;';

%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