//+------------------------------------------------------------------+ //| SocketExample.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2000-2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property description "Örneğin çalışmasına izin vermek için terminal ayarlarında izin verilenler listesine Adres ekleyin" #property script_show_inputs input string Address="www.mql5.com"; input int Port =80; bool ExtTLS =false; //+------------------------------------------------------------------+ //| Sunucuya komut gönder | //+------------------------------------------------------------------+ bool HTTPSend(int socket,string request) { char req[]; int len=StringToCharArray(request,req)-1; if(len<0) return(false); //--- 443 numaralı bağlantı noktası üzerinden güvenli TLS bağlantısı kullanılıyorsa if(ExtTLS) return(SocketTlsSend(socket,req,len)==len); //--- standart TCP bağlantısı kullanılıyorsa return(SocketSend(socket,req,len)==len); } //+------------------------------------------------------------------+ //| Sunucu yanıtını oku | //+------------------------------------------------------------------+ bool HTTPRecv(int socket,uint timeout) { char rsp[]; string result; uint timeout_check=GetTickCount()+timeout; //--- soketten verileri zaman aşımından uzun olmayacak şekilde mevcut oldukları sürece oku do { uint len=SocketIsReadable(socket); if(len) { int rsp_len; //--- bağlantının güvenli olup olmamasına bağlı olarak çeşitli okuma komutları if(ExtTLS) rsp_len=SocketTlsRead(socket,rsp,len); else rsp_len=SocketRead(socket,rsp,len,timeout); //--- cevabı analiz et if(rsp_len>0) { result+=CharArrayToString(rsp,0,rsp_len); //--- yalnızca yanıt başlığını yazdır int header_end=StringFind(result,"\r\n\r\n"); if(header_end>0) { Print("HTTP cevap başlığı alındı:"); Print(StringSubstr(result,0,header_end)); return(true); } } } } while(GetTickCount()<timeout_check && !IsStopped()); return(false); } //+------------------------------------------------------------------+ //| Script programı başlatma fonksiyonu | //+------------------------------------------------------------------+ void OnStart() { int socket=SocketCreate(); //--- tanıtıcı değerini kontrol et if(socket!=INVALID_HANDLE) { //--- her şey yolundaysa bağlan if(SocketConnect(socket,Address,Port,1000)) { Print("Bağlantı kuruldu: ",Address,":",Port); string subject,issuer,serial,thumbprint; datetime expiration; //--- bağlantı sertifika ile güvenliyse, verilerini görüntüle if(SocketTlsCertificate(socket,subject,issuer,serial,thumbprint,expiration)) { Print("TLS sertifikası:"); Print(" Sahip: ",subject); Print(" Veren: ",issuer); Print(" Numara: ",serial); Print(" Parmak izi: ",thumbprint); Print(" Bitiş tarihi: ",expiration); ExtTLS=true; } //--- sunucuya GET isteği gönder if(HTTPSend(socket,"GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n")) { Print("GET isteği gönderildi"); //--- cevabı oku if(!HTTPRecv(socket,1000)) Print("Bir yanıt alınamadı, hata ",GetLastError()); } else Print("GET isteği gönderilemedi, hata ",GetLastError()); } else { Print("Bağlantı ",Address,":",Port," kurulamadı, hata ",GetLastError()); } //--- kullandıktan sonra soketi kapat SocketClose(socket); } else Print("Soket oluşturulamadı, hata ",GetLastError()); } //+------------------------------------------------------------------+ |