计算机的源端口号是你根据,网络协议的判定是根据源端口还是目的端口?
#include #pragma comment(lib,"ws2_32.lib") /*链接api相关连的ws2_32.lib静态库*/
#define max_hostname_lan 255
#define sio_rcvall _wsaiow(ioc_vendor,1)
#define max_addr_len 16
struct ipheader {
unsigned char ip_hl:4; /*header length(报头长度)*/
unsigned char ip_v:4; /*version(版本)*/
unsigned char ip_tos; /*type os service服务类型*/
unsigned short int ip_len; /*total length (总长度)*/
unsigned short int ip_id; /*identification (标识符)*/
unsigned short int ip_off; /*fragment offset field(段移位域)*/
unsigned char ip_ttl; /*time to live (生存时间)*/
unsigned char ip_p; /*protocol(协议)*/
unsigned short int ip_sum; /*checksum(校验和)*/
unsigned int ip_src; /*source address(源地址)*/
unsigned int ip_dst; /*destination address(目的地址)*/
}; /* total ip header length: 20 bytes (=160 bits) */
typedef struct tcpheader {
unsigned short int sport; /*source port (源端口号)*/
unsigned short int dport; /*destination port(目的端口号)*/
unsigned int th_seq; /*sequence number(包的序列号)*/
unsigned int th_ack; /*acknowledgement number(确认应答号)*/
unsigned char th_x:4; /*unused(未使用)*/
unsigned char th_off:4; /*data offset(数据偏移量)*/
unsigned char flags; /*标志全*/
unsigned short int th_win; /*windows(窗口)*/
unsigned short int th_sum; /*checksum(校验和)*/
unsigned short int th_urp; /*urgent pointer(紧急指针)*/
}tcp_hdr;
typedef struct udphdr {
unsigned short sport; /*source port(源端口号)*/
unsigned short dport; /*destination port(目的端口号)*/
unsigned short len; /*udp length(udp长度)*/
unsigned short cksum; /*udp checksum(udp校验和)*/
}udp_hdr;
void main(){
socket sock;
wsadata wsd;
dword dwbytesret;
unsigned int optval = 1;
unsigned char *dataudp,*datatcp;
int i,pcount=0,lentcp, lenudp;
sockaddr_in sa,sasource, sadest;
struct hostent far * phostent;
char far name[max_hostname_lan];
char szsourceip[max_addr_len], szdestip[max_addr_len],recvbuf[65535] = {0};
struct udphdr *pudpheader;
struct ipheader *pipheader;
struct tcpheader *ptcpheader;
wsastartup(makeword(2,1),&wsd);
if ((sock = socket(af_inet, sock_raw, ipproto_ip))==socket_error)
exit(1);
gethostname(name, max_hostname_lan);
phostent = gethostbyname(name);
sa.sin_family = af_inet;
sa.sin_port = htons(6000);
memcpy(&sa.sin_addr.s_un.s_addr, phostent->h_addr_list[0], phostent->h_length);
bind(sock, (sockaddr *)&sa, sizeof(sa)); /*bind()设定自己主机的ip地址和端口号*/
if ((wsagetlasterror())==10013)
exit(1);
wsaioctl(sock, sio_rcvall, &optval, sizeof(optval), null, 0, &dwbytesret, null, null);
pipheader = (struct ipheader *)recvbuf;
ptcpheader = (struct tcpheader *)(recvbuf+ sizeof(struct ipheader ));
pudpheader = (struct udphdr *) (recvbuf+ sizeof(struct ipheader ));
while (1){
memset(recvbuf, 0, sizeof(recvbuf));
recv(sock, recvbuf, sizeof(recvbuf), 0);
sasource.sin_addr.s_addr = pipheader->ip_src;
strncpy(szsourceip, inet_ntoa(sasource.sin_addr), max_addr_len);
sadest.sin_addr.s_addr = pipheader->ip_dst;
strncpy(szdestip, inet_ntoa(sadest.sin_addr), max_addr_len);
lentcp =(ntohs(pipheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader)));
lenudp =(ntohs(pipheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr)));
if((pipheader->ip_p)==ipproto_tcp&&lentcp!=0){
printf("*******************************************\n");
pcount++;
datatcp=(unsigned char *) recvbuf+sizeof(struct ipheader)+sizeof(struct tcpheader);
printf("-tcp-\n");
printf("\n目的ip地址:%s\n",szdestip);
printf("\n目的端口:%i\n",ntohs(ptcpheader->dport));
printf("datatcp address->%x\n",datatcp);
printf("size of ipheader->%i\n",sizeof(struct ipheader));
printf("size of tcpheader->%i\n",sizeof(struct tcpheader));
printf("size of the hole packet->%i\n",ntohs(pipheader->ip_len));
printf("\nchar packet%i [%i]=\"",pcount,lentcp-1);
for (i=0;i=20)
printf("%c",*(datatcp+i));
else
printf(".");
}
printf("\n\n*******************************************\n");
}
if((pipheader->ip_p)==ipproto_udp&&lentcp!=0){
pcount++;
dataudp=(unsigned char *) recvbuf+sizeof(struct ipheader)+sizeof(struct udphdr);
printf("-udp-\n");
printf("\n目的ip地址:%s\n",szdestip);
printf("\n目的端口:%d\n",ntohs(ptcpheader->dport));
printf("udp数据地址:%x\n",dataudp);
printf("ip头部长度:%i\n",sizeof(struct ipheader));
printf("udp头部长度:%i\n",sizeof(struct udphdr));
printf("包的大小:%i\n",ntohs(pipheader->ip_len));
printf("\nchar packet%i [%i]=\"",pcount,lenudp-1);
for (i=0;i=20)
printf("%c",*(dataudp+i));
else
printf(".");
}
printf("\n\n*******************************************\n");
}
}
}
计算机的源端口号是你根据,网络协议的判定是根据源端口还是目的端口?相关推荐
- ldap协议端口号_什么是LDAP协议端口号? 比较LDAP端口389和636
ldap协议端口号 Lightweight Directory Access Protocol or LDAP is used to authenticate and authorize users. ...
- 指定ip地址和端口号登录mysql数据库_防火墙规则,指定ip访问mysql数据库3306端口...
防火墙规则,指定ip访问mysql数据库3306端口 2019年12月08日 阅读数:6 这篇文章主要向大家介绍防火墙规则,指定ip访问mysql数据库3306端口,主要内容包括基础应用.实用技巧.原 ...
- 【网络篇】第三篇——源端口号和目的端口号
端口号的定义 理解源端口号和目的端口号 PORT VS PID 端口号如何确定? 端口号于协议 认识TCP协议和UDP协议 端口号的定义 数据链路层的地址是MAC地址,用来识别同一链路中不同的计算机 ...
- 计算机 - 知名端口号和注册端口号 - 收集与查询
1.应用场景 主要用于计算机 - 知名端口号和注册端口号 - 收集与查询. 2.学习/操作 1.文档阅读 Service Name and Transport Protocol Port Number ...
- 计算机常用默认端口号 总结
端口 端口是传输层服务访问点 (TSAP), 数据链路层的SAP是MAC地址,网络层的SAP是IP地址,传输层则是端口. 端口号长度16bit,可以表示65536个不同端口号.服务 ...
- 你所不知道的端口号知识!
端口号---具有网络功能的应用软件的标识号.注意,端口号是不固定的,即可以由用户手工可以分配(当然,一般在软件编写时就已经定义).当然,有很多应用软件有公认的默认的端口,比如FTP:20和21,HTT ...
- linux端口访问规则,linux 查看开发放端口号--iptables
一.查看哪些端口被打开 netstat -anp 二.关闭端口号: # iptables -A INPUT -p tcp --drop 端口号 -j DROP //不带协议的 # iptables - ...
- 端口号是什么以及常见端口号
端口号---具有网络功能的应用软件的标识号.注意,端口号是不固定的,即可以由用户手工可以分配(当然,一般在软件编写时就已经定义).当然,有很多应用软件有公认的默认的端口,比如FTP:20和21,HTT ...
- 萌新扫盲9—端口号的故事
在网络技术中,端口(Port)包括逻辑端口和物理端口两种类型. 物理端口指的是物理存在的端口,如ADSL Modem.集线器.交换机.路由器上用 于连接其他网络设备的接口,如RJ-45端口.SC端口等 ...
- 计算机网络 之 TCP和UDP的端口号解析
前言:今天了解一下tcp和udp报文的端口.发现一直以来都只是知道端口用于区分同一IP的服务器的不同服务,已经端口的大小.在查找traceroute的资料的时候,才了解到一些之前没注意到的东西. (一 ...
最新文章
- mysql防止从节点可写数据_mysql 主从数据不一致 Slave_SQL_Running: No 解决方法
- html input标签 alt和title 比较
- boost::log模块测试样板,用于检查每个公共标头是否都是独立的并且没有任何缺失的 #includes
- 自动undo管理下如何添加和删除回滚段
- AWS load balancer
- 光端机与光电转换器的区别介绍
- python 最快 因式分解_Python实现的对一个数进行因式分解操作示例
- dvd打开显示服务器失败,9g、10g和11G PowerEdge服务器未启动的e1000故障安全错误
- Eclipse中的SVN插件
- 计算机闹铃音乐是什么歌,适合当闹铃的歌曲
- 双系统linux开机黑屏,解决双系统中ubuntu开关机异常,黑屏,出现“nouveau , SCHED_ERROR”字样等的问题...
- 为什么中国的大学,不搞单人宿舍?
- 自动擦窗机器人作文_清洁机器人作文(六篇)
- jsd2205-csmall-passport(Day13)
- Windows Moblie上的网络连接
- 周金涛生前20篇雄文精华,一文尽览
- 《程序是怎么跑起来的》第五章学习笔记
- Spring事务的传播机制
- SRCNN超分辨率Pytorch实现,代码逐行讲解,附源码
- 《编程之美》相关参考资料
热门文章
- Scratch游戏设计——愤怒的小鸟
- python单例模式有什么用_python单例模式是什么
- Default clip could not be found in attached animations list...must be marked as legacy解决办法
- 知道PDF密码,想要移除如何操作?
- c语言的链表ppt,C语言链表详解ppt.ppt
- 谷歌浏览器利用开发者工具截取长图
- 微信小程序点播音频服务器,微信小程序无法播放本地音频
- 解决ubuntu与virtualbox共享文件夹访问权限问题
- 3d打印英语文献_万华推出可3D打印PP粉末材料
- 遍历上三角或者下三角