Solved

On Win 32 API socket programming

Posted on 1998-11-10
7
1,070 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

947 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

20 Experts available now in Live!

Get 1:1 Help Now