当前位置: 技术问答>linux和unix
tcpdump 抓不到包
来源: 互联网 发布时间:2017-05-25
本文导语: 小弟初学网络编程,想用tcpdump抓包看一下,但是死活抓不到,求大神指点 服务器代码 #include #include #include #include #include #include #include #include #define SERVER_PORT 8003 #define MSG_BUF_SIZE 128 main() { int sock...
小弟初学网络编程,想用tcpdump抓包看一下,但是死活抓不到,求大神指点
服务器代码
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8003
#define MSG_BUF_SIZE 128
main()
{
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int addr_len,numbytes;
char buffer[MSG_BUF_SIZE];
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
fprintf(stderr,"socket error");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVER_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
{
fprintf(stderr,"bind errorn");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if((numbytes = recvfrom(sockfd,buffer,MSG_BUF_SIZE,0,(struct sockaddr *)&their_addr,&addr_len)) == -1)
{
fprintf(stderr,"recvfrom error");
exit(1);
}
printf("got packet from %sn",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes longn",numbytes);
buffer[numbytes]='';
printf("packet contains "%s"n",buffer);
close(sockfd);
}
客户端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8003
int main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in their_addr;
struct hostent *hostname;
int num_bytes;
if(argc != 3)
{
fprintf(stderr,"usage:talker hostname messagen");
exit(1);
}
if((hostname = gethostbyname(argv[1])) == NULL)
{
fprintf(stderr,"gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
fprintf(stderr,"socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(SERVER_PORT);
their_addr.sin_addr = *((struct in_addr *)hostname->h_addr);
bzero(&(their_addr.sin_zero),8);
if((num_bytes = sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))) == -1)
{
fprintf(stderr,"sendto");
exit(1);
}
printf("sent %d bytes to %sn",num_bytes,inet_ntoa(their_addr.sin_addr));
close(sockfd);
return 0;
}
所用的命令是 tcpdump port 8003
程序正常运行,但是抓不到包
服务器代码
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8003
#define MSG_BUF_SIZE 128
main()
{
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int addr_len,numbytes;
char buffer[MSG_BUF_SIZE];
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
fprintf(stderr,"socket error");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVER_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
{
fprintf(stderr,"bind errorn");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if((numbytes = recvfrom(sockfd,buffer,MSG_BUF_SIZE,0,(struct sockaddr *)&their_addr,&addr_len)) == -1)
{
fprintf(stderr,"recvfrom error");
exit(1);
}
printf("got packet from %sn",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes longn",numbytes);
buffer[numbytes]='';
printf("packet contains "%s"n",buffer);
close(sockfd);
}
客户端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8003
int main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in their_addr;
struct hostent *hostname;
int num_bytes;
if(argc != 3)
{
fprintf(stderr,"usage:talker hostname messagen");
exit(1);
}
if((hostname = gethostbyname(argv[1])) == NULL)
{
fprintf(stderr,"gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
fprintf(stderr,"socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(SERVER_PORT);
their_addr.sin_addr = *((struct in_addr *)hostname->h_addr);
bzero(&(their_addr.sin_zero),8);
if((num_bytes = sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))) == -1)
{
fprintf(stderr,"sendto");
exit(1);
}
printf("sent %d bytes to %sn",num_bytes,inet_ntoa(their_addr.sin_addr));
close(sockfd);
return 0;
}
所用的命令是 tcpdump port 8003
程序正常运行,但是抓不到包
|
本地环回不跑以太网卡,tcpdump -i lo 抓本地环回的。