Click here to Skip to main content
16,018,534 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: __loctotime_t function in Win32 Pin
_AnsHUMAN_ 22-May-07 22:43
_AnsHUMAN_ 22-May-07 22:43 
GeneralRe: __loctotime_t function in Win32 Pin
bouli22-May-07 22:46
bouli22-May-07 22:46 
QuestionRe: __loctotime_t function in Win32 Pin
David Crow23-May-07 4:11
David Crow23-May-07 4:11 
AnswerRe: __loctotime_t function in Win32 Pin
bouli23-May-07 4:13
bouli23-May-07 4:13 
QuestionSocket Connection problem in Windows XP System from VC++ Pin
ledallam22-May-07 21:14
ledallam22-May-07 21:14 
AnswerRe: Socket Connection problem in Windows XP System from VC++ Pin
KarstenK22-May-07 21:19
mveKarstenK22-May-07 21:19 
GeneralRe: Socket Connection problem in Windows XP System from VC++ Pin
ledallam22-May-07 21:31
ledallam22-May-07 21:31 
AnswerRe: Socket Connection problem in Windows XP System from VC++ [modified] Pin
Mark Salsbery23-May-07 6:22
Mark Salsbery23-May-07 6:22 
The only thing I see right away is that your timeout value is pretty short. Half a second...
Factor in the accuracy of clock ticks and it often could be much shorter.

For what it's worth, here's an alternative method using an event instead of looping (which is
inefficient use of CPU!)...
tmpSocket = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ;
 
if (tmpSocket != INVALID_SOCKET)
{
   BOOL fConnectSuccess = FALSE;
 
   HANDLE hNetEvent = ::CreateEvent(NULL, true, false, NULL); // manual reset
 
   ::WSAEventSelect(tmpSocket, hNetEvent, FD_CONNECT);
 
   // Socket is now NON-blocking
 
   sockaddr_in SClient;
   SClient.sin_family = AF_INET;
   SClient.sin_port = htons(502);
   SClient.sin_addr.s_addr = inet_addr(strIPAddr);
 
   // Attempt to Connect - timeout 10 seconds
 
   if (SOCKET_ERROR == ::connect(tmpSocket, (sockaddr *) &SClient, sizeof(sockaddr_in)))
   {
      int rc = WSAGetLastError();
      if (rc == WSAEWOULDBLOCK)
      {
         if (WAIT_OBJECT_0 == ::WaitForSingleObject(hNetEvent, 10000))
         {
            WSANETWORKEVENTS WsaNetworkEvents;
            if (0 == ::WSAEnumNetworkEvents(tmpSocket, hNetEvent, &WsaNetworkEvents)) //(resets hNetEvent)
            {
               if (0 != WsaNetworkEvents.iErrorCode[FD_CONNECT_BIT])
               {
                  // connect error occurred
               }
               else
               {
                  // connect success!
                  fConnectSuccess = TRUE;
               }
            }
         }
         else
         {
            // timed out
         }
      }
      else
      {
         //::connect() failed
      }
   }
   else
   {
      // connect success!
      fConnectSuccess = TRUE;
   }
 
   ::closesocket(tmpSocket);
   ::CloseHandle(hNetEvent);
 
   return fConnectSuccess;
}
else
{
   return FALSE;
}



-- modified at 13:42 Wednesday 23rd May, 2007
added cleanup for event handle Roll eyes | :rolleyes:


"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

QuestionXML Parsing Pin
g_sandipan22-May-07 21:03
g_sandipan22-May-07 21:03 
AnswerRe: XML Parsing (another point of view) Pin
Joan M23-May-07 0:13
professionalJoan M23-May-07 0:13 
GeneralRe: XML Parsing (another point of view) Pin
g_sandipan23-May-07 1:43
g_sandipan23-May-07 1:43 
AnswerRe: XML Parsing Pin
James R. Twine23-May-07 3:51
James R. Twine23-May-07 3:51 
QuestionCOM+ Dll Registration Failed!! Pin
Rane22-May-07 20:32
Rane22-May-07 20:32 
AnswerRe: COM+ Dll Registration Failed!! Pin
Hans Dietrich22-May-07 21:05
mentorHans Dietrich22-May-07 21:05 
GeneralRe: COM+ Dll Registration Failed!! Pin
Rane22-May-07 23:58
Rane22-May-07 23:58 
AnswerRe: COM+ Dll Registration Failed!! Pin
Paresh Chitte23-May-07 0:12
Paresh Chitte23-May-07 0:12 
QuestionRe: COM+ Dll Registration Failed!! Pin
David Crow23-May-07 4:13
David Crow23-May-07 4:13 
QuestionHow to develop an XML file using VC++ [modified] Pin
kunal.tawde22-May-07 20:12
kunal.tawde22-May-07 20:12 
AnswerRe: How to develop an XML file using VC++ [modified] Pin
Nibu babu thomas22-May-07 20:15
Nibu babu thomas22-May-07 20:15 
AnswerRe: How to develop an XML file using VC++ Pin
Cedric Moonen22-May-07 20:19
Cedric Moonen22-May-07 20:19 
GeneralRe: How to develop an XML file using VC++ Pin
kunal.tawde22-May-07 22:40
kunal.tawde22-May-07 22:40 
AnswerRe: How to develop an XML file using VC++ Pin
Hamid Taebi22-May-07 21:05
professionalHamid Taebi22-May-07 21:05 
GeneralRe: How to develop an XML file using VC++ Pin
kunal.tawde22-May-07 22:43
kunal.tawde22-May-07 22:43 
GeneralRe: How to develop an XML file using VC++ Pin
Hamid Taebi22-May-07 23:39
professionalHamid Taebi22-May-07 23:39 
QuestionPassing a pointer to a function/dynamic memory allocation Pin
hpjchobbes22-May-07 19:44
hpjchobbes22-May-07 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.