Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

On Win 32 API socket programming

Posted on 1998-11-10
7
Medium Priority
?
1,082 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 300 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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 …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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 video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

597 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