概述
亲爱的大家:
我使用基于python的套接字客户端发送字符串数据(即日志数据)。
另一方面,我使用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)
import socket, sys
host = sys.argv[1] #Server IP Address
textport = sys.argv[2] #Server Binding Port
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #socket
try:
port = int(textport)
except ValueError:
port = socket.getservbyname(textport, 'udp')
s.connect((host, port)) #connect
while(1):
print "Enter data to transmit:"
data = sys.stdin.readline().strip() #UDP data
s.sendall(data) #Send UDP data
服务器端(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] = '