changeset 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 850a4a9bd832
children 6c8fb6f07b47
files core/UdpServer.cpp include/UdpServer.h
diffstat 2 files changed, 12 insertions(+), 4 deletions(-) [+]
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)
--- a/include/UdpServer.h	Sat Aug 22 02:53:36 2015 +0100
+++ b/include/UdpServer.h	Mon Aug 24 15:25:34 2015 +0100
@@ -12,6 +12,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <errno.h>
 #include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -50,6 +51,7 @@
 			return as much data as is currently available without blocking.
 		 */
 		int read(void* destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived);
+		void close();
 		int emptySocket();
 		int emptySocket(int maxBytes);
 		void *getWaste();