# HG changeset patch # User Giulio Moro # Date 1440426334 -3600 # Node ID 719119fb29055c7d947f89e817d414a089250b2f # Parent 850a4a9bd832a3be0e6f7bc4913696b79789149d Closing socket in the destructor diff -r 850a4a9bd832 -r 719119fb2905 core/UdpServer.cpp --- 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) diff -r 850a4a9bd832 -r 719119fb2905 include/UdpServer.h --- 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 #include #include +#include #include #include #include @@ -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();