Pub:
Package COM. zeromq. test. psen_pub_sub; import Org. zeromq. zmq;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 5:56:05 */public class psenvpub {public static void main (string [] ARGs) throws interruptedexception {zmq. context context = zmq. context (1); zmq. socket publisher = context. socket (zmq. pub); Publisher. BIND ("TCP: // *: 5563"); While (true) {// write two messages in the form of an envelope-content publisher. send ( "A". getbytes (), zmq. sndmore); Publisher. Send ("OK! ". Getbytes (), 0); thread. Sleep (1000 );}}}
Sub:
Package COM. zeromq. test. psen_pub_sub; import Org. zeromq. zmq;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 6:02:30 */public class psenvsub {public static void main (string [] ARGs) {zmq. context context = zmq. context (1); zmq. socket subscriber = context. socket (zmq. sub); subscriber. connect ("TCP: // localhost: 5563"); subscriber. subscribe ("We wowould like to see this ". getbytes (); While (true) {// Read Envelope content // read content string address = new string (subscriber. recv (0); string contents = new string (subscriber. recv (0); system. out. println (address + ":" + contents );}}}