System.Net.Sockets.SocketException

1,156 views
Skip to first unread message

Can Dolgun

unread,
May 12, 2019, 8:10:35 AM5/12/19
to NJ4X Forum
Hi,

I got an exception from connect method. Demo account already connected to nj4x terminal. When I run connect method, throw an exception:

========================

System.Net.Sockets.SocketException
  HResult=0x80004005
  Message=Only one usage of each socket address (protocol/network address/port) is normally permitted
  Source=System.Net.Sockets
  StackTrace:
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.Net.Sockets.TcpListener.Start(Int32 backlog)
   at nj4x.Net.NJ4XServer.<.ctor>b__10_0()
   at System.Threading.Thread.ThreadMain_ThreadStart()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

========================

Terminal image:


========================

C# Code:

(Note: exception throw as Undhandled Exception) I got this error for the first time

try
{
Nj4xClient.Connect("127.0.0.1", 7788, new Broker(Broker), Account, Password, true);
}
catch (MT4Exception ex)
{
return new ProcessResult(ex);
}
catch (Exception ex)
{
return new ProcessResult(ex);
}

Roman Gerasimenko

unread,
May 12, 2019, 3:45:12 PM5/12/19
to NJ4X Forum
You need to setup different 'nj4x_server_port' values for different applications running on the same machine.

NJ4X client (Java or C#) application  automatically starts socket server, which is bound to 127.0.0.1 IP and 7777 port by default.
 
This is done for security reason, and you can change IP address and/or port using "nj4x_server_host"/"nj4x_server_portSystem properties (Java) or  App.config properties (C#)
 
Each instance of application creates socket server at client machine.
It uses port number specified by nj4x_server_port App.config parameter.
 
Therefore, it is not possible to start two different applications using the same nj4x_server_port parameter.
 
P.S.
In distributed environment MT4 terminals would be able to connect to your Java/.Net application, if you set  "nj4x_server_host" system property to the available IP address of your app machine, accessible from MT4 terminal's box.
 
e.g.,
Terminal Server machine: 192.168.0.1
Java/.Net app machine: 192.168.0.123

=> C# App.config:
<configuration>...<appSettings>...
<add key="nj4x_server_host" value="192.168.0.123" />
...</appSettings>...</configuration>
...
        // strategy connects to Terminal Server as usual
            mt4.Connect( "192.168.0.1", 7788,  new Broker(p.Broker), p.Account, p.Password);
 
=> Java app class: ... static { System.setProperty("nj4x_server_host", "192.168.0.123");  } ...
        // strategy connects to Terminal Server as usual
                   strategy.connect("192.168.0.1", 7788, new Broker("..."), ...
 
 
P.S.
nj4x_server_port - this is really confusing property name, but this is not a terminal server port. It is port of your application, mt4 terminals connects to.

So, you need to set different nj4x_server_port values for different applications, e.g. 5555 for app A and 5566 for application B.
Port of TS remains unchanged (i.e. 7788) and you pass this port in the connect method: session.Connect(tsHost, tsPort=7788, ...)

нд, 12 трав. 2019 о 15:10 Can Dolgun <hc.d...@gmail.com> пише:
--
You received this message because you are subscribed to the Google Groups "NJ4X Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nj4x+uns...@googlegroups.com.
To post to this group, send email to nj...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nj4x/74e961ae-c360-4609-afa4-a60aead658dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages