Solved

On Win 32 API socket programming

Posted on 1998-11-10
7
1,078 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 1415921
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
ID: 1415922
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
ID: 1415923
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:necfm
ID: 1415924
Please give more detailed asnwer urgently. This is not satisfied.
0
 

Author Comment

by:necfm
ID: 1415925
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
ID: 1415926
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
ID: 1415927
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 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