MQL5Book/Scripts/p7/SocketIsConnected.mq5
super.admin 1c8e83ce31 convert
2025-05-30 16:09:41 +02:00

57 lines
2 KiB
MQL5

//+------------------------------------------------------------------+
//| SocketIsConnected.mq5 |
//| Copyright 2022, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property description "Connects to a server via sockets and shows socket state."
#property description "NB: Default 'Server' requires to allow 'www.mql5.com' in terminal settings - to use other servers, change the settings accordingly."
#property script_show_inputs
#include "..\..\Include\PRTF.mqh"
input string Server = "www.mql5.com";
input uint Port = 443;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
PRTF(Server);
PRTF(Port);
const int socket = PRTF(SocketCreate());
if(PRTF(SocketConnect(socket, Server, Port, 5000)))
{
int i = 0;
while(PRTF(SocketIsConnected(socket)) && !IsStopped())
{
PRTF(SocketIsReadable(socket));
PRTF(SocketIsWritable(socket));
Sleep(1000);
if(++i >= 5)
{
PRTF(SocketClose(socket));
}
}
}
}
//+------------------------------------------------------------------+
/*
Example:
Server=www.mql5.com / ok
Port=443 / ok
SocketCreate()=1 / ok
SocketConnect(socket,Server,Port,5000)=true / ok
SocketIsConnected(socket)=true / ok
SocketIsReadable(socket)=0 / ok
SocketIsWritable(socket)=true / ok
SocketIsConnected(socket)=true / ok
SocketIsReadable(socket)=0 / ok
SocketIsWritable(socket)=true / ok
SocketClose(socket)=true / ok
SocketIsConnected(socket)=false / NETSOCKET_INVALIDHANDLE(5270)
*/
//+------------------------------------------------------------------+