2

Transport endpoint is not connected在 UDP 服务器程序中遇到错误,而我试图通过shutdown(m_ReceiveSocketId, SHUT_RDWR); 以下是我的代码片段来关闭套接字:

 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;

    }

任何帮助,将不胜感激。谢谢尤维。

4

1 回答 1

4

您不需要为 UDP 套接字调用 shutdown()。从手册页:

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

如果在 UDP 套接字上调用 shutdown(),它将返回 ENOTCONN(指定的套接字未连接),因为 UDP 是无连接协议。

您需要做的就是关闭套接字并将套接字设置为 INVALID_SOCKET。然后在你的析构函数中检查套接字在关闭之前是否已经设置为 INVALID_SOCKET 。

于 2012-04-12T08:25:56.243 回答