资源简介
发送TCP数据包
设计题目
本设计的目的是填充一个TCP数据包,并发送给目的主机。
1) 以命令行形式运行:SendTCP source_ip source_port dest_ip dest_port Data
其中SendTCP为程序名,source_ip、source_port、dest_ip和dest_port分别为源IP地址、目的IP地址、源端口和目的端口, Data为数据字段。
2)其他的TCP头部参数请自行设定。
3)数据字段为“This is my homework of network,I am very happy!”。
4)成功发送后在屏幕上输出“send OK”。

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib“ws2_32.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_RAWNULL0
WSA_FLAG_OVERLAPPED))==INVALID_SOCKET)
{
printf(“Socket Setup Error!\n“);
return false;
}
flag=true;
if(setsockopt(sockIPPROTO_IPIP_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 fal
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 77312 2004-01-01 01:42 SendTcp\SendTcp.doc
目录 0 2015-06-01 10:30 SendTcp\SendTcp\
目录 0 2015-06-01 10:30 SendTcp\SendTcp\Debug\
文件 176187 2010-12-22 12:33 SendTcp\SendTcp\Debug\SendTcp.exe
文件 184524 2010-12-22 12:33 SendTcp\SendTcp\Debug\SendTcp.ilk
文件 17290 2010-12-22 12:33 SendTcp\SendTcp\Debug\sendTcp.obj
文件 3727052 2010-12-22 12:33 SendTcp\SendTcp\Debug\SendTcp.pch
文件 353280 2010-12-22 12:33 SendTcp\SendTcp\Debug\SendTcp.pdb
文件 164864 2010-12-22 13:02 SendTcp\SendTcp\Debug\vc60.idb
文件 94208 2010-12-22 12:33 SendTcp\SendTcp\Debug\vc60.pdb
文件 6114 2010-12-22 12:32 SendTcp\SendTcp\sendTcp.cpp
文件 4296 2010-12-22 12:33 SendTcp\SendTcp\SendTcp.dsp
文件 522 2010-12-22 12:32 SendTcp\SendTcp\SendTcp.dsw
文件 41984 2010-12-22 13:02 SendTcp\SendTcp\SendTcp.ncb
文件 48640 2010-12-22 13:02 SendTcp\SendTcp\SendTcp.opt
文件 248 2010-12-22 13:02 SendTcp\SendTcp\SendTcp.plg
相关资源
- Modbus协议官方文档中、英文全
- Scratch源码
- E4A无障碍跨程序操作类库(带源码、
- 设备管理系统源码
- 安卓wifi直连app源码
- 我的世界源码(易语言版)
- Omron ETN21模块进行modbustcp通讯
- labview编程软件滤波器以及编写程序设
- 我的界面(visual foxpro)源码
- 易语言:一键cf基址源码
- The Secret Path 3D 3D魔方迷宫[源码][scra
- scratch垃圾分类源码(最终版本).sb
- 安卓QQ6.71协议源码易语言,qq协议源码
- 编译原理实验工具及参考源码(lex&
- E盾偷后台工具源码
- UNIX/LINUX编程实践教程的源码
- 十以内加减法练习 powerbuilder源码
- 农场开发项目
- OCR源码
- PLC上位机编程软件
- 用foobar2000听google音乐[更新一下]
- 学生信息管理系统源码
- 用VC 编写的仿QQ聊天室程序源代码
- 毕业论文之温度传感器DS18B20(源码
- 可自定义导航网站源码
- 栅栏填充算法源码(VC)
- msp430F149操作红外接收模块源码
- [免费]图像识别c 源码
- 周易排盘源码
- RSA算法源码
评论
共有 条评论