Solved

On Win 32 API socket programming

Posted on 1998-11-10
7
1,071 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Import all database objects 12 50
Changing the installation path of this MSI 5 100
What are the big features of MVC5? 4 83
Authentication of Web Services 3 72
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 …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 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

11 Experts available now in Live!

Get 1:1 Help Now