my "connect" function always immediately returns 0; even when not really connected to anything.
And then my recv function will sit for about 30 seconds then timeout.
I tried specifying every WinSoc ver on WSAStartup from 1.0 to 2.2, all have same effect.
I'm using MinGW, No MFC. Is there something else I should be doing?
Code:
//request to startup use for sockets version 2.0
WSADATA wsaData;
int ret = WSAStartup(WSA_VER,&wsaData);
if(ret!=0)
{
TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
return FALSE;
}
//create a TCP socket
SOCKET soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(soc == INVALID_SOCKET)
{
TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
WSACleanup();
return FALSE;
}
//check if we have a host name or IP address
BOOL bIsHost = isalpha(sServ[0]);
//resolve IP if needed
LPHOSTENT hostEntry = {0};
if(bIsHost)//resolve hostname to IP address list
{
hostEntry = gethostbyname(sServ);
//display IP address in debugger screen (just display the first IP in the list of returned IP's)
TRACE("\nRESOLVED IP: %s", (char*)inet_ntoa(*(in_addr*)hostEntry->h_addr_list[0]) );
TRACE("\nRESOLVED HOSTNAME: %s", (char*)hostEntry->h_name );
if(!hostEntry)
{
TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
WSACleanup();
return FALSE;
}
}
//connection data
sockaddr_in con;
con.sin_family = AF_INET;
if(bIsHost)
{ con.sin_addr.s_addr = *(u_long*)hostEntry->h_addr_list[0]; }//use the IP address we resolved
else
{ con.sin_addr.s_addr = inet_addr(sServ); }//an IP was passed in, so just use that
con.sin_port = htons(iPort);
//try to connect
ret = connect(soc,(SOCKADDR*)&con,sizeof(con));
if(ret != 0)
{
TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
WSACleanup();
return FALSE;
}
TRACE("\n Connect Returned: %d, socket value = %d",ret,soc);
What is the value of bIsHost? Are you trying to set yourself up as a server (perhaps bIsHost is true) and then connect to yourself? That obviously cannot work. You would need two instances of your program, one which sets itself up as a server and calls accept (and does not call connect), and the other which sets itself up as a client and calls connect (and does not call accept).
Note that the code is not identical to the code at the MS site. If the code is unaltered, does it work as expected?
Unaltered MS code does the same thing.
I'm not setting myself up as a server, I am connecting to my appache server on another computer when I want to test a good connection, and just making up fake local IP's when I want to test a failed connection.
bIsHost changes depending on if sServ is "www.mywebsite.com" or "192.168.2.100"
bIsHost is not the problem, the code all works, I can download files without problem. everything is working, the problem is "connect" always immediately returns 0 (returns instantly! it does not even block!), even if the ip address is 0.0.0.0 or 192.168.10.0 (which does not exist on my network), or if my network adapter is unplugged. It returns 0 as if it just made a successful connection. then when the code reaches my "recv" blocking function, the "recv" times out (it times out because im not really connected, even though the "connect" function lied and said I was)
Problem is, the code should never go past the "connect" function when I specify and invalid IP, or IP that does not exist. but it does!.
See attached picture for example of what seems impossible according to MSDN details of how "connect" works
Last edited by 12oclocker; November 1st, 2011 at 07:26 PM.
If the server isn't running when the client is started the client terminates with 'Unable to connect to server!'
If the server is started when the client is started the send/echo everything works alright.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Thanks SMA, I created a new empty project, and copied and pasted those examples in, and re-linked to the libraries, then "connect" behaved as expect. So I'll need to do some digging to see why it's not working on my GUI app correctly. I'm unaware of anything that would cause connect to misbehave like this, but something is causing it, thanks!
Is your code inside a message handler? If so, then perhaps you have re-entrancy issues.
Perhaps you could post more of the code. It was not clear, for example, that your code was used in a GUI application. In fact, use of blocking socket calls in a GUI application, or any functions that block indefinitely in a GUI applications, is somewhat unexpected and perhaps ill-advised.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.