鱼C论坛

 找回密码
 立即注册
查看: 1762|回复: 4

C++代碼 那位魚哥 幫忙註釋一下 謝謝

[复制链接]
发表于 2014-7-7 09:04:17 | 显示全部楼层 |阅读模式
10鱼币
#include <iostream.h>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib")
#define ICMP_ECHOREPLY 0
#define ICMP_ECHOREQ 8
#define REQ_DATASIZE 32
#define FD_SETSIZE 64
#define DESTPORT 7777
#define DESTIP "127.0.0.1"
int WaitForEchoReply(SOCKET s);
int SendEchoRequest(SOCKET s, LPSOCKADDR_IN lpstToAddr);

typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;
typedef struct tagIPHDR
{
    unsigned char   VIHL;
    unsigned char   TOS;
    short           TotLen;
    short           ID;
    short           FlagOff;
    unsigned char   TTL;
    unsigned char   Protocol;
    unsigned short  Checksum;
    struct in_addr iaSrc;
    struct in_addr  iaDst;
}IPHDR,*PIPHDR;
typedef struct tagICMPHDR
{
    u_char Type;
    u_char Code;
    u_short Checksum;
    u_short ID;
    u_short Seq;
    char Data;
}ICMPHDR,*PICMPHDR;
typedef struct tagECHOREQUEST
{
    ICMPHDR icmpHdr;
    DWORD dwTime;
    char cData[REQ_DATASIZE];
}ECHOREQUEST,*PECHOREQUEST;
typedef struct tagECHOREPLY
{
    IPHDR ipHdr;
    ECHOREQUEST echoRequest;
    char cFiller[256];
}ECHOREPLY,*PECHOREPLY;
void main()
{
    WSADATA wsaData;
   
    int error = WSAStartup(MAKEWORD(2, 1), &wsaData);
    if (error !=0)
    {
        cout << "wsastartup error" << endl;
        return;
    }
   
/*
    SOCKET s = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
    if (s == INVALID_SOCKET)
    {
        cout << "Invalid socket " << endl;
        WSACleanup();
        return;
    }
*/
    SOCKET s = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,
                           WSA_FLAG_OVERLAPPED);
    if (s == INVALID_SOCKET)
    {
        cout << "Invalid socket " << endl;
        WSACleanup();
        return;
    }
    struct sockaddr_in dest;
    dest.sin_family = AF_INET;
    dest.sin_port = htons(7777);
    dest.sin_addr.s_addr = inet_addr("127.0.0.1");
    for (int i=0; i<5; i++)
    {
        cout << sizeof(ECHOREQUEST) << endl;
        
        int size = SendEchoRequest(s,&dest);
         cout << size << endl;
        cout << sizeof(ICMPHDR) << endl;
        
        int nRet = WaitForEchoReply(s);
        cout << nRet << endl;
    }
}
int SendEchoRequest(SOCKET s, LPSOCKADDR_IN lpstToAddr)
{
    static ECHOREQUEST echoReq;
    static nId=1;
    static nSeq =1;
    int nRet1;
    echoReq.icmpHdr.Type=ICMP_ECHOREQ;
    echoReq.icmpHdr.Code=0;
    echoReq.icmpHdr.Checksum=0;
    echoReq.icmpHdr.ID=nId++;
    echoReq.icmpHdr.Seq=nSeq++;

    for(nRet1=0;nRet1<REQ_DATASIZE;nRet1++)
        echoReq.cData[nRet1]=' '+nRet1;
    echoReq.dwTime=GetTickCount();
    nRet1=sendto(s,
       (LPSTR)&echoReq,
          sizeof(ECHOREQUEST),
         0,
        (SOCKADDR*)lpstToAddr,
        sizeof(SOCKADDR_IN));
    if(nRet1==SOCKET_ERROR)
        cout << "sendto() error" << endl;
    return nRet1;
}
int WaitForEchoReply(SOCKET s)
{
  struct timeval Timeout;
  fd_set readfds;
  Timeout.tv_sec=1;
  Timeout.tv_usec=0;
  FD_ZERO(&readfds);
  FD_SET(s,&readfds);
  return select(1,&readfds,NULL,NULL,&Timeout);
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-7-7 10:31:37 | 显示全部楼层
←_←  你可以的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-7-8 19:30:26 | 显示全部楼层
.....对于抓包的这些代码,表示不会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-7-10 15:20:53 | 显示全部楼层

版主 能給個聯繫QQ不?教教我 原始套接字 方面的知識 好不好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-8-6 10:26:17 | 显示全部楼层
:big
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-26 05:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表