求TcpIp报文头和报文体的一个实例

ccll001 2009-08-01 09:32:09
如题,且客户端发送数据后马上得到服务端数据
有经验的兄弟们指点一下,谢谢
...全文
494 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yulitian 2009-08-02
  • 打赏
  • 举报
回复
需要完整类代码的话~~~~~
懒得写了
自己去下载这个吧

我前年写的一本内部教材
http://download.csdn.net/source/1463925

免积分下载,你要的内容在第六章,自己慢慢看吧!
yulitian 2009-08-02
  • 打赏
  • 举报
回复
厄,TCP包还是IP包?


IP头是这个:

public class ipHeader
{
//4位首部长度+4位IP版本号
public Byte ip_verlen;
//8位服务类型TOS
public Byte ip_tos;
//16位数据包总长度(字节)
public UInt16 ip_totallength;
//16位标识
public UInt16 ip_id;
//3位标志位
public UInt16 ip_offset;

//8位生存时间 TTL
public Byte ip_ttl;
//8位协议(TCP, UDP, ICMP, Etc.) (1->ICMP, 2->IGMP, 6->TCP, 17->UDP)
public Byte ip_protocol;
//16位IP首部校验和
public UInt16 ip_checksum;
//32位源IP地址
public uint ip_srcaddr;
//32位目的IP地址
public uint ip_destaddr;
//其他协议数据包
public Byte[] dates;

/// <summary>
/// 构造函数,通过参数提供包头各部分信息
/// </summary>
/// <param name="ipverlen"></param>
/// <param name="iptos"></param>
/// <param name="iptotallength"></param>
/// <param name="ipid"></param>
/// <param name="ipoffset"></param>
/// <param name="ipttl"></param>
/// <param name="ipprotocol"></param>
/// <param name="ipchecksum"></param>
/// <param name="ipsrcaddr"></param>
/// <param name="ipdestaddr"></param>
public ipHeader(Byte ipverlen, Byte iptos, UInt16 iptotallength, UInt16 ipid, UInt16 ipoffset, Byte ipttl, Byte ipprotocol, UInt16 ipchecksum, uint ipsrcaddr, uint ipdestaddr)
{
ip_verlen = ipverlen;
ip_tos = iptos;
ip_totallength = iptotallength;
ip_id = ipid;
ip_offset = ipoffset;
ip_ttl = ipttl;
ip_protocol = ipprotocol;
ip_checksum = ipchecksum;
ip_srcaddr = ipsrcaddr;
ip_destaddr = ipdestaddr;

}

/// <summary>
/// 获取头部校验和
/// </summary>
/// <param name="buffer"></param>
/// <param name="size"></param>
/// <returns></returns>
public UInt16 checksum_get(byte[] buffer, int size)
{
int Cksum_buffer_length = (int)Math.Ceiling(((Double)size) / 2);
UInt16[] Cksum_buffer = new UInt16[Cksum_buffer_length];
int Icmp_header_buffer_index = 0;
for (int I = 0; I < Cksum_buffer_length; I++)
{
//将两个byte转化为一个uint16
Cksum_buffer[I] = (UInt16)BitConverter.ToInt16(buffer, Icmp_header_buffer_index);
Icmp_header_buffer_index += 2;
}

int cksum = 0;
for (int i = 0; i < Cksum_buffer.Length; i++)
{
cksum += (int)buffer[i];
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);

return (UInt16)(~cksum);

}

//计算包长度
public int CountByte(ref Byte[] buffer)
{
Byte[] _ip_verlen = new Byte[1] { ip_verlen };

Byte[] _ip_tos = new Byte[1] { ip_tos };

Byte[] _ip_totallength = BitConverter.GetBytes(ip_totallength);

Byte[] _ip_id = BitConverter.GetBytes(ip_id);

Byte[] _ip_offset = BitConverter.GetBytes(ip_offset);

Byte[] _ip_ttl = new Byte[1] { ip_ttl };

Byte[] _ip_protocol = new Byte[1] { ip_protocol };

Byte[] _ip_checksum = BitConverter.GetBytes(ip_checksum);

Byte[] _ip_srcaddr = BitConverter.GetBytes(ip_srcaddr);

Byte[] _ip_destaddr = BitConverter.GetBytes(ip_destaddr);

int i = 0;
Array.Copy(_ip_verlen, 0, buffer, i, _ip_verlen.Length);
i += _ip_verlen.Length;
Array.Copy(_ip_tos, 0, buffer, i, _ip_tos.Length);
i += _ip_tos.Length;
Array.Copy(_ip_totallength, 0, buffer, i, _ip_totallength.Length);
i += _ip_totallength.Length;
Array.Copy(_ip_id, 0, buffer, i, _ip_id.Length);
i += _ip_id.Length;
Array.Copy(_ip_offset, 0, buffer, i, _ip_offset.Length);
i += _ip_offset.Length;
Array.Copy(_ip_ttl, 0, buffer, i, _ip_ttl.Length);
i += _ip_ttl.Length;
Array.Copy(_ip_protocol, 0, buffer, i, _ip_protocol.Length);
i += _ip_protocol.Length;
Array.Copy(_ip_checksum, 0, buffer, i, _ip_checksum.Length);
i += _ip_checksum.Length;
Array.Copy(_ip_srcaddr, 0, buffer, i, _ip_srcaddr.Length);
i += _ip_srcaddr.Length;
Array.Copy(_ip_destaddr, 0, buffer, i, _ip_destaddr.Length);
i += _ip_destaddr.Length;
Array.Copy(dates, 0, buffer, i, dates.Length);
i += dates.Length;

return i;

}
}
ccll001 2009-08-01
  • 打赏
  • 举报
回复
顶,没人知道吗?

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧