Solved

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

Posted on 2002-05-26
5
232 Views
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?

Regards,
Ganesh
0
Comment
Question by:ganeshokade
[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
5 Comments
 
LVL 32

Expert Comment

by:jhance
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7036175
ganeshokade,

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");
0
 
LVL 30

Expert Comment

by:Axter
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.
Example:
CString SectionName;
SectionName.Format("SocketNumber %i", mycsocket.m_hSocket);
CString KeyName;
KeyName.Format("PortNo%i_Close", PortNo);
WritePrivateProfileString(SectionName, KeyName, NULL, "MyFileLogName.ini");

0
 
LVL 22

Accepted Solution

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

setsockopt(SO_REUSEADDR..)

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
0
 

Author Comment

by:ganeshokade
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!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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