qq_22701117
请叫我杨工
2017-12-20 09:42

socket UDP发送数据返回10013

20
  • udp
  • 数据
  • socket

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条回答