tcpdump 抓不到包
小弟初学网络编程,想用tcpdump抓包看一下,但是死活抓不到,求大神指点
服务器代码
[code=C/C++][/code]
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#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 error\n");
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 %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buffer[numbytes]='\0';
printf("packet contains \"%s\"\n",buffer);
close(sockfd);
}
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>
#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 message\n");
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 %s\n",num_bytes,inet_ntoa(their_addr.sin_addr));
close(sockfd);
return 0;
}
所用的命令是 tcpdump port 8003
程序正常运行,但是抓不到包
------解决方案--------------------
本地环回不跑以太网卡,tcpdump -i lo 抓本地环回的。