• 大小: 98KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: C/C++
  • 标签:

资源简介

发送tcp数据包 C++程序实现。。。。。。

资源截图

代码片段和文件信息

#include  
#include  
#include  
#include 
#include 
#include 
#include 
//#pragma comment(lib“wsock32.lib“)

#define IPVER   4           //IP协议预定
#define MAX_BUFF_LEN 65500  //发送缓冲区最大值

typedef struct ip_hdr    //定义IP首部 

UCHAR h_verlen;            //4位首部长度4位IP版本号 
UCHAR tos;                //8位服务类型TOS 
USHORT total_len;        //16位总长度(字节) 
USHORT ident;            //16位标识 
USHORT frag_and_flags;    //3位标志位 
UCHAR ttl;                //8位生存时间 TTL 
UCHAR proto;            //8位协议 (TCP UDP 或其他) 
USHORT checksum;        //16位IP首部校验和 
ULONG sourceIP;            //32位源IP地址 
ULONG destIP;            //32位目的IP地址 
}IP_HEADER; 

typedef struct tsd_hdr //定义TCP伪首部 

ULONG saddr;    //源地址
ULONG daddr;    //目的地址 
UCHAR mbz;        //没用
UCHAR ptcl;        //协议类型 
USHORT tcpl;    //TCP长度 
}PSD_HEADER; 

typedef struct tcp_hdr //定义TCP首部 

USHORT th_sport;            //16位源端口 
USHORT th_dport;            //16位目的端口 
ULONG th_seq;                //32位序列号 
ULONG th_ack;                //32位确认号 
UCHAR th_lenres;            //4位首部长度/6位保留字 
UCHAR th_flag;                //6位标志位 
USHORT th_win;                //16位窗口大小 
USHORT th_sum;                //16位校验和 
USHORT th_urp;                //16位紧急数据偏移量 
}TCP_HEADER; 

//CheckSum:计算校验和的子函数 
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); 


int main(int argc char* argv[]) 

    WSADATA WSAData; 
    SOCKET sock; 
    
    IP_HEADER ipHeader; 
    TCP_HEADER tcpHeader; 
    PSD_HEADER psdHeader; 

    char Sendto_Buff[MAX_BUFF_LEN];  //发送缓冲区
    unsigned short check_Buff[MAX_BUFF_LEN]; //检验和缓冲区
    const char tcp_send_data[]={“This is my homework of networtI am happy!“};

    BOOL flag; 
    int rectnTimeOver; 

    if (argc!= 5) 
    {
        printf(“Useage: SendTcp soruce_ip source_port dest_ip dest_port \n“); 
        return false; 
    } 

    if (WSAStartup(MAKEWORD(22) &WSAData)!=0) 
    { 
        printf(“WSAStartup Error!\n“); 
        return false; 
    } 

    if ((sock=WSASocket(AF_INETSOCK_RAWIPPROTO_IPNULL0WSA_FLAG_OVERLAPPED))==INVALID_SOCKET) 
    { 
        printf(“Socket Setup Error!\n“); 
        return false; 
    } 
    flag=true; 
    if (setsockopt(sockIPPROTO_IP IP_HDRINCL(char *)&flagsizeof(flag))==SOCKET_ERROR) 
    { 
        printf(“setsockopt IP_HDRINCL error!\n“); 
        return false; 
    } 
    nTimeOver=1000; 
    if (setsockopt(sock SOL_SOCKET SO_SNDTIMEO (char*)&nTimeOver sizeof(nTimeOver))==SOCKET_ERROR) 
    { 
        printf(“setsockopt SO_SNDTIMEO error!\n“); 
        return false; 
    } 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      17011  2008-06-24 20:27  发送tcp数据包\tcp\Debug\net.obj

     文件      25600  2008-06-24 20:27  发送tcp数据包\tcp\Debug\tcp.pdb

     文件     148480  2008-06-24 20:27  发送tcp数据包\tcp\Debug\vc60.idb

     文件      77824  2008-06-24 20:27  发送tcp数据包\tcp\Debug\vc60.pdb

     文件       6080  2008-06-24 20:27  发送tcp数据包\tcp\net.cpp

     文件       3922  2008-06-24 22:30  发送tcp数据包\tcp\tcp.dsp

     文件        531  2008-06-24 15:27  发送tcp数据包\tcp\tcp.dsw

     文件      33792  2008-06-24 22:30  发送tcp数据包\tcp\tcp.ncb

     文件      48640  2008-06-24 22:30  发送tcp数据包\tcp\tcp.opt

     文件        913  2008-06-24 20:27  发送tcp数据包\tcp\tcp.plg

     文件     108032  2008-07-04 09:20  发送tcp数据包\发送tcp数据包.doc

     目录          0  2009-05-16 09:09  发送tcp数据包\tcp\Debug

     目录          0  2009-05-16 09:09  发送tcp数据包\tcp

     目录          0  2009-06-24 18:21  发送tcp数据包

----------- ---------  ---------- -----  ----

               470825                    14


评论

共有 条评论

相关资源