简体   繁体   中英

Ways to listen a UDP port with mozilla firefox

In a nutshell, I am trying to write a C++ XPCOM component which listens on a UDP port and calls a callback function (a javascript function) every time a UDP packet arrives.

Sorry if the answer is too obvious but I was wondering what are the ways to listen on a UDP port with Mozilla (Preferably something easy to do)?

I know that there is an interface called nsIServerSocket which allows some listeners to be attached to it, but this is only for opening TCP ports. Is there any UDP equivalent of this (where I can attach a listener which is notified every time a UDP packet arrives)?

I also know that I could probably use PR_OpenUDPSocket and such. Is there a way of using this without dealing with threads? (As far as I understand I have to return to the calling javascript function after opening the port).

Thanks.

Why do you need to restrict yourself to Mozilla's API if you are writing C++ code? You can use the POSIX socket API directly, see How to set up a Winsock UDP socket? for a WinSock example (the only difference for Linux and OS X should be that WSAStartup() call is unnecessary). nsIServerSocket is mainly useful for JavaScript code that doesn't have the option to use the system libraries directly.

If you prefer a straight answer to your question: no, there is no XPCOM API to create UDP sockets. You already found the NSPR API ( PR_OpenUDPSocket() ) but it is probably designed with DNS communication in mind since that's pretty much the only UDP communication a browser would do. Don't expect much here.

And a side-note: you might want to avoid binary XPCOM components and create a native library that can be called via js-ctypes instead. See here for the details .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM