unresolved external symbol __imp__ntohs@4

luozhiweim 2009-08-15 01:58:56
winpcap 分析数据包出现的如标题的错误请大家帮我解决一下~
我连接了两个库
#pragma comment(lib,“packet.lib”)
#pragma comment(lib,“wpcap.lib”)
包含了两个头文件
#include<pcap.h>
#include<remote-ext.h>
代码如下~书上范例代码~
typedef struct ip_address{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
typedef struct ip_header{
u_char ver_ihl;
u_char tos;
u_short tlen;
u_short identification;
u_short flags_fo;
u_char ttl;
u_char proto;
u_short crc;
ip_address saddr;
ip_address daddr;
u_int op_pad;
}ip_header;
typedef struct udp_header{
u_short sport;
u_short dport;
u_short len;
u_short crc;
}udp_header;
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %sn", errbuf);
exit(1);
}
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)n", d->description);
else
printf(" (No description available)n");
}

if(i==0)
{
printf("nNo interfaces found! Make sure WinPcap is installed.n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("nInterface number out of range.n");
pcap_freealldevs(alldevs);
return -1;
}
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
if ( (adhandle= pcap_open(d->name,
65536,
PCAP_OPENFLAG_PROMISCUOUS,
1000,
NULL,
errbuf ) ) == NULL)
{
fprintf(stderr,"nUnable to open the adapter. %s is not supported by WinPcapn");
pcap_freealldevs(alldevs);
return -1;
}
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,"nThis program works only on Ethernet networks.n");
pcap_freealldevs(alldevs);
return -1;
}
if(d->addresses != NULL)
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
netmask=0xffffff;
if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
{
fprintf(stderr,"nUnable to compile the packet filter. Check the syntax.n");
pcap_freealldevs(alldevs);
return -1;
}
if (pcap_setfilter(adhandle, &fcode)<0)
{
fprintf(stderr,"nError setting the filter.n");

pcap_freealldevs(alldevs);
return -1;
}
printf("nlistening on %s...n", d->description);
pcap_freealldevs(alldevs);
pcap_loop(adhandle, 0, packet_handler, NULL);

return 0;
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport,dport;
time_t local_tv_sec;
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
ih = (ip_header *) (pkt_data +
14);
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
sport = ntohs( uh->sport );
dport = ntohs( uh->dport );
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%dn",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
}
...全文
854 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgjxwl 2009-08-15
  • 打赏
  • 举报
回复
举个简单的例子。。。比如说有的时候。。。宏定义开关。。可以有选择的编译不同的代码。
比如说在不同的平台上。在这个平台上编译这段,而在另一个平台上编译另外一段。。
zgjxwl 2009-08-15
  • 打赏
  • 举报
回复
这个一般就是库没引入引起的啊。。。使用了库里的东西确没引入库。。。IDE默认没把你需要的库给包含进去。
luozhiweim 2009-08-15
  • 打赏
  • 举报
回复
顶起~
fallening 2009-08-15
  • 打赏
  • 举报
回复
呵呵,这种问题发生的很大一部分原因在于ide自动完成工具把compile和link的细节掩盖而产生的。
楼主如果是一个linux程序员,习惯从命令行下编译调试,不会出现这样的问题的。
superbtl 2009-08-15
  • 打赏
  • 举报
回复
这个好像就是工程的包含路径问题 是否加载头文件的源码
Jerry0916 2009-08-15
  • 打赏
  • 举报
回复
我在程序中试验了~ 不需要加此头文件了~ 谢谢~
那还有个问题~ 这样做的好处是?
/////////////////////////////////////////////////////////////////
答:这样做的好处是可以通过一个宏定义来定制(设置)功能的有或无,比如你给不同的客户做项目,有的客户需要某个功能,有的客户不需要,这样就可以使用同样的源代码,增加或不增加一个编译选项,然后就可以生成你所要的两个版本了,所以比较方便。
luozhiweim 2009-08-15
  • 打赏
  • 举报
回复
我在程序中试验了~ 不需要加此头文件了~ 谢谢~
那还有个问题~ 这样做的好处是?
luozhiweim 2009-08-15
  • 打赏
  • 举报
回复
谢谢这么晚~ 好像懂了~ 那还要添加 remote-ext.h头文件吗?
Jerry0916 2009-08-15
  • 打赏
  • 举报
回复
至于怎么进行代码操作,在编译器的编译选项增加HAVE_REMOTE,或者源代码中增加“#define HAVE_REMOTE”就OK了。

不知道有没有明白你的问题?有没有回答到你的问题。
Jerry0916 2009-08-15
  • 打赏
  • 举报
回复
HAVE_REMOTE这个宏定义是用来作为支不支持“远程捕获功能”的开关,把这个宏定义了,也就是把开关打开了,编译器就会把“远程捕获”相关的代码(用宏HAVE_REMOTE隔开的部分)给编译进来,新的程序也就有这个功能了。
luozhiweim 2009-08-15
  • 打赏
  • 举报
回复
我在winpcap中文技术档中看到这样一句话~
如果你的程序使用了WinPcap的远程捕获功能,那么在预处理定义中加入HAVE_REMOTE。不要直接把remote-ext.h直接加入到你的源文件中去。
能帮我解释一下为什么吗?和怎么进行代码操作~?
luozhiweim 2009-08-15
  • 打赏
  • 举报
回复
问题解决了~ 我在加入了一个
#pragma comment(lib,"ws2_32.lib")
错误没了~ 运行正常~

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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