MQL5 Socket Client connection to Python Socket Server

Описание к видео MQL5 Socket Client connection to Python Socket Server

본 동영상 포함된 블로그 글 : http://igotit.tistory.com/2116 제목 : MQL5. 소켓통신

source codes for MQL5

//+------------------------------------------------------------------+
//| Socket.mq5 |
//| igotit |
//| http://igotit.tistory.com |
//+------------------------------------------------------------------+
#property copyright "igotit"
#property link "http://igotit.tistory.com"
#property version "1.00"

int My_Socket_Handle = INVALID_HANDLE;

int OnInit()
{
My_Socket_Handle = Socket_Connect();

return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{

Socket_Close(My_Socket_Handle);

}


void OnTick()
{
string str_price_ask = DoubleToString(SymbolInfoDouble(_Symbol, SYMBOL_ASK));

Socket_Send(My_Socket_Handle,str_price_ask);
}


int Socket_Connect()
{
int h_socket = SocketCreate(SOCKET_DEFAULT);

if(h_socket != INVALID_HANDLE)
{
if(SocketConnect(h_socket,"192.168.11.65",9090,2000))
{
Print("Connected to Socket Server");


}//if(SocketConnect
else
{
Print("Fail connected to Socket Server. error code : ", GetLastError());
}


}//if(socket != INVALID_HANDLE)
else
{
Print("Fail SocketCreate error code : ", GetLastError());
}

return h_socket;
}

void Socket_Close(int socket_handle)
{
if(socket_handle != INVALID_HANDLE)
{
SocketClose(socket_handle);
My_Socket_Handle = INVALID_HANDLE;
Print("Socket Closed");
}

}

int Socket_Send(int socket_handle,string str_data)
{
if(socket_handle == INVALID_HANDLE) return 0;

uchar bytes[];
int byte_size = StringToCharArray(str_data,bytes)-1;

return SocketSend(socket_handle,bytes,byte_size);

}

===================================================
source code for python socket server
import socket

class socketserver:
def __init__(self, address = '', port = 9090):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.address = address
self.port = port
self.sock.bind((self.address, self.port))
self.cummdata = ''
def waitforconnection(self):
self.sock.listen(1)
self.conn, self.addr = self.sock.accept()
print('connected to', self.addr)
return 1

def recvmsg(self):
self.cummdata = ''
while True:
data = self.conn.recv(10000)
self.cummdata+=data.decode("utf-8")
if not data:
break

self.conn.send(bytes(self.cummdata,"utf-8")) # loop back test

return self.cummdata

def __del__(self):
print('sock close')
self.sock.close()

#####################################################

serv = socketserver('192.168.11.65', 9090)
serv.waitforconnection()

while True:
msg = serv.recvmsg()
print('received data : ',msg);

Комментарии

Информация по комментариям в разработке