Socket problems with MT3 API

 

Hi all,

I am writing an automatic trading system on a website using Microsoft Visual Studio, C#, ASP.NET, MT3APIDotNet.DLL and mtapi.DLL. With an InterbankFX demo account, it works fine when using my local host (on PC). Sometimes, I saw a problem "Invalid Socket Handle" when trying to run this application. It happens sometimes when I did not cleanup sockets (using WSACleanup function) and not disposing API objects, at the end or during debugging this application.

The solution for this moment is that I needed to restart my PC, to get all sockets cleaned up, okay.

But, it is not the best solution for when I moved this application to a commercial server. So, there are still some sockets left hanging, when testing this application there, I still get an exception "Invalid Socket Handle". I cannot ask the provider to restart the whole server to get sockets cleaned up?? So I need to find out how I can get sockets cleaned up.

Can anyone help me to get this resolved? Any experience with sockets?

Thanks for advance,

Jeff

 

It is most likely problem inside mtapi.dll provided by MetaQuotes. My code (MT3APIDotNet.DLL) only take error code from mtapi.dll and raise managed exception.

Probably this is caused by the disconnection from mt server, or problably you miss a few calls to Disconnect() or Dispose() in api objects. You can simply reconnect back by first catch the exception, call disconnect() and then connect(). Everything would be just fine.

 
scorpion:
It is most likely problem inside mtapi.dll provided by MetaQuotes. My code (MT3APIDotNet.DLL) only take error code from mtapi.dll and raise managed exception.Probably this is caused by the disconnection from mt server, or problably you miss a few calls to Disconnect() or Dispose() in api objects. You can simply reconnect back by first catch the exception, call disconnect() and then connect(). Everything would be just fine.

Well, thanks for your reply, but in a meanwhile, I have just resolved the socket problem. It happens that when I have left already all 10 sockets connected, I got an exception "Cannot create a new socket". From this point, you cannot create a new socket anymore, since all sockets were busy. So, I have added a static function to MT3APIWrapped module, to force socket deletion:


Public Shared Sub Dispose(ByVal Socket As Integer)
MtDelete(Socket)
End Sub

After recompiling MT3APIWrapped module, I called this function 10 times, from socket = 1 to 10, then everything works fine.

Jeff

Reason: