Solved

On Win 32 API socket programming

Posted on 1998-11-10
7
1,064 Views
Last Modified: 2013-12-03
My application is Multithreaded.  I have a function which performes the following action. The function reads a Host Name from the INI file and tries to retireve the host IP address using gethostbyname().  I am performing a WSAStartup() and WSACleanup() everytime in this function.  The function resides in a DLL and is called by lot of threads.

Sometimes,  this gethostbyname() call goes to a blocking state and hangs the system.

I would like to know what makes the gethostbyname() block and hang the system?
0
Comment
Question by:necfm
7 Comments
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
WSAStartup and WSACleanup should be called ONCE only, ie: at program start and at program end. The WSAStartup will load the protocol handle into the NP Pool; Cleanup may delete it. The call to gethostbyname blocks when it translate the name to IP address anyway, and its result must be read before any other call. It is thread safe. I do not remember reading that WSAStartup and WSACleanup are thread safe, so its best to do them only once.
0
 

Author Comment

by:necfm
Comment Utility
Why the system goes into a freeze mode?  It is only possible to reboot the system.  Why?  Evenafter calling wsastartup at the beginning and wsacleanup in the end only once in the process the same thing happens when we call gethostbyname.  
When we have to terminate our application we call gethostbyname to get the ipaddress of the server to inform about the closure of the clients.  This is where the system hangs.
While initialisation we do the same thing nothing happens.  Only during the termination we face this problem.
0
 
LVL 6

Expert Comment

by:WxW
Comment Utility
Use WSAAsyncGetHostByName() . gethostbyname "waits" for the host name to be resolved , and that time depends on your connection . WSAAsyncGetHostByName() allows you to have a message posted in a window when the resolving is completed .
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:necfm
Comment Utility
Please give more detailed asnwer urgently. This is not satisfied.
0
 

Author Comment

by:necfm
Comment Utility
This is not the answer. we are not using async gethostbyname.
Please answer appropirately.
0
 
LVL 1

Accepted Solution

by:
nilos earned 150 total points
Comment Utility
Let's say:

Thread A --> WSAStartup() --> gethostbyname() (blocks for X seconds) --> WSACleanup()

Thread B --> Do the same.

If thread A completes when thread B is in the blocked middle of the gethostbyname() you are closing TCP/IP for all threads. Of course the gethostbyname() is going to be in a very unstable state.

Remember that WSAStartup/WSACleanup are process/program wide.

The better thing to to is to change your DLL to handle the WSAStartup when the DLL is first called and the WSACleanup when the DLL is about to be unloaded. Something like:

//
//
//      DLLMAIN - Starts and stops SCCSERV
//
BOOL APIENTRY DllMain(HANDLE hInst, DWORD ul_reason_being_called, LPVOID lpReserved)
{
      switch(ul_reason_being_called) {
      case DLL_PROCESS_ATTACH:
            instancia=hInst;
            break;
      case DLL_PROCESS_DETACH:
            WSACleanup();
            break;
      default:
            break;
      }
    return(1);
      UNREFERENCED_PARAMETER(hInst);
      UNREFERENCED_PARAMETER(ul_reason_being_called);
      UNREFERENCED_PARAMETER(lpReserved);
}

Nilo

0
 

Author Comment

by:necfm
Comment Utility
We did the same but still it has the same behaviour.  Then we changed to get the ipaddress from the ini file itself instead of the name and then calling gethostbyname.  Now it is ok.  Your explanation was good.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now