sinat_35128840
sinat_35128840
2016-06-13 14:12

这个程序 sendto 返回-1 错误编号 10038 是为什么啊 麻烦大家帮我看一看

#include
#include"ping.h"
#include
CInitSock initSock; // 链接初始化 winsock 库
USHORT checksum(USHORT* buff, int size)
{
unsigned long cksum = 0;

while (size > 1)
{
    cksum += *buff++;
    size -= sizeof(USHORT);
}

if (size)
{
    cksum += *(UCHAR*)buff;
}

// 将cksum的高16位和低16位相加,取反后得到校验和
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);

}

int main()
{
char szDestIp[] = "180.97.33.107";
SOCKET sRaw = ::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

SOCKADDR_IN dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(0);
dest.sin_addr.S_un.S_addr = inet_addr(szDestIp);

ECHOREQUEST echoReq;

echoReq.icmphdr.icmp_type = 8;
echoReq.icmphdr.icmp_code = 0;
echoReq.icmphdr.icmp_id = (USHORT)GetCurrentProcessId();
echoReq.icmphdr.icmp_checksum = 0;
echoReq.icmphdr.icmp_sequence = 0;

memset(&echoReq.cData, 'E', 32);

USHORT nSeq = 0;
SOCKADDR_IN from;
int nLen = sizeof(from);
while (TRUE)
{
    static int nCount = 0;
    int nRet;
    if (nCount++ == 4) break;

    echoReq.icmphdr.icmp_checksum = 0;

    echoReq.icmphdr.icmp_timestamp = GetTickCount();
    echoReq.icmphdr.icmp_sequence = nSeq++;
    echoReq.icmphdr.icmp_checksum =
        checksum((USHORT*)&echoReq, sizeof(echoReq));

    nRet = sendto(sRaw, (char*)&echoReq, sizeof(echoReq), 0,
        (SOCKADDR*)&dest, sizeof(dest));
    if (nRet == SOCKET_ERROR)
    {
        printf("sendto() failed:%d\n", WSAGetLastError());
        return -1;
    }

    ECHOREPLY echoReply;
    nRet = recvfrom(sRaw, (char*)&echoReply, sizeof(ECHOREPLY),
        0, (sockaddr*)&from, &nLen);
    if (nRet == SOCKET_ERROR)
    {
        if (WSAGetLastError() == WSAETIMEDOUT)
        {
            printf("time out\n");
            continue;
        }
        printf("recvfrom() failed:%d\n", WSAGetLastError());
        return -1;
    }

    if (nRet < sizeof(ECHOREPLY))
    {
        printf("Too few bytes from %s \n", inet_ntoa(from.sin_addr));
    }
    if (echoReply.echoRequest.icmphdr.icmp_type != 0)
    {
        printf("nonecho type %d recvd\n",
            echoReply.echoRequest.icmphdr.icmp_type);
        return -1;
    }
    if (echoReply.echoRequest.icmphdr.icmp_id != GetCurrentProcessId())
    {
        printf("someone else's packet!\n");
        return -1;
    }

    printf("%d bytes Reply from %s", nRet,
        inet_ntoa(from.sin_addr));
    printf("icmp_seq = %d.",
        echoReply.echoRequest.icmphdr.icmp_timestamp);

    printf("TTL=%d", echoReply.iphdr.ipTTL);

    printf("\n");
    Sleep(1000);
}




return 0;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答