| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3967 人关注过本帖
标题:socket函数调用错误10013
只看楼主 加入收藏
lyl930130
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:323
专家分:111
注 册:2013-5-13
结帖率:91.43%
收藏
 问题点数:0 回复次数:0 
socket函数调用错误10013
在到sockRaw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)这一步时,报错10013
上网查,解决的办法是用管理员运行,确实也可行。
但感觉这不是根本的办法,请教如何解决这个问题。

先谢谢

程序代码:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

#define WIN32_LEAN_AND_MEAN

#pragma comment(lib,"ws2_32.lib")

#define IP_RECORD_ROUTE  0x7

// IP header structure
typedef struct _iphdr 
{
    //Suppose the BYTE_ORDER is LITTLE_ENDIAN
    unsigned int   h_len:4;        // Length of the header
    unsigned int   version:4;      // Version of IP
    unsigned char  tos;            // Type of service
    unsigned short total_len;      // Total length of the packet
    unsigned short id;             // Unique identification
    unsigned short frag_offset;    // Fragment offset
    unsigned char  ttl;            // Time to live
    unsigned char  protocol;       // Protocol (TCP, UDP etc)
    unsigned short checksum;       // IP checksum
    unsigned int   sourceIP;       // Source IP
    unsigned int   destIP;         // Destination IP
} IpHeader;

#define ICMP_ECHO        8
#define ICMP_ECHOREPLY   0
#define ICMP_MIN         8 // Minimum 8-byte ICMP packet (header)

// ICMP header structure
// This is not the standard header, but we reserve space for time
typedef struct _icmphdr 
{
    BYTE   i_type;
    BYTE   i_code;                 // Type sub code
    USHORT i_cksum;
    USHORT i_id;
    USHORT i_seq;
    ULONG  timestamp;
} IcmpHeader;

// IP option header - use with socket option IP_OPTIONS
typedef struct _ipoptionhdr
{
    unsigned char code;        // Option type
    unsigned char len;         // Length of option hdr
    unsigned char ptr;         // Offset into options
    unsigned long addr[9];     // List of IP addrs
} IpOptionHeader;

#define DEF_PACKET_SIZE  32        // Default packet size
#define MAX_PACKET       1024      // Max ICMP packet size
#define MAX_IP_HDR_SIZE  60        // Max IP header size w/options


int   datasize,flag_web;
char *lpdest;

// Function: FillICMPData
// Description:
//    Helper function to fill in various fields for our ICMP request
void FillICMPData(char *icmp_data, int datasize)
{
    IcmpHeader *icmp_hdr = NULL;
    char *datapart = NULL;

    icmp_hdr = (IcmpHeader*)icmp_data;
    icmp_hdr->i_type = ICMP_ECHO; // Request an ICMP echo, type is 8
    icmp_hdr->i_code = 0;    // code is 0
    icmp_hdr->i_id = (USHORT)GetCurrentProcessId();
    icmp_hdr->i_cksum = 0;
    icmp_hdr->i_seq = 0;

    datapart = icmp_data + sizeof(IcmpHeader);
    // Place some junk in the buffer
    memset(datapart, 'E' , datasize - sizeof(IcmpHeader));
}


// Function: checksum
// Description:
//    This function calculates the 16-bit one's complement sum
//    of the supplied buffer (ICMP) header
USHORT checksum(USHORT *buffer, int size) 
{
    unsigned long cksum=0;
    while(size > 1) 
    {
        cksum += *buffer++;
        size -= sizeof(USHORT);
    }
    if(size) 
    {
        cksum += *(UCHAR*)buffer;
    }
    cksum = (cksum >> 16) + (cksum & 0xffff);
    cksum += (cksum >>16);
    return (USHORT)(~cksum);
}

// Function: DecodeIPOptions
// Description:
//    If the IP option header is present, find the IP options
//    within the IP header and print the record route option values
void DecodeIPOptions(char *buf, int bytes)
{
    IpOptionHeader *ipopt = NULL;
    IN_ADDR         inaddr;
    int             i;
    HOSTENT        *host = NULL;

    ipopt = (IpOptionHeader *)(buf + 20);

    printf("Record Route:   ");
    for(i = 0; i < (ipopt->ptr / 4) - 1; i++)
    {
        inaddr.S_un.S_addr = ipopt->addr[i];
        if (i != 0) printf("   ");
        host = gethostbyaddr((char *)&inaddr.S_un.S_addr, sizeof(inaddr.S_un.S_addr), AF_INET);
        if (host)
            printf("(%-15s) %s ", inet_ntoa(inaddr), host->h_name);
        else
            printf("(%-15s) ", inet_ntoa(inaddr));
    }
    printf("\n");
    return;
}

// Function: DecodeICMPHeader
// Description:
//    The response is an IP packet. We must decode the IP header to
//    locate the ICMP data.
void DecodeICMPHeader(char *buf, int bytes, struct sockaddr_in *from)
{
    IpHeader       *iphdr = NULL;
    IcmpHeader     *icmphdr = NULL;
    unsigned short  iphdrlen;
    DWORD           tick;
    static   int    icmpcount = 0;

    iphdr = (IpHeader *)buf;
    // Number of 32-bit words * 4 = bytes
    iphdrlen = iphdr->h_len * 4;
    tick = GetTickCount();

    if((iphdrlen == MAX_IP_HDR_SIZE) && (!icmpcount))
        DecodeIPOptions(buf, bytes);

    if(bytes < iphdrlen + ICMP_MIN) 
    {
        printf("Too few bytes from %s \n", inet_ntoa(from->sin_addr));
    }

    icmphdr = (IcmpHeader*)(buf + iphdrlen);
    if(icmphdr->i_type != ICMP_ECHOREPLY) // echo type must be 0
    {
        printf("non-echo type %d recvd \n", icmphdr->i_type);
        return;
    }
    // Make sure this is an ICMP reply to something we sent!
    // In FillICMPData function we assign the i_id is GetCurrentProcessId()
    if(icmphdr->i_id != (USHORT)GetCurrentProcessId()) 
    {
        printf("someone else's packet! \n");
        return ;
    }
    printf("%d bytes from %s ", bytes, inet_ntoa(from->sin_addr));
    printf("icmp_seq = %d. ", icmphdr->i_seq);
    printf("time: %d ms", tick - icmphdr->timestamp);
    printf("\n");
    flag_web=1;
    icmpcount++;
    return;
}
    
//
// Function: main
//
// Description:
//    Setup the ICMP raw socket, and create the ICMP header. Add
//    the appropriate IP option header, and start sending ICMP
//    echo requests to the endpoint. For each send and receive,
//    we set a timeout value so that we don t wait forever for a 
//    response in case the endpoint is not responding. When we
//    receive a packet decode it.

int ping()
{
    WSADATA            wsaData;
    SOCKET             sockRaw = INVALID_SOCKET;
    struct sockaddr_in dest, from;
    int                bread,
                       fromlen = sizeof(from),
                       timeout = 1000,
                       ret;
    char               icmp_data[MAX_PACKET],
                       recvbuf[MAX_PACKET];
    unsigned int       addr = 0;
    USHORT             seq_no = 0;
    struct hostent    *hp = NULL;
    IpOptionHeader     ipopt;

    if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("WSAStartup() failed: %d \n", GetLastError());
        return -1;
    }

    // Check the input arguments
    lpdest = "8.8.8.8";
    datasize = DEF_PACKET_SIZE;
    bool bBroadcast=TRUE;
    // Create a raw socket with IPPROTO_ICMP protocol
    sockRaw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

    if(sockRaw == INVALID_SOCKET) 
    {
        printf("WSASocket() failed: %d \n", WSAGetLastError());
        return -1;
    }
  
    // Set the send/recv timeout values
    timeout = 1000;
    bread = setsockopt(sockRaw, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
    if(bread == SOCKET_ERROR) 
    {
        printf("setsockopt(SO_RCVTIMEO) failed: %d \n", WSAGetLastError());
        return -1;
    }
  
    
    // Resolve the endpoint's name if necessary
    memset(&dest, 0, sizeof(dest));
    dest.sin_family = AF_INET;
    if((dest.sin_addr.s_addr = inet_addr(lpdest)) == INADDR_NONE)
    {
        if((hp = gethostbyname(lpdest)) != NULL)
        {
            memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);
            dest.sin_family = hp->h_addrtype;
            printf("dest.sin_addr = %s \n", inet_ntoa(dest.sin_addr));
        }
        else
        {
            printf("gethostbyname() failed: %d \n", WSAGetLastError());
            return -1;
        }
    }

    // Create the ICMP packet
    datasize += sizeof(IcmpHeader);
    memset(icmp_data,0,MAX_PACKET);
    // Fill the ICMP packet
    FillICMPData(icmp_data,datasize);

    // Start sending/receiving ICMP packets
    flag_web=0;
    while(1) 
    {
        static int nCount = 0;
        int bwrote;
                
        if(nCount++ == 10) break;
                
        ((IcmpHeader*)icmp_data)->i_cksum = 0;
        ((IcmpHeader*)icmp_data)->timestamp = GetTickCount();
        ((IcmpHeader*)icmp_data)->i_seq = seq_no++;
        ((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data, datasize);

        bwrote = sendto(sockRaw, icmp_data, datasize, 0, (struct sockaddr*)&dest, sizeof(dest));
        if(bwrote == SOCKET_ERROR)
        {
            if(WSAGetLastError() == WSAETIMEDOUT) 
            {
                printf("timed out \n");
                continue;
            }
            printf("sendto() failed: %d \n", WSAGetLastError());
            return -1;
        }
        if(bwrote < datasize) 
        {
            printf("Wrote %d bytes \n", bwrote);
        }
        bread = recvfrom(sockRaw, recvbuf, MAX_PACKET, 0, (struct sockaddr*)&from, &fromlen);
        if(bread == SOCKET_ERROR)
        {
            if(WSAGetLastError() == WSAETIMEDOUT) 
            {
                printf("timed out \n");
                continue;
            }
            printf("recvfrom() failed: %d \n", WSAGetLastError());
            return -1;
        }
        DecodeICMPHeader(recvbuf, bread, &from);
        Sleep(1000);
    }

    // Cleanup
    if (sockRaw != INVALID_SOCKET) 
        closesocket(sockRaw);
    WSACleanup();

    return flag_web;
}

int main()
{
    //0表示ping通,1表示ping不通,-1表示初始化过程失败
    printf("%d",ping());
    return 0;
}
搜索更多相关主题的帖子: 管理员 如何 
2016-03-10 22:03
快速回复:socket函数调用错误10013
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024229 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved