Libraries: WinSock

 

WinSock:

A WinSock library and a client/server example

Author: rob

 

The client doesn't compile, I tried to modify this:

hp = gethostbyaddr(addr, 4, AF_INET);
Print("server addr:"+addr[0]+" hp"+hp);

for this:

hp = gethostbyaddr(addr[0], 4, AF_INET);
Print("server addr:"+addr[0]+" hp"+hp);


But then trying to connect in the same machine with two charts the client programs says:

2009.10.30 09:46:51 wsockclnt EURUSD,H1: Client: Cannot resolve address "127.0.0.1": Error :11004

 

The modification should be:



hp = gethostbyaddr(server_addr, 4, AF_INET);

but it doesn't work either. This is working:

hp = addr[0];

The script is not resolving names anyway.

Regards

 
NablaQuant:

The modification should be:



hp = gethostbyaddr(server_addr, 4, AF_INET);

but it doesn't work either. This is working:

hp = addr[0];

The script is not resolving names anyway.

Regards

hp is a pointer to a winsock's host information structure (struct hostent). Here the script doesn't use gethostbyaddr() to resolve the name but to check if server_addr is valid. Error 11004 means valid name, no data record of requested type which might mean you might have a problem with your IP stack since 127.0.0.1 is the loopback address and is defined also in the hosts file as "localhost". So error 11004 is recoverable. Furthermore you can bypass completely gethostbyaddr() since it is used only for checking.

 

Seems that struct2str doesnt work properly.


For exemple the string "hello" sended from the client side give me "hhhhoooo" on the serverside


Edit: ok i found the error:

in string struct2str(int struct[], int field) int x=struct[p]>>(o<<3); is not in the right place

so the correct function is:

string struct2str(int struct[], int field) {
string r="";
int l=field>>16;
if (l==0) return(r);
field&=0xFFFF;
int p=field>>2;
int o=field%4;
for (l=l; l>0; l--) {

int x=struct[p]>>(o<<3);
r=r+CharToStr(x&0xFF);
if (o==3) {
o=0;
p++;
x=struct[p];
} else
o++;
}
return(r);
}

 

First of all I notice that no-one has acknowledged ryaz for his great contribution. The only winsock related MQL discussion I have found on the net. Good stuff ryaz.

To some technicals. (I am a bit of a newbie so bear with me.)

Q. 1. I notice that when AddSocketToArray is called, the code pauses until a new connection request is received. Which is all good and well. Is there a similar function that can be called that proceeds (in same fashion) only when a client disconnects? (e.g. WSAWaitForMultipleEvents)

Or some sort of object that fires an event when client disconnects? At the moment I have to use a messy array approach that appends values of all socket requests i.e.

int AddSocketToArray(int intNewSocket) {
//Add new socket to first available pidgeon hole

for(int intLoopCounter=0;intLoopCounter<=99;intLoopCounter++) {
if (intMessageSocket[intLoopCounter] == 0) {
intMessageSocket[intLoopCounter] = intNewSocket;
return(intLoopCounter);
}
}

}

then has to continually loop through the entire array to check if connections are still active.

Q. 2. This also presents an issue of managing the array, (which is, in part, dealt with quite effectively by function above). But I am sure that there must be some method for enumerating on object containing active connections. Any ideas?

Thanks,

 

@ohilton576 you're welcome

if you get aquainted with winsock programming you'll realize that there are three models of communication the blocking one presented here the non-blocking (both polling oriented) and the most efficienf the overlapped. The library presented here is in a basic form so it doesn't show how the latter two models can be implemented.

 

Since nobdy rated yet i will give a 10 rating. The information i derived from this library was very helpful for creating my rudimentary mql4 IRC client. (Although I didn't use this library itself, only looked at how things are done i wouldn't have been able to do it so easily)

 
7bit:

Since nobdy rated yet i will give a 10 rating. The information i derived from this library was very helpful for creating my rudimentary mql4 IRC client. (Although I didn't use this library itself, only looked at how things are done i wouldn't have been able to do it so easily)



Hello,
I cannot set a timeout on recv() function. I read that I should use setsockopt but I don't know hot to insert ti right syntax on winsock.mqh and the right syntax on the client code. Anyone could help me?
Thanks Mario
 
NablaQuant:

The client doesn't compile, I tried to modify this:

hp = gethostbyaddr(addr, 4, AF_INET);
Print("server addr:"+addr[0]+" hp"+hp);

for this:

hp = gethostbyaddr(addr[0], 4, AF_INET);
Print("server addr:"+addr[0]+" hp"+hp);


But then trying to connect in the same machine with two charts the client programs says:

2009.10.30 09:46:51 wsockclnt EURUSD,H1: Client: Cannot resolve address "127.0.0.1": Error :11004




Hello,
I cannot set a timeout on recv() function. I read that I should use setsockopt but I don't know hot to insert ti right syntax on winsock.mqh and the right syntax on the client code. Anyone could help me?
Thanks Mario
 

brosnet:

Hi,

could somebody please take a look on struct2string ? becasue is not working and I can`t find out whats wrong thx

I get instead Hello and hhhhhhhlllloooo


NablaQuant:

The client doesn't compile, I tried to modify this:

hp = gethostbyaddr(addr, 4, AF_INET);
Print("server addr:"+addr[0]+" hp"+hp);

for this:

hp = gethostbyaddr(addr[0], 4, AF_INET);
Print("server addr:"+addr[0]+" hp"+hp);


But then trying to connect in the same machine with two charts the client programs says:

2009.10.30 09:46:51 wsockclnt EURUSD,H1: Client: Cannot resolve address "127.0.0.1": Error :11004




Hello,
I cannot set a timeout on recv() function. I read that I should use setsockopt but I don't know hot to insert ti right syntax on winsock.mqh and the right syntax on the client code. Anyone could help me?
Thanks Mario
Reason: