我正在尝试编写一个Syslog监听器,到目前为止,它很好地通过TCP接受传入消息,但我也希望UDP能够运行.
这是我正在使用的UDP服务器代码,它使用python客户端应用程序.我还有另一个应用程序也可以使用python客户端应用程序.
# Server program # UDP VERSION from socket import * # Set the socket parameters host = "localhost" port = 514 buf = 1024 addr = (host,port) # Create socket and bind to address UDPSock = socket(AF_INET,SOCK_DGRAM) UDPSock.bind(addr) # Receive messages while 1: data,addr = UDPSock.recvfrom(buf) if not data: print "Client has exited!" break else: print "\nReceived message '", data,"'" # Close socket UDPSock.close()
使用此代码,我可以发送到服务器并让它显示代码.
# Client program from socket import * # Set the socket parameters host = "localhost" port = 514 buf = 1024 addr = (host,port) # Create socket UDPSock = socket(AF_INET,SOCK_DGRAM) def_msg = "===Enter message to send to server==="; print "\n",def_msg # Send messages while (1): data = raw_input('>> ') if not data: break else: if(UDPSock.sendto(data,addr)): print "Sending message '",data,"'....." # Close socket UDPSock.close()
我已经尝试过Kiwi Syslog Message Generator和Snare将系统日志消息发送到UDP服务器,但没有任何结果.有人可以帮我理解吗?