各位:
我使用基于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来捕获字符串数据。在
所以在第二次发生客户端的套接字错误时。在
有人能给我一些建议来克服这个问题吗?在
非常感谢你的帮助。在