34

I have created a socket file something like the following and want that the output of the socket must be read by the MQL5. See the following Python code:

daemon.py

import socket
#import arcpy

def actual_work():
    #val = arcpy.GetCellValue_management("D:\dem-merged\lidar_wsg84", "-95.090174910630012 29.973962146120652", "")
    #return str(val)
    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.py

import socket
import sys


def main():
    sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
    sock.settimeout(1)
    try:
        sock.sendto('', ('127.0.0.1', 6666))
        reply, _ = sock.recvfrom(4096)
        print reply
    except socket.timeout:
        sys.exit(1)
    finally:
        sock.close()


if __name__ == '__main__':
    main()

Kindly, help me in accepting the output of the socket through MQL5

EDITED

I just want that the reply should be accepted on the MQL5 in a variable, which produced by the daemon.py. How I can do that? Say I want that MQL5 should print the response from the Python , as in the above example, I want that MQL5 should give output as dummy_reply in string variable if possible.

Is there any possibility with ZeroMQ?

I want to get the client.py to be done with MQL5 instead of using Python. Please help me.

RMPR
  • 3,368
  • 4
  • 19
  • 31
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 1
    You have basic UDP client/server example code there. How does this relate to MQL5? What exactly do you want to communicate with MQL5? – AKX Aug 02 '18 at 09:10
  • @AKX I just want that the `reply` should be accepted on the MQL5 in a variable, whch produced by the `daemon.py`. How I can do that? Say I want that MQL5 should print the response from the python , as in the above example, I want that MQL5 should give output as `dummy_reply` in string variable if possible. – Jaffer Wilson Aug 02 '18 at 09:18
  • You got what I want to achieve or you have any questions. Feel free to ask please. – Jaffer Wilson Aug 02 '18 at 10:17
  • Can anyone hep me with this please let me know? – Jaffer Wilson Aug 06 '18 at 13:51
  • What a pity!.. does no one has any answers to my question. I=I guess my bounty is getting wasted everytime I apply for. – Jaffer Wilson Aug 13 '18 at 08:24
  • the client.py and zeromq are confusing to me. are you doing research on socket in MQL5? writing another MQL5 program to read from daemon.py? – James Li Aug 30 '19 at 01:12
  • @bigdataolddriver Yes, you got it right. – Jaffer Wilson Aug 30 '19 at 06:20
  • there are official docs and demos out there https://www.mql5.com/en/docs/network https://www.mql5.com/en/docs/network/socketconnect you may start from there , and you original question could be much simplified – James Li Aug 30 '19 at 06:31
  • I tried working with it. It was a bit confusing stuff. I started my own and created my own ways now. Thank you for your help. – Jaffer Wilson Aug 30 '19 at 10:48

1 Answers1

1

Please find a running example. Important element is to create byte object of the payload instead of string before you send as reply. socket object produces and ingests only bytes


    import socket
    import threading
    import sys


    def actual_work(data):
       print(data)
       return b'ACK'

    def daemon():
       sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
       sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
       sock.bind(('127.0.0.1', 6666))
       print("Listening on udp %s:%i" % ('127.0.0.1', 6666))
       try:

          while True:
              data, addr = sock.recvfrom(4096)
              ack = actual_work(data)
              sock.sendto(ack, addr)
       except Exception as e:
          print(e)  
       finally:
          sock.close()


    def client():
       sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
       sock.settimeout(1)
       try:
           sock.sendto(b'payload', ('127.0.0.1', 6666))
           reply, _ = sock.recvfrom(4096)
           print(reply)
       except socket.timeout as e:
           print(e)
           sys.exit(1)
       finally:
           sock.close()

    if __name__ == '__main__':
        thread = threading.Thread(target=daemon)
        thread.start()
        client()
        client()
        client()
        client()
        #Issue kill thread here
        thread.join()
Supreet Sethi
  • 1,780
  • 14
  • 24