//+------------------------------------------------------------------+
//| SocketIsConnected.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com
#property version "1.00"
#property description "Add Address to the list of allowed ones in the terminal settings to let the example work"
#property script_show_inputs
input string Address ="www.mql5.com";
input int Port =80;
input bool CloseSocket=true;
bool ExtTLS =false;
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart(void)
{
//--- 创建一个套接字并取得它的句柄
int socket=SocketCreate();
//--- 检查句柄
if(socket!=INVALID_HANDLE)
{
//--- 如果一切正常, 就连接
if(SocketConnect(socket,Address,Port,1000))
{
PrintFormat("Established connection to %s:%d",Address,Port);
string subject,issuer,serial,thumbprint;
datetime expiration;
//--- 如果连接有证书保护, 显示它的数据
if(SocketTlsCertificate(socket,subject,issuer,serial,thumbprint,expiration))
{
Print("TLS certificate:");
Print(" Owner: ",subject);
Print(" Issuer: ",issuer);
Print(" Number: ",serial);
Print(" Print: ",thumbprint);
Print(" Expiration: ",expiration);
ExtTLS=true;
}
//--- 向服务器发送 GET 请求
if(HTTPSend(socket,"GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n"))
{
Print("GET request sent");
//--- 读取响应
if(!HTTPRecv(socket,1000))
Print("Failed to get a response, error ",GetLastError());
}
else
Print("Failed to send GET request, error ",GetLastError());
}
else
{
PrintFormat("Connection to %s:%d failed, error %d",Address,Port,GetLastError());
}
//--- 如果设置了标志, 就在使用后关闭套接字
if(CloseSocket)
SocketClose(socket);
}
else
Print("Failed to create a socket, error ",GetLastError());
//--- 检查服务器连接
bool connected=SocketIsConnected(socket);
//--- 如果没有连接就终止运行
if(!connected)
{
Print("No connection to server");
}
//--- 如果服务器连接存在
else
{
Print("Connection to the server is available\nThe connection needs to be closed. Closing");
//--- 关闭套接字并再次检查连接状态
SocketClose(socket);
connected=SocketIsConnected(socket);
}
//--- 显示当前服务器连接状态
Print("Currently connected: ",(connected ? "opened" : "closed"));
/*
result in case CloseSocket = true:
No connection to server
Currently connected: closed
result in case CloseSocket = false:
Connection to the server is available
The connection needs to be closed. Closing
Currently connected: closed
*/
}
//+------------------------------------------------------------------+
//| 向服务器发送命令 |
//+------------------------------------------------------------------+
bool HTTPSend(int socket,string request)
{
//--- 把字符串转换成字符数组, 丢弃结尾的零
char req[];
int len=StringToCharArray(request,req)-1;
if(len<0)
return(false);
//--- 如果使用了通过端口443的安全TLS连接
if(ExtTLS)
return(SocketTlsSend(socket,req,len)==len);
//--- 如果使用了常规TCP连接
return(SocketSend(socket,req,len)==len);
}
//+------------------------------------------------------------------+
//| 读取服务器响应 |
//+------------------------------------------------------------------+
bool HTTPRecv(int socket,uint timeout_ms)
{
char rsp[];
string result;
ulong timeout_check=GetTickCount64()+timeout_ms;
//--- 在有数据时从套接字读取数据,但不能超过超时时间
do
{
uint len=SocketIsReadable(socket);
if(len)
{
int rsp_len;
//--- 不同的读取命令取决于连接是否安全
if(ExtTLS)
rsp_len=SocketTlsRead(socket,rsp,len);
else
rsp_len=SocketRead(socket,rsp,len,timeout_ms);
//--- 解析响应
if(rsp_len>0)
{
result+=CharArrayToString(rsp,0,rsp_len);
//--- 仅显示响应头
int header_end=StringFind(result,"\r\n\r\n");
if(header_end>0)
{
Print("HTTP answer header received:");
Print(StringSubstr(result,0,header_end));
return(true);
}
//--- 更新读取超时过期时间
timeout_check=GetTickCount64()+timeout_ms;
}
}
}
while(GetTickCount64()<timeout_check && !IsStopped());
return(false);
}
|