[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


MFC Sockets and "Address already in Use" problem...

Posted on 2002-05-26
Medium Priority
Last Modified: 2013-11-20
Dear Friends,
   I have a simple server program using MFC sockets to receive requests from client applications on TCP/IP and respond with some data. The application basically creates a socket, binds it to a IP address and starts listening on a specific port. I have a problem here.
   Randomly the application exits with an error message "Address already in use" indicating the port number being used for listening is already in use. But I have used different port numbers which I am sure are not used elsewhere and I still get this error. Unfortunately the occurance is random. Sometimes the problem does not get solved even after restarting the computer.
   I have a feeling that when the earlier instance of this socket program exits it might not be doing a graceful shutdown and hence traces of the connection made by it are still remaining causing the next instance to fail. To be on the safer side I would like to do a "scoket deinitialisation" first and then start the application so that any old traces are removed. Can someone suggest how one could do a socket deinitialisation so that the program that starts is fail proof?

Question by:ganeshokade
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
LVL 32

Expert Comment

ID: 7035107
I know of no sure fire way other than a reboot.  If you have an application listening on a socket and it aborts it's quite possible that the socket will be left in the listening state.  Now I've seen this clear up spontaneously after a variable wait time (sometimes as long as an hour...) and other times it never clears.  I suspect it just depends on what the state of the socket was when it happened.  Since I know of no way to get the handle to the socket, there is no way to close it.

So unless someone here knows a trick to get at this, I think the answer is going to be:

1) Be more careful about leaving sockets hanging.  Careful application design should permit you do get control and cleanup in all but the most dire situation.  Trap the Top Level Exception is you have to.

2) If your application starts and cannot get its socket, warn the user and request a reboot.
LVL 30

Expert Comment

ID: 7036175

You could confirm wheather or not this is the problem, by writing to an INI file when the connection is made, and then  writing when the connection is closed.

This would be easy to do using WritePrivateProfileString function.
Open connection could have something like the following code:
CString SectionName;
SectionName.Format("SocketNumber %i", mycsocket.m_hSocket);
CString KeyName;
KeyName.Format("PortNo%i_Open", PortNo);
CString Value = "Open connection";
WritePrivateProfileString(SectionName, KeyName, Value, "MyFileLogName.ini");

Close connection can have something like the following:
CString SectionName;
SectionName.Format("SocketNumber %i", mycsocket.m_hSocket);
CString KeyName;
KeyName.Format("PortNo%i_Close", PortNo);
CString Value = "Close connection";
WritePrivateProfileString(SectionName, KeyName, Value, "MyFileLogName.ini");
LVL 30

Expert Comment

ID: 7036183
If you're afraid that your log file is going to get to big, you could just have the Close connection function remove the entry made by previous connection.
CString SectionName;
SectionName.Format("SocketNumber %i", mycsocket.m_hSocket);
CString KeyName;
KeyName.Format("PortNo%i_Close", PortNo);
WritePrivateProfileString(SectionName, KeyName, NULL, "MyFileLogName.ini");

LVL 22

Accepted Solution

ambience earned 200 total points
ID: 7040959
i think what you are missing is


winsock would not allow an address to be reused after a socket has been bound to an address, and even after the socket is closed there is a time period during which no other socket can bind to that address. By specifying SO_REUSEADDR you are supressing such behaviour and maybe this is all you need.

In MFC that would be CAsyncSocket::SetSockOpt

Author Comment

ID: 7126962
That's the nearest one. There seems to be a bug in the Win 2K SP2.0 which is causing this problem - still not resolved. It occurs the VERY FIRST time I run the socket application on a freshly installed win 2k advanced server with SP 2.0!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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