2

The following is the scenario of me trying to connect the socket through MQL5 as client, but the server is from the Python. See the following:
Server.py

import socket

def actual_work():

    return 'dummy_reply'


def main():
    sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
    try:
        sock.bind( ('127.0.0.1', 6666) )

        while True:
            data, addr = sock.recvfrom( 4096 )
            reply = actual_work()
            sock.sendto(reply, addr)
    except KeyboardInterrupt:
        pass
    finally:
        sock.close()
if __name__ == '__main__':
    main()

Client from MQL5

#include <simple-client-socket-send-only.mqh>

ClientSocket * glbSocket;

void OnInit()
{
   glbSocket = new ClientSocket("127.0.0.1", 6666);
   if (!glbSocket.IsSocketConnected()) {
      Print("Failed to connect");
   }
}

void OnDeinit(const int reason)
{
   delete glbSocket;
}


void OnTick()
{
   if (glbSocket.IsSocketConnected()) {
      Print(glbSocket.Send("Hello"));
   }
}

The library included is here: Socket client MQH

I am not able to connect to the socket using this format. Please let me know, if I am missing something. Help me improve.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • Looks a bit like you pass around strings while I would expect it to be bytes. Not sure about the MQL5 part, but running your server code with some q&d Python client only required me to change `sock.sendto(reply, addr)` into `sock.sendto(bytes(reply, encoding="utf-8"), addr)` for it to work. – shmee Aug 07 '18 at 10:28
  • @shmee I am not able to connect the server, will the conversion be helpful in making the connection feasible? I don't think that the connection has anything to do with the string - byte conversion. What do you say about it? – Jaffer Wilson Aug 07 '18 at 10:47
  • Oh, wait ... just glanced at your client library. If I'm not mistaken (and I could be as I have no clue about MQL5), you create a [stream socket](https://gist.github.com/JafferWilson/11ab0d4cb44fca1481e4a167115243f4#file-simple-client-socket-send-only-mqh-L105) on your client and try to connect it to a DGRAM socket on your server. – shmee Aug 07 '18 at 10:52
  • Whatever the issue with the connection will turn out to be, in Python's socket implementation [sendto](https://docs.python.org/3/library/socket.html#socket.socket.sendto) and all other implementations of `send` expect a bytes-like object as payload – shmee Aug 07 '18 at 10:58
  • @shmee Yes I know the send issue. Its true. I will correct it when it will give error. But current, there is no connection at all. That is my issue. Once I connect to the port then I will handle the error for the byte and string. – Jaffer Wilson Aug 07 '18 at 11:59
  • Could you please check the socket types as I mentioned in the comment above that? Your client socket appears to be STREAM, your server is DGRAM. That might very well be the issue with your connection ... – shmee Aug 07 '18 at 12:03
  • @shmee Sure I will thank you for the suggestion – Jaffer Wilson Aug 07 '18 at 12:37
  • @shmee I have tried changing the `SOCK_STREAM` to `SOCK_DGRAM` I am still not able to connect the socket. I guess there is some another issue behind it. – Jaffer Wilson Aug 08 '18 at 05:56
  • Well, your Python server worked for me when testing with my q&d python client, so maybe you wanna concentrate on the MQL5 for starters. I can't help with that however. – shmee Aug 08 '18 at 06:02
  • Ok Thank you for your suggestion and help. – Jaffer Wilson Aug 08 '18 at 09:34

0 Answers0