c - WSA Connection refused even though the host is not present -


i'm writing code discover devices on network , part of it.

i'm trying discover host establishing socket connection host on port 80.

it connects on of hosts on port 80 devices listening on port 80. @ times connect function returns error wsaconnectionrefused there no web service running on host.

it surprising see when try establish socket connection on invalid ip addresses wsaconnectionrefused instead of wsaetimedout.

i googled , found out antivirus , firewall can cause problems , have disabled both on scanning machine no luck causing problem?

i have posted code below:

#ifndef unicode #define unicode #endif  #define win32_lean_and_mean  #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h>  #pragma comment(lib, "ws2_32.lib")  int main() {   wsadata wsadata;   int iresult = wsastartup(makeword(2, 2), &wsadata);   if (iresult != no_error)   {     wprintf(l"wsastartup function failed error: %d\n", iresult);     return 1;   }   socket connectsocket;   connectsocket = socket(af_inet, sock_stream, ipproto_tcp);    if (connectsocket == invalid_socket)   {     wprintf(l"socket function failed error: %ld\n", wsagetlasterror());     wsacleanup();     return 1;   }   sockaddr_in clientservice;   clientservice.sin_family = af_inet;    clientservice.sin_addr.s_addr = inet_addr("192.168.4.28"); //no host ip   address     clientservice.sin_port = htons(80); //port 80     iresult = connect(connectsocket, (sockaddr *) &clientservice,       sizeof(clientservice));    printf("the socket connect return status : %d ", iresult);   if (iresult == socket_error)   {      wprintf(l"connect function failed error: %ld\n", wsagetlasterror());      iresult = closesocket(connectsocket);     if (iresult == socket_error)       wprintf(l"closesocket function failed error: %ld\n",           wsagetlasterror());     wsacleanup();     return 1;   }    wprintf(l"connected server.\n");    iresult = closesocket(connectsocket);   if (iresult == socket_error)   {     printf("\n socket connection failed ");     wsacleanup();     return 1;   }    wsacleanup();   return 0; } 


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -