MyException - 我的异常网
当前位置:我的异常网» C++ Builder » 妖哥能不能帮忙看一上

妖哥能不能帮忙看一上

www.MyException.Cn  网友分享于:2013-01-08  浏览:4次
妖哥能不能帮忙看一下

妖哥:问一个问题,我的局域网上有一个设备,我要搜索到这台设备就要通过UDP广播发送请求命令去查询,然后设备通过广播返回设备信息给我。
下面是我的发送命令的过程:
        int t_nRet = 0;
        SOCKET t_Socket = NULL;
        SOCKADDR_IN t_SocketAddrin;
        BOOL t_bBroadcast = TRUE;
        
        t_SocketAddrin.sin_family = AF_INET;
        t_SocketAddrin.sin_port = htons(1092);
        t_SocketAddrin.sin_addr.s_addr = inet_addr("255.255.255.255");

        char t_chMessage[113] =
        {
            0x5a,0x4c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00
        };

        t_Socket = socket(AF_INET, SOCK_DGRAM, 0);
        if(t_Socket == SOCKET_ERROR)
        {
            return;
        }

        t_nRet = setsockopt(t_Socket,SOL_SOCKET,SO_BROADCAST,(const char*)&t_bBroadcast,sizeof(BOOL));
        if(t_nRet != 0)
        {
            closesocket(t_Socket);
            return;
        }

        sendto(t_Socket, t_chMessage, 113, 0, (struct sockaddr*)&t_SocketAddrin, sizeof(SOCKADDR_IN));

        closesocket(t_Socket);

==================================================================
这里是我的接受网络中广播的代码:
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 0);

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有