linux下原始套接字打印问题
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#define BUFFER_MAX 2048
int main(int argc,char *argv[])
{
int sock,nread,proto;
char buff[BUFFER_MAX];
char *ethheader,*ipheader;
if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
{
fprintf(stdout, "create socket error\n");
exit(0);
}
while(1)
{
nread=recvfrom(sock,buff,2048,0,NULL,NULL);
printf("the num of packets %d",nread);
printf("whole pack %s",buff); //我想用这句打印出整个包的数据,为什么不行呢?
}
return 0;
}
谢谢哪位帮忙!
------解决方案--------------------呵呵,printf 打印字符串的哦,要求以 '\0' 结尾,貌似你收到网络数据包不一定符合这个要求,
所以,你应该循环打印每个字节,或按协议格式打印...
------解决方案--------------------nread是多少?你先在buff的尾部加个'\0'看看.