Yuvi
Yuvi

Reputation: 1350

Getting "Transport endpoint is not connected" in UDP socket programming in C++

I am getting Transport endpoint is not connected error in UDP server program, while I am try to shutdown the socket via shutdown(m_ReceiveSocketId, SHUT_RDWR); Following is my code snippet:

 bool UDPSocket::receiveMessage()
    {
        struct sockaddr_in serverAddr; //Information about the server
        struct hostent *hostp; // Information about this device 

        char buffer[BUFFERSIZE]; // Buffer to store incoming message
        int serverlen; // to store server address length

        //Open a datagram Socket
        if((m_ReceiveSocketId = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            Utility_SingleTon::printLog(LOG_ERROR,"(%s %s %d) UDP Client - socket() error",__FILE__,__func__, __LINE__);
            pthread_exit(NULL);
            return false;
        }


        //Configure Server Address.
        //set family and port
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(m_ListeningPort);

        if (bind(m_ReceiveSocketId, (struct sockaddr *) &serverAddr,sizeof(struct sockaddr_in)) < 0 )
        {
            Utility_SingleTon::printLog(LOG_ERROR,"(%s %s %d) UDP Client- Socket Bind error=%s",__FILE__,__func__, __LINE__,strerror(errno));
            pthread_exit(NULL);
            return false;
        }

        //TODO Re-Route Mechanism.
        if((serverAddr.sin_addr.s_addr = inet_addr(m_ServerIPStr.c_str())) == (unsigned long)INADDR_NONE)
        {
            /* Use the gethostbyname() function to retrieve */
            /* the address of the host server if the system */
            /* passed the host name of the server as a parameter. */
            /************************************************/
            /* get server address */
            hostp = gethostbyname(m_ServerIPStr.c_str());
            if(hostp == (struct hostent *)NULL)
            {
                /* h_errno is usually defined */
                /* in netdb.h */
                Utility_SingleTon::printLog(LOG_ERROR,"%s %d %s %s %d", "Host Not found",  h_errno,__FILE__,__func__, __LINE__);
                pthread_exit(NULL);
                return false;
            }
            memcpy(&serverAddr.sin_addr, hostp->h_addr, sizeof(serverAddr.sin_addr));
        }


        serverlen = (int )sizeof(serverAddr);

        // Loop and listen for incoming message 
        while(m_RecevieFlag)
        {
            int receivedByte = 0;
            memset(buffer, 0, BUFFERSIZE);
            //receive data from the server
            receivedByte = recvfrom(m_ReceiveSocketId, buffer, BUFFERSIZE, 0, (struct sockaddr *)&serverAddr, (socklen_t*)&serverlen);
            if(receivedByte == -1)
            {

                Utility_SingleTon::printLog(LOG_ERROR,"[%s:%d#%s] UDP Client - receive error",__FILE__,__LINE__,__func__);
                close(m_ReceiveSocketId);
                pthread_exit(NULL);
                return false;
            }
            else if(receivedByte > 0)
            {
                string rMesg;
                rMesg.erase();
                for(int loop = 0; loop < receivedByte; loop++)
                    rMesg.append(1, buffer[loop]);
                Utility_SingleTon::printLog(LOG_DEBUG,"[%s:%d#%s] received message=%d",__FILE__,__LINE__,__func__, rMesg.length());
                QOMManager_SingleTon::getInstance()->setReceivedMessage(rMesg);
                raise(SIGUSR1);
            }

        }

        close(m_ReceiveSocketId);
        pthread_exit(NULL);
        return true;

    }

Any help would be appreciated. Thanks Yuvi.

Upvotes: 2

Views: 11545

Answers (1)

Simon Elliott
Simon Elliott

Reputation: 2117

You don't need to call shutdown() for a UDP socket. From the man page:

The shutdown() call causes all or part of a full-duplex connection on the socket
associated with sockfd to be shut down.

If you call shutdown() on a UDP socket, it will return ENOTCONN (The specified socket is not connected) because UDP is a connectionless protocol.

All you need to do is close the socket and set the socket to INVALID_SOCKET. Then in your destructor check whether the socket has already been set to INVALID_SOCKET before closing it.

Upvotes: 5

Related Questions