Discussion of article "Working with sockets in MQL, or How to become a signal provider" - page 2

 
Alexey Volchanskiy:
Is this a forum for low-level network programming? )))
the article reveals new possibilities, not makes a test of your knowledge.

who wants to learn and use client-server technologies in

- copiers
- newsletters
- signals distribution
- exchange of trading information
he takes note of it and uses it.

but not in your case, you're just out of the loop,

so feel free to pass by.

I will delete flud.

 
o_O:
the article reveals new possibilities, not makes a test of your knowledge.

who wants to learn and use client-server technologies in

- copiers
- newsletters
- signals distribution
- exchange of trading information
he takes note and uses it.

but not in your case, you're just out of the loop,

so feel free to pass by.

Flooding will be deleted.

very well done, neat, no rush,

mapping those stupid socket structures to the MQL base.

is very clear and clear. Here https://www.mql5.com/en/articles/1361.

also about the same socket topic, but perhaps very cumbersome,

for simple tasks the approach proposed by the author is preferable...

thanks to the author, for me, for example, it is very relevant to the current project, I will use it...

 
is there any possibility to do the same on mql4 without using third-party dlls.
 
Dmitry Melnichenko:
is there any way to do the same on mql4 without using third-party dlls.

yes. all the codes given in the article are reproduced similarly in mql4. but you still have to use a Windows dll.

 

I don't understand why there is such mass scepticism when a person shares such a serious topic. Is it jealous that someone knows and someone else doesn't even realise what it is?

Especially since now everyone has an opportunity to understand the topic.

 
Dmitry Fedoseev:

I don't understand why there is such mass scepticism when a person shares such a serious topic. Is it jealous that someone knows and someone else doesn't even realise what it is?

Especially since now everyone has the opportunity to understand the topic.

The announcement was not written completely. People didn't read the article (as usual, nobody reads instructions :), and a whole page of flud appeared. Until I explained popularly that it is desirable to read the article in full before reviewing it.
 

The code in the article is wrong. You do not take into account alignment in structures at all.

// x86
typedef struct WSAData
{
  ushort wVersion;             // 2
  ushort  wHighVersion;        // 2
  char szDescription[256+1];   // 257
  char szSystemStatus[128+1];  // 129
  ushort iMaxSockets;          // 2
  ushort iMaxUdpDg;            // 2
                               // +2 insertion to get alignment 4 (the location is inaccurate, if I'm not mistaken, the compiler can place it wherever it wants)
  char  *lpVendorInfo;         // 4
}; // sizeof(WSAData) == 400

// x64
typedef struct WSAData
{
    WORD                    wVersion;                              // 2
    WORD                    wHighVersion;                          // 2
    WORD                    iMaxSockets;                           // 2
    WORD                    iMaxUdpDg;                             // 2
    char                   *lpVendorInfo;                          // 8 
    char                    szDescription[WSADESCRIPTION_LEN+1];   // 257
    char                    szSystemStatus[WSASYS_STATUS_LEN+1];   // 129
                                                                   // +6 insert to obtain alignment 8
}; // sizeof(WSAData) == 408

// Your mql structure
struct WSAData
  {
   WORD              wVersion;                              // 2
   WORD              wHighVersion;                          // 2
   char              szDescription[WSADESCRIPTION_LEN+1];   // 257
   char              szSystemStatus[WSASYS_STATUS_LEN+1];   // 129
   ushort            iMaxSockets;                           // 2
   ushort            iMaxUdpDg;                             // 2
   char              lpVendorInfo[];                        // 8 on x64, 4 on x86
  }; // sizeof(WSAData) == 402(x64), 398(x86)
The size of a structure in x64 is a theoretical calculation based on the assumption that pointer alignments == 8 (8 is most likely. But, for example, alignof(double) in structures == 4 and outside == 8. That's why I'm not 100% sure). But even if it is 4, the structure's size in x64 will be 404 instead of 408. In any case, you overflow the buffer under WSData both on x86 and x64. Before writing an article, you should make the simplest sizeof() measurements and explicitly specify the bitness of the system the code is written for (since you have taken to duplicating system structures). I am not talking about the unaligned initial address of the structure. I haven't checked the whole code, perhaps there are problems with other structures too.

By the way, if someone from the MKL team sees the message: all your addresses are unaligned, why? Modern processors are smart enough and all models will be able to read the data? A small fee in the form of performance loss, but nothing more (nothing like crashing).
 

I'm not quite right though. In MQL, the size of WSData will be much larger than necessary (lpVendorInfo[] is a dynamic array with size around 50, not a pointer). So the code should work correctly. But this is an accident, you are lucky, not correct theoretical assumptions. The structure itself in MKL is not valid, it is better to create an array of sufficient size and not declare anything. If you had written:

WSADatat wsdata
int res=WSAStartup(MAKEWORD(2,2), &wsadata);

// вместо
char wsaData[]; ArrayResize(wsaData,sizeof(WSAData));
int res=WSAStartup(MAKEWORD(2,2), wsaData);
you would have got an error.
 

yes, in MQL structures are aligned https://www.mql5.com/en/docs/basis/types/classes

judging by the contents of WinSock2.h the WSAData structure is without #pragma pack(1), so its size as you said may be more than µl.

But I can say that either something counts wrong or these data do not kill the stack, but all mcl software with these sockets works stably and without losses for months without terminal reloading.

Figuring it out all the way through would be good.

 
Just rewrite it in the style:
#define  WSA_DATA_SZ   420
...
char wsaData[WSA_DATA_SZ];
int res=WSAStartup(MAKEWORD(2,2), wsaData);
And don't worry about alignment or where the compiler put the extra bytes. You're not accessing any field anyway, so what's the point of this headache of trying to declare a structure?