I guess you should have a loop for send/recv. Did you try the
client/server example in J.  I do not know much about socket in c#, is
it synchronous or asysc ?

sab, 02 Jan 2010, Matthew Brand skribis:
> I could not get the .NET/COM/DLL to talk to NinjaTrader c# so I am
> using a socket connection instead.
> 
> Below is some C# code and some J code which gets NT communicating with
> J in case anybody wants to do that. I am not a very good programmer so
> only use with caution, but it works.
> 
> c#/Ninjatrader:
> 
> #region Using declarations
> using System;
> using System.ComponentModel;
> using System.Diagnostics;
> using System.Drawing;
> using System.Drawing.Drawing2D;
> using System.Xml.Serialization;
> using NinjaTrader.Cbi;
> using NinjaTrader.Data;
> using NinjaTrader.Indicator;
> using NinjaTrader.Gui.Chart;
> using NinjaTrader.Strategy;
> using System.Net;
> using System.Net.Sockets;
> #endregion
> 
> // This namespace holds all strategies and is required. Do not change it.
> namespace NinjaTrader.Strategy
> {
>     /// <summary>
>     /// Sends samples to a J server and maintains its required positions.
>     /// </summary>
>     [Description("Sendsmessages to J server and outputs replies")]
>     public class connectJ : Strategy
>     {
>         #region Variables
>                       Socket JSocket = null;
>                       const string server = "127.0.0.1";
>                       const int port = 1500;
>                       const int bufferLength = 1024*10;
>       #endregion
>               private string messageJ(string message)
>               {
>                       // Send a message to J on server:port and return the 
> string based output.
>                       // Algo is: Connect if not connected, send message, 
> return reply.
>                       // Socket code based on:
>                       // 
> http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
>                       bool connected = false;
>                       if (JSocket == null) {
>                                       connected = false;
>                       } else {
>                               connected = JSocket.Connected;
>                       }
>                       if (! connected) {
>                               Print("Not connected, connecting...");
>                               IPHostEntry hostEntry = null;
>                               hostEntry = Dns.GetHostEntry(server);
>                               foreach(IPAddress address in 
> hostEntry.AddressList)
>                               {
>                                       IPEndPoint ipe = new 
> IPEndPoint(address, port);
>                                       Socket tempSocket =
>                                               new Socket(ipe.AddressFamily, 
> SocketType.Stream, ProtocolType.Tcp);
>                                       tempSocket.Connect(ipe);
>                                       if(tempSocket.Connected)
>                                       {
>                                               JSocket = tempSocket;
>                                               break;
>                                       }
>                                       else
>                                       {
>                                               continue;
>                                       }
>                               }
>                       }
>                       Byte[] byData = 
> System.Text.Encoding.ASCII.GetBytes(message);
>                       JSocket.Send(byData);
>                       Byte[] buffer = new Byte[bufferLength];
>                       string reply = "";
>                       int bytes = 0;
>                       // do {
>                               try {
>                                       bytes = JSocket.Receive(buffer, 
> buffer.Length, 0);
>                       reply = reply +
> System.Text.Encoding.ASCII.GetString(buffer, 0, bytes);
>                                       // Print("reply was:" + reply);
>                               }
>                               catch (System.Net.Sockets.SocketException ex) {
>                                       Print("Socket exception on read:" + 
> ex.SocketErrorCode);
>                               }
> 
>                       // } while (bytes > 0); Loop causes a hang. Do not know 
> why!
> So made buffer huge.
>                       return reply;
>               }
> 
>       
>               public override void Dispose()
>               {
>               // Clean up your resources here
>                       messageJ("NT closing connection now...");
>                       
> JSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
>                       JSocket.Close();
>               base.Dispose();
>               }
> 
>         /// <summary>
>         /// This method is used to configure the strategy and is
> called once before any strategy method is called.
>         /// </summary>
>         protected override void Initialize()
>         {
>             CalculateOnBarClose = true;
>         }
> 
>         /// <summary>
>         /// Called on each bar update event (incoming tick)
>         /// </summary>
>         protected override void OnBarUpdate()
>         {
>                       Print(messageJ("hello!"));
>               }
> 
>         #region Properties
>         #endregion
>     }
> }
> 
> 
> J:
> 
> 0 : 0
> Starts a server ready to accept trading comms from NinjaTrader.
> )
> 
> 
> load'socket'
> coinsert 'jsocket'
> 
> connect =: 3 : 0
> NB. close all open sockets
> sdcleanup ''
> NB. create a socket to LISTEN
> SKLISTEN =: 0 pick sdcheck sdsocket''
> NB. bind an address to the SKLISTEN socket
> sdcheck sdbind SKLISTEN ; AF_INET_jsocket_ ; '' ; y
> NB. tell the socket to listen for 1 message at a time
> sdcheck sdlisten SKLISTEN , 1
> NB. poll until connection recieved (must be a better way?)
> smoutput ''
> smoutput ''
> smoutput ''
> smoutput 'Waiting for client to connect...'
> while. ((3$a:) -: sdc =. sdcheck sdselect'') do. end.
> smoutput 'Connected.'
> NB. accept the connection
> skserver=: 0 pick sdcheck sdaccept SKLISTEN
> NB. close the listening socket
> sdcheck sdclose SKLISTEN
> )
> 
> messageLoop =: 3 : 0
> NB. wait for and echo messages
> NB. if connection drops then try to reestablish
> msg =. ''
> while. msg -.@:-: 'exit' do.
>       try. smoutput msg =. ; sdcheck sdrecv skserver,1000,0
>       catch.
>               smoutput 'Client not connected.'
>               connect 1500 NB. go to reconnect if any problems
>       end.
>       try. sdcheck ('You said:', msg) sdsend skserver , 0
>       catch.
>               connect 1500
>       end.
> end.
> NB. kill the socket
> smoutput 'Client sent exit command ... closing socket.'
> sdcleanup '' NB. only if msg was exit.
> )
> 
> messageLoop ''
> 
> 2009/12/31 bill lam <[email protected]>:
> [---=| TOFU protection by t-prot: 32 lines snipped |=---]

-- 
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to