Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 获得;“未连接传输终结点”;C++;_C++_C_Linux_Sockets_Udp - Fatal编程技术网

C++ 获得;“未连接传输终结点”;C++;

C++ 获得;“未连接传输终结点”;C++;,c++,c,linux,sockets,udp,C++,C,Linux,Sockets,Udp,当我尝试连接时,UDP服务器程序中出现传输端点未连接错误 通过shutdown关闭插座(m_ReceiveSocketId,shutu RDWR) 以下是我的代码片段: bool UDPSocket::receiveMessage() { struct sockaddr_in serverAddr; //Information about the server struct hostent *hostp; // Information about thi

当我尝试连接时,UDP服务器程序中出现
传输端点未连接
错误 通过
shutdown关闭插座(m_ReceiveSocketId,shutu 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;

    }
bool-UDPSocket::receiveMessage()
{
serverAddr;中的struct sockaddr\u//有关服务器的信息
struct hostent*hostp;//有关此设备的信息
char buffer[BUFFERSIZE];//用于存储传入消息的缓冲区
int serverlen;//用于存储服务器地址长度
//打开数据报套接字
如果((m_ReceiveSocketId=socket(AF_INET,SOCK_DGRAM,0))<0)
{
实用程序单例::打印日志(日志错误,(%s%s%d)UDP客户端-套接字()错误;
pthread_exit(NULL);
返回false;
}
//配置服务器地址。
//设置族和端口
serverAddr.sin_family=AF_INET;
serverAddr.sinu-port=htons(m\u-listingport);
if(绑定(m_ReceiveSocketId,(struct sockaddr*)和serverAddr,sizeof(struct sockaddr_in))<0)
{
实用工具\u单例::打印日志(日志错误,(%s%s%d)UDP客户端-套接字绑定错误=%s“,\uuuuu文件\uuuuuu,\uuuuu函数\uuuuu,\uuuuu行错误,strerror(errno));
pthread_exit(NULL);
返回false;
}
//TODO重新路由机制。
if((serverAddr.sin\u addr.s\u addr=inet\u addr(m\u ServerIPStr.c\u str())===(无符号长)INADDR\u NONE)
{
/*使用gethostbyname()函数检索*/
/*主机服务器的地址(如果系统*/
/*将服务器的主机名作为参数传递*/
/************************************************/
/*获取服务器地址*/
hostp=gethostbyname(m_ServerIPStr.c_str());
if(hostp==(struct hostent*)NULL)
{
/*通常定义h_errno*/
/*在netdb.h中*/
实用程序单例::打印日志(日志错误、%s%d%s%s%d、“找不到主机”、h\u错误号、\u文件、\u函数、\u行);
pthread_exit(NULL);
返回false;
}
memcpy(&serverAddr.sin_addr,hostp->h_addr,sizeof(serverAddr.sin_addr));
}
serverlen=(int)sizeof(serverAddr);
//循环并侦听传入的消息
while(m_receiveflag)
{
int receivedByte=0;
memset(buffer,0,BUFFERSIZE);
//从服务器接收数据
receivedByte=recvfrom(m_ReceiveSocketId,buffer,BUFFERSIZE,0,(struct sockaddr*)和serverAddr,(socklen_t*)和serverlen);
如果(接收字节==-1)
{
实用程序单例::打印日志(日志错误,[%s:%d#%s]UDP客户端-接收错误“,uuuu文件,uuu行,uuu函数);
关闭(m_ReceiveSocketId);
pthread_exit(NULL);
返回false;
}
否则如果(接收字节>0)
{
字符串rMesg;
rMesg.erase();
for(int-loop=0;loopsetReceivedMessage(rMesg);
升高(SIGUSR1);
}
}
关闭(m_ReceiveSocketId);
pthread_exit(NULL);
返回true;
}
任何帮助都将不胜感激。
谢谢,Yuvi。

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是无连接协议


您只需关闭套接字并将套接字设置为无效。然后在析构函数中检查套接字是否已设置为无效的\u套接字,然后再关闭它

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是无连接协议


您只需关闭套接字并将套接字设置为无效。然后在析构函数中检查套接字是否已设置为无效的\u套接字,然后再关闭它

您不应该在调用
bind
之前设置
serverAddr.sin\u addr
?另外,您从不在任何地方调用
shutdown
shutdown
位于类的析构函数中。关于bind,我不这么认为。。如果我在
serverAddr.sin\u addr
之后使用
bind
它就不起作用了如果对
shutdown
的调用在析构函数中,那么在大多数情况下你还没有关闭套接字吗?
receiveMessage
是一个线程函数,如果我没有调用
shutdown
,那么执行将进入
recvfrom
或监听模式。因此,为了摆脱套接字的监听模式,我使用了
shutdown
。并且在
recvfrom
之后继续执行。我自己没有太多使用UDP套接字,但是快速的Google搜索表明不应该在UDP套接字上使用
shutdown
recvfrom
阻塞是因为套接字正在阻塞,请将套接字设为非阻塞状态,或者将标志
MSG\u DONTWAIT
设置为
recvfrom
。在调用
bind
之前,是否应该设置
serverAddr.sin\u addr
?另外,您从不在任何地方调用
shutdown
shutdown
位于类的析构函数中。关于bind,我不这么认为。。如果我在
serverAddr.sin\u addr
之后使用
bind
它不起作用如果对
shutdown
的调用在析构函数中,您在大多数情况下不是已经通过t关闭了套接字吗