Jump to content

  • Log In with Google      Sign In   
  • Create Account


Avoiding address already in use


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 yahastu   Members   -  Reputation: 152

Like
0Likes
Like

Posted 25 August 2012 - 04:45 PM

I wrote my server in C++ and put it on am amazon ec2-micro instance, listening for connections on port 1234. Tested it over period of weeks with no issues. I left it running indefinitely for several months and after some period of time (unknown) discovered it was no longer running. I logged in and tried to restart and got the "address already in use" error when attempting to bind the listensocket to port 1234. Note that SO_REUSEADDR was already set. Over a period of several days I continued to try and it has never been released. I suppose this amazon instance is set with TIME_WAIT=infinity so it will wait until server reboots, but I am afraid to reboot it due to posts like this: http://blog.9minutes...ot-t1micro-ec2/

I have discovered that whenever I kill the server process when it is currently connected to a client, this error will occur when I try to restart the server..so I need to change the port that it waits on to get it back up. But thats not viable solution, because clients need to hardcode the port to connect to. What do I do?

bool resume_listening()
  {
   std::cout << "Listening for new connections..\n";
   if ( ::listen( listenSocket, SOMAXCONN ) == SOCKET_ERROR )
   {
	perror("listen listenSocket");
	std::cout << "Error listening on socket\n";
	closesocket(listenSocket);
	return false;
   }
   return true;
  }
  bool create_listen_socket(int port)
  {
   std::cout << "Creating listener socket..\n";
   //create listen socket
   listenSocket = socket( AF_INET, SOCK_STREAM, 0 );
   if (listenSocket == INVALID_SOCKET) {
	perror("create listenSocket");
	std::cout << "Unable to create listenSocket\n";
	return false;
   }
   //----------------------
   // The sockaddr_in structure specifies the address family,
   // IP address, and port of the client to be connected to.
   struct sockaddr_in service;
   service.sin_family = AF_INET;
   service.sin_addr.s_addr = INADDR_ANY;
   service.sin_port = htons(port);
   // Enable address reuse
   //discussion: http://hea-www.harvard.edu/~fine/Tech/addrinuse.html
   int on = 1;
   setsockopt( listenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on) );
   //setup
   if ( bind(listenSocket, (SOCKADDR*)&service, sizeof(service) ) == SOCKET_ERROR )
   {
	perror("bind listenSocket");
	std::cout << "Unable to bind listenSocket\n";
	closesocket(listenSocket);
	return false;
   }
   return resume_listening();
  }


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8178

Like
0Likes
Like

Posted 26 August 2012 - 12:36 PM

How about not killing the server while it's connected to a client? Find a way to guarantee the server socket will be closed before the server dies - perhaps via some sort of wrapper process which ensures the socket gets cleaned up... why do you kill your server anyway?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 hplus0603   Moderators   -  Reputation: 4978

Like
0Likes
Like

Posted 27 August 2012 - 10:16 AM

You can track this down with "netstat -napl -t tcp" and with "ps alx" run as root.
Also, you are going to have to reboot your machine sometime. You might want to save away all source and data that's important to you so you can easily restore it to a new image, and reboot, just to check.
Also, 10.04 LTS is super old and gnarly now -- 12.04 LTS is out, and even that is much older than rolling releases.

enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS