python调用libpcap_libPcap接受的基于python的套接字程序(cbase)

各位:

我使用基于python的socket客户机来发送字符串数据(即日志数据)。在

另一方面,我使用libpcap在服务器端嗅探字符串数据。在

但是当我第二次向服务器端发送字符串数据时,客户端出现了一个错误

错误如下:Traceback (most recent call last):

File "./udp_client_not_sendback.py", line 21, in

s.sendall(data) #Send UDP data

File "/usr/lib/python2.7/socket.py", line 224, in meth

return getattr(self._sock,name)(*args)

socket.error: [Errno 111] Connection refused

下面是我在客户端和服务器端的代码:

客户端(Python)

^{pr2}$

服务器端(C libpcap)pcap_handler_func(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)

{

char timebuf[64];

char addrstr[64];

struct ether_header *ethhdr = (struct ether_header *)bytes;

struct iphdr *ipv4h;

struct ip6_hdr *ipv6h;

memset(timebuf, 0, sizeof(timebuf));

if (ctime_r(&h->ts.tv_sec, timebuf) == NULL) {

return;

}

timebuf[strlen(timebuf) - 1] = '\0';

printf("%s, caplen:%d, len:%d, ", timebuf, h->caplen, h->len);

ipv4h = (struct iphdr *)(bytes + sizeof(struct ether_header));

inet_ntop(AF_INET, &ipv4h->saddr, addrstr, sizeof(addrstr));

printf("src[%s]\n", addrstr);

return;

}

int main()

{

pcap_t *p;

char errbuf[PCAP_ERRBUF_SIZE];

char cmdstr[] = "udp";

struct bpf_program bpfprog;

p = pcap_open_live("eth1", 65536, 1, 10, errbuf);

//Filter

if (pcap_setfilter(p, &bpfprog) < 0) {

fprintf(stderr, "%s\n", pcap_geterr(p));

return 1;

}

//Packet action

if (pcap_loop(p, -1, pcap_handler_func, NULL) < 0) {

fprintf(stderr, "%s\n", pcap_geterr(p));

pcap_close(p);

return 1;

}

pcap_close(p);

return 0;

}

我想问题是我没有在服务器端绑定套接字,我只是使用pcap来捕获字符串数据。在

所以在第二次发生客户端的套接字错误时。在

有人能给我一些建议来克服这个问题吗?在

非常感谢你的帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值