Communicate between two java programs
I would like to create two Java programs, that communicate each other by messages. I know method client<->server<->client (using sockets) but I want to create client<->client communication (without server).开发者_如何学Go How could I do it?
If you have implemented a basic client / server architecture with sockets, then you can do the same thing. Each client is both a client and a server, and you have a more peer to peer type structure.
To set up the client<->client connection, one client can send a message to the server, which passes the message onto the other client. The message would include client ip, port, whatever is needed to connect directly to the first client.
This is a common method for file transfer in IM clients and the actual media stream in SIP. Be sure to watch out for firewall issues if the clients are on different networks - see STUN, TURN and ICE traversals.
You may be able to (re)use an existing Jabber (XMPP) implementation for this.
It depends on how you implement, you can use network protocol to communicate between programs such as HTTP (quite common).
For the message passing, there are different types of protocol as well, such as XML, JSON or a light-weighted Protocol Buffer, etc.
You can use a JXTA implementation for this purpose. It's not an easy API although.
精彩评论