如何监听UDP并使用PHP
我必须建立一个系统来侦听来自 GPS 设备的请求,该设备只能发送 UDP 请求。如果可能的话,我希望通过 PHP 来分析请求。
我不知道从哪里开始。我需要什么?我可以使用 PHP 吗?使用PHP可靠吗?我可以调整 Apache 来监听 UDP 请求吗?
I have to build up a system which listens for requests from a GPS device which is only capable of sending UDP requests. Then I am going to analyze requests hopefully by PHP if it is possible.
I do not know where to start. What do I need? Can I make use of PHP? Would it be reliable to use PHP? Can I just adjust Apache for listening UDP requests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要总体程序设计的概念,并且正如 @karim79 指出的那样,了解您所选语言的套接字编程 API。
您是指 Apache
httpd
吗?简短的回答是“不”。请改用 PEAR 的
System_Daemon
之类的内容。长的答案是“是的,这是可能的”。特别是使用暴露
httpd
内部结构的模块化插件,您可以做任何您想做的事情 (tm)(例如,参见mod_perl
)。您可以将httpd
打入某种应用程序服务器,用于长期运行的(一组?)PHP 进程,这些进程本身并不是 HTTP 驱动的。两者更好的答案是“不”。 :)
You need a notion of an overall program design, and, as @karim79 pointed out, an understanding of socket programming APIs for your chosen language.
Do you mean the Apache
httpd
? The short answer is "no."Use something like PEAR's
System_Daemon
instead.The long answer is "yes, that is possible." Particularly with modular plugins exposing the
httpd
's internals, you can do Just About Anything You Want (tm) (see, for instance,mod_perl
). You could beathttpd
into a sort of application server for a long-running (set of?) PHP process(es) which are not themselves intrinsically HTTP-driven.The better answer of the two is, again, "no." :)
从各方面来看,我认为 PHP 应该可以胜任这一工作,但我自己还没有做过类似的事情。您需要研究套接字编程,这里有一个教程:
http://www.devshed.com/c/a/PHP/Socket-Programming-With-PHP/
By all accounts I think PHP should be fine for that, but I haven't done anything like that myself. You will need to look into socket programming, here's a tutorial:
http://www.devshed.com/c/a/PHP/Socket-Programming-With-PHP/
您可以让
inetd
调用您的 PHP 脚本。You could just have your PHP script invoked by
inetd
.好吧,PHP 支持一组 Socket 函数,允许您处理对于 UDP,我自己使用它们构建了一个 NSLookup 类,我可以指定名称服务器(全部在 UDP 中)和一个 Ping 类(RAW/ICMP)。它的工作方式与标准 C/C++ 套接字库类似。但我真的不认为使用 Apache+PHP 是构建这些东西的好选择。如果您想坚持使用 PHP,最好将其编写为控制台应用程序。
Well, PHP supports a set of Socket Functions that allow you to deal with UDP, I've used them myself to build a NSLookup class which I could specify the name server (all in UDP) and a Ping class (RAW/ICMP). It works just like the standard C/C++ socket library. But I don't really think using Apache+PHP is a good choice to build those things. If you want to stick with PHP, its better to script it as a console application.