云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 计算机网络课程设计报告

计算机网络课程设计报告

  • 62 次阅读
  • 3 次下载
  • 2025/5/4 13:21:19

nAccum <<= 1; nData <<= 1; } Table_CRC[i] = nAccum; } }

// 计算 32 位 CRC-32 值

unsigned long CRC_32( unsigned char * aData, unsigned long aSize ) { unsigned long i; unsigned long nAccum = 0; for ( i = 0; i < aSize; i++ ) nAccum = ( nAccum << 8 ) ^ Table_CRC[( nAccum >> 24 ) ^ *aData++]; return nAccum; }

void main() { unsigned char data[100]; unsigned long len(unsigned char *); printf(\ scanf(\ BuildTable16( nCRC_16 ); printf(\ BuildTable16( nCRC_CCITT ); printf(\ BuildTable32( nCRC_32 ); printf(\}

unsigned long len(unsigned char *str) { unsigned long len=0; while(*str++!='\\0') len++; return len; }

3、checksum

#include #include

#define IP_HDRINCL

typedef struct _IPHEADER {

UCHAR VerHeadLen; UCHAR TOS; USHORT Length;

USHORT ID; USHORT Flags; UCHAR TTL; UCHAR Protocol; USHORT Checksum; ULONG SourceIP; ULONG DestIP; }IPHEADER;

typedef struct _UDPHEADER {

USHORT SourcePort; USHORT DestPort; USHORT Length;

USHORT CheckSum; }UDPHEADER;

USHORT checksum(USHORT * buff, int size) {

ULONG cksum = 0; while (size > 1) {

cksum += *(buff++); size -= sizeof(USHORT); }

if(size) {

cksum += *((UCHAR*)buff); }

cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (USHORT)(~cksum); }

void UdpHeaderChecksum(IPHEADER *IpHeader,UDPHEADER payload,int payloadlen)

*UdpHeader,char *

{

char buf[1024]; char *ptr = buf; int chksumlen = 0; ULONG zero = 0;

memcpy(ptr,&IpHeader->SourceIP,sizeof(IpHeader->SourceIP)); ptr += sizeof(IpHeader->SourceIP);

chksumlen += sizeof(IpHeader->SourceIP);

memcpy(ptr,&IpHeader->DestIP,sizeof(IpHeader->DestIP)); ptr += sizeof(IpHeader->DestIP);

chksumlen += sizeof(IpHeader->DestIP); memcpy(ptr,&zero,1); ptr += 1;

chksumlen += 1;

memcpy(ptr,&IpHeader->Protocol,sizeof(IpHeader->Protocol)); ptr += sizeof(IpHeader->Protocol);

chksumlen += sizeof(IpHeader->Protocol);

memcpy(ptr,&UdpHeader->Length,sizeof(UdpHeader->Length)); ptr += sizeof(UdpHeader->Length);

chksumlen += sizeof(UdpHeader->Length);

memcpy(ptr,&UdpHeader->SourcePort,sizeof(UdpHeader->SourcePort)); ptr += sizeof(UdpHeader->SourcePort);

chksumlen += sizeof(UdpHeader->SourcePort);

memcpy(ptr,&UdpHeader->DestPort,sizeof(UdpHeader->DestPort)); ptr += sizeof(UdpHeader->DestPort);

chksumlen += sizeof(UdpHeader->DestPort);

memcpy(ptr,&UdpHeader->Length,sizeof(UdpHeader->Length)); ptr += sizeof(UdpHeader->Length);

chksumlen += sizeof(UdpHeader->Length);

memcpy(ptr,&zero,sizeof(UdpHeader->CheckSum)); ptr += sizeof(USHORT);

chksumlen += sizeof(USHORT); memcpy(ptr,payload,payloadlen); ptr += payloadlen;

chksumlen += payloadlen;

for(int i = 0; i < payloadlen %2; i++) {

*ptr = 0; ptr++;

chksumlen++; }

UdpHeader->CheckSum = checksum((USHORT*)buf,chksumlen); }

int main() {

int i = 0;

WSADATA wsaData;

int wsaret = WSAStartup(0x101,&wsaData); if(wsaret != 0) {

printf(\失败\\n\ }

char szDestIP[] = \ char szSourceIP[] = \ USHORT nDestPort = 1234; USHORT nSourcePort = 8888; char szMsg[] = \ int nMsgLen = strlen(szMsg);

SOCKET sRaw = ::socket(AF_INET,SOCK_RAW,IPPROTO_UDP); BOOL bIncl = TRUE;

i = ::setsockopt(sRaw,IPPROTO_IP,IP_HDRINCL,(char*)&bIncl,sizeof(bIncl)); char buff[1024] = {0};

int TotalLength = sizeof(IPHEADER) + sizeof(UDPHEADER) + nMsgLen; //IP头

IPHEADER * IpHeader = (IPHEADER*)buff;

IpHeader->VerHeadLen = (4 << 4 | (sizeof(_IPHEADER)/sizeof(ULONG))); IpHeader->Length = TotalLength; IpHeader->TTL = 128;

IpHeader->Protocol = IPPROTO_UDP;

IpHeader->SourceIP = ::inet_addr(szSourceIP); IpHeader->DestIP = ::inet_addr(szDestIP);

IpHeader->Checksum = checksum((USHORT*)IpHeader,sizeof(IPHEADER)); //UDP头

UDPHEADER * UdpHeader = (UDPHEADER*)(buff + sizeof(IPHEADER)); UdpHeader->SourcePort = htons(nSourcePort); UdpHeader->DestPort = htons(nDestPort);

UdpHeader->Length = htons(sizeof(UDPHEADER) + nMsgLen); UdpHeader->CheckSum = 0;

UdpHeaderChecksum(IpHeader,UdpHeader,szMsg,nMsgLen); char* pData = buff + sizeof(IPHEADER) + sizeof(UDPHEADER); memcpy(pData,szMsg,nMsgLen); SOCKADDR_IN Addr = {0}; Addr.sin_family = AF_INET; Addr.sin_port = htons(nDestPort); memset(Addr.sin_zero,0,8);

Addr.sin_addr.S_un.S_addr = ::inet_addr(szDestIP);

搜索更多关于: 计算机网络课程设计报告 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

nAccum <<= 1; nData <<= 1; } Table_CRC[i] = nAccum; } } // 计算 32 位 CRC-32 值 unsigned long CRC_32( unsigned char * aData, unsigned long aSize ) { unsigned long i; unsigned long nAccum = 0; for ( i = 0; i < aSize; i++ ) nAccum = ( nAccum <> 24 ) ^ *aData++]; return nAccum; } void main() { unsigned char data[100]; unsigned lon

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com