I have a C API that talks to hardware and I want to ultimately make calls to that API from a browser. Thinking some to the c++ servlet lib's are too complicated, as is JNI, I thought I could prove the concept like this:
A java servlet gets a simple string message via HTTP. The servlet opens a socket to pass along data via UDP. C process running opens a socket and awaits message. C process receives message, makes call, and send back simple string message via UDP. Servlet passes responce message back to browser.
In short,开发者_如何学JAVA can a servlet open a socket to talk to native (winsock) code?
Yes this would be a great solution.
However one thing you need to consider carefully is how concurrent requests need to be handled when 2 servlets make a connection. If the library/machine it might be useful to use a simple socket which does not accept other connections once a connection is made.
In the java you can then serialize access to the librarysocket, using a BlockingQueue for example, so that you have a nicely defined concurrent access behavior.
This will avoid spurius unrepeatable problems.
No reason why not. All you need to do is make TCP/UDP connection, and have your c++ server in place to respond to that.
A socket is a socket. Transport-layer protocols like UDP would be pointless if you could only use them to talk to an endpoint using the exact same implementation of the protocol.
The important part is making sure both endpoints agree on a protocol/data format for what you're transmitting.
精彩评论