diff core/UdpServer.cpp @ 126:719119fb2905 scope-refactoring

Closing socket in the destructor
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 24 Aug 2015 15:25:34 +0100
parents 23137a333c93
children ff28e56e5b7e
line wrap: on
line diff
--- a/core/UdpServer.cpp	Sat Aug 22 02:53:36 2015 +0100
+++ b/core/UdpServer.cpp	Mon Aug 24 15:25:34 2015 +0100
@@ -13,9 +13,7 @@
 	init(0);
 }
 UdpServer::~UdpServer(){
-	printf("Close the socket\n");
-	shutdown(inSocket, 0); //Stop receiving data for this socket. If further data arrives, reject it.
-	//TODO: unbind from port. AFAIK, this involves closing the socket, therefore creating the socket should become part of bindToPort
+	close();
 };
 bool UdpServer::init(int aPort){
 	enabled=true;
@@ -47,7 +45,15 @@
 	}
 	enabled=true;
 	return true;
-};
+}
+
+void UdpServer::close(){
+	int ret=::close(inSocket);
+	if(ret != 0)
+		printf("Error while closing socket, errno: %d\n", errno);//Stop receiving data for this socket. If further data arrives, reject it.
+	inSocket=0;
+}
+
 int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){
 //	If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1.
 	if(enabled==false)