socket UDP发送数据返回10013 20C

win7系统,已经是Administrator用户,单播使用的28900端口,并没有其他进程占用端口。在使用sendFrameData函数之前,用了一次广播sendFrameBoradCast。之后在使用单播时,就出现10013错误码,请各位大大帮我解答,谢谢了!!!

int IBUdpDev::sendFrameBoradCast(IBFrame &mFrame)
{
int errCode = -1;;
DWORD sendLen = 0;
DWORD dataLen = 0;
errCode = WSAStartup(MAKEWORD(2, 2), &wasData);
if (errCode != 0)
{
return -1;
}
SOCKET bcSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (bcSocket == INVALID_SOCKET)
{
errCode = WSAGetLastError();
closesocket(bcSocket);
printf("%s: socket eror errCode:%d\n", func, errCode);
return errCode;
}

//3.设置该套接字为广播类型,
bool opt = true;
setsockopt(bcSocket, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char FAR *>(&opt), sizeof(opt));

//4.设置发往的地址
sockaddr_in addrbc;            //发往的地址    
memset(&addrbc, 0, sizeof(addrbc));
addrbc.sin_family = AF_INET;               //地址类型为internetwork
addrbc.sin_addr.s_addr = INADDR_BROADCAST; //设置ip为广播地址
addrbc.sin_port = htons(BROADCAST_PORT);             //端口号为

int nlen = sizeof(addrbc);
sendLen = mFrame.getFrameLen();
dataLen = sendLen;
BYTE *data = mFrame.getFrameBuffer();

if (!data)
{
    closesocket(bcSocket);
    printf("%s: !data  errCode:%d\n", __func__, errCode);
    return errCode;
}

while (dataLen > 1024) 
{
    if (sendto(bcSocket, (char *)&data[sendLen - dataLen], 1024, 0, reinterpret_cast<sockaddr*>(&addrbc),
        nlen) == SOCKET_ERROR)
    {
        errCode = WSAGetLastError();
        closesocket(bcSocket);
        printf("%s: sendto 11  errCode:%d\n", __func__, errCode);
        return errCode;
    }
    dataLen = dataLen - 1024;
}

if (dataLen > 0)
{
    if (sendto(bcSocket, (char *)&data[sendLen - dataLen], dataLen, 0, reinterpret_cast<sockaddr*>(&addrbc),
        nlen) == SOCKET_ERROR)
    {
        errCode = WSAGetLastError();
        closesocket(bcSocket);
        printf("%s: sendto 22  errCode:%d\n", __func__, errCode);

        return errCode;
    }

    dataLen = 0;
}

closesocket(bcSocket);
WSACleanup();
return 0;

}

int IBUdpDev::sendFrameData(IBFrame &mFrame, const char *mIp, int mPort)
{
int errCode = -1;;
DWORD sendLen = 0;
DWORD dataLen = 0;
errCode = WSAStartup(MAKEWORD(2, 2), &wasData);
if (errCode != 0)
{
return errCode;
}

SOCKET sendSocket= socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sendSocket == INVALID_SOCKET){
    errCode = WSAGetLastError();
    closesocket(sendSocket);

    return errCode;
}

//4.设置发往的地址
sockaddr_in addrsend;            //发往的地址  
memset(&addrsend, 0, sizeof(addrsend));
addrsend.sin_family = AF_INET;              //地址类型为internetwork
addrsend.sin_addr.s_addr = inet_addr(mIp);  //设置ip为
addrsend.sin_port = htons(mPort);               //端口号为
int nlen = sizeof(addrsend);
sendLen = mFrame.getFrameLen();
dataLen = sendLen;
BYTE *data = mFrame.getFrameBuffer();

if (!data)
{
    closesocket(sendSocket);
    printf("%s: !data  errCode:%d\n", __func__, errCode);
    return errCode;
}

if (dataLen > 0) 
{
    if (sendto(sendSocket, (char *)&data[sendLen - dataLen], dataLen, 0, reinterpret_cast<sockaddr*>(&addrsend),
        nlen) == SOCKET_ERROR)
    {
        errCode = WSAGetLastError();
        closesocket(sendSocket);
        printf("%s: sendto 22  errCode:%d\n", __func__, errCode);

        return errCode;
    }

    dataLen = 0;
}

closesocket(sendSocket);
WSACleanup();
return 0;

}

4个回答

广播使用的端口和单播使用的端口不一样

广播使用的端口和单播使用的端口要一致

qq_22701117
qq_22701117 为什么要使用相同的端口,如果使用相同的端口,那单播不也就需要设置成广播模式?
一年多之前 回复

建议用5000以后的端口

qq_22701117
qq_22701117 应该不是端口号的问题
一年多之前 回复

广播和单播使用的端口要相同

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!