Solo Predictor Example Connection Code: Difference between revisions

From Eigenvector Research Documentation Wiki
Jump to navigation Jump to search
imported>Benjamin
No edit summary
No edit summary
Line 95: Line 95:
   rcv = rcv{1};
   rcv = rcv{1};
end
end
</pre>
===Matlab – HTTP Connection===
Matlab script demonstrating how a client program can communicate with Solo_Predictor (acting as the server) using the HTTP protocol. The script sends a message and retrieve the response.
<pre>
% Load and view a data file
msg = 'input=''C:/eigenu_dm/datafolder/spectrum1.spc'';:xml;input;';   
% Create the request string   
serverIPAddress  = '127.0.0.1';
serverPort      = '2211';
httpstr          = ['http://', serverIPAddress, ':', serverPort, '/?'];
httpURL          = sprintf('http://%s:%s/?%s', serverIPAddress, serverPort, urlencode(msg));
% Make HTTP request and read response as text 
webData = webread(httpURL);
disp(sprintf('Response is: \n%s\n', webData));
</pre>
</pre>

Revision as of 14:57, 10 June 2020

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

Matlab – HTTP Connection

Matlab script demonstrating how a client program can communicate with Solo_Predictor (acting as the server) using the HTTP protocol. The script sends a message and retrieve the response.

% Load and view a data file
msg = 'input=''C:/eigenu_dm/datafolder/spectrum1.spc'';:xml;input;';    

% Create the request string     
serverIPAddress  = '127.0.0.1';
serverPort       = '2211';
httpstr          = ['http://', serverIPAddress, ':', serverPort, '/?'];
httpURL          = sprintf('http://%s:%s/?%s', serverIPAddress, serverPort, urlencode(msg));

% Make HTTP request and read response as text  
webData = webread(httpURL);

disp(sprintf('Response is: \n%s\n', webData));