Zeromq/jzmq req/REP (C/S) mode and Java code example

Source: Internet
Author: User

The C/S mode is in the response state and cannot send multiple data at the same time.

Reference: http://blog.csdn.net/kaka11/article/details/6614479

1. Create CTX context objects. The objects will create io_thread and start them. The reactor model is used as poller for continuous polling. Inter-thread communication uses mailbox for communication, while mailbox is essentially a two-way socketpair.

2. Create a zmq_socket object. If it is a client, connect it with connector. If it is a server, use listener to listen.

3. The connetor or listener will create the object except zmq_init_t, Which is exchanged as identity.

4. After the above Initialization is completed, the corresponding session_t object will be created and associated with the corresponding read/write pipeline, plugin zmq_engine for real message data reading and writing.

5. Message data is read and written asynchronously, that is, calling zmq: zmq_send () and zmq: zmq_recv () only writes the message data to the corresponding pipeline. Session_t reads and writes the message data from the pipeline when poller polls the corresponding read/write events.

Let's not talk about the Code:

Server:

Package COM. zeromq. test. CS; import Org. zeromq. zmq;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 7:32:34 */public class hwserver {// Hello World server written in Java // Interprocess Communication IPC: // 11111, cross-host communication using TCP: // 10.5.0.170: 26666 // when receiving a message, rep will return a message // use req mode for inter-host communication connection: TCP: // localhost: 5555 // send "hello" to the server, receive "world" public static void main (string [] ARGs) {// create a context and initialize an io_thread // create one (maximum number of sockets + number of Io threads + 3) slots pointer array //. each socket object has its own mailbox. // B. each io_thread object also has its own mailbox. // C. the other three are the zmq_term thread, Reaper thread, and log thread mailbox. // corresponding zermq socket, such as req, rep, pair, push, pull... in this example, create the rep type // bind to the port and connect zmq in io_thread. context context = zmq. context (1); zmq. socket socket = context. socket (zmq. rep); socket. BIND ("TCP: // *: 5555"); While (true) {byte [] request; // wait for the next client request // wait for a string ending with 0 // ignore the last 0 print request = socket. recv (0); system. out. println ("received request: [" + new string (request, 0, request. length-1) + "]"); try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();} // returns a string whose last digit is 0 to the client string replystring = "world" + ""; byte [] Reply = replystring. getbytes (); reply [reply. length-1] = 0; socket. send (reply, zmq. noblock );}}}

Client:

Package COM. zeromq. test. CS; import Org. zeromq. zmq;/*** @ author Lu Gui strong * @ email larry.lv.word@gmail.com * @ version Creation Time: 7:07:19 */public class hwclient {// Hello World client written in Java // after sending the message, you must receive a response message before sending a new message. // Use req mode for inter-host communication and connection: TCP: // localhost: 5555 // send "hello" to the server, receive "world" public static void main (string [] ARGs) {zmq. context context = zmq. context (1); zmq. socket socket = context. socket (zmq. REQ); socket. connect ("TCP: // localhost: 5555"); For (INT request_nbr = 0; request_nbr <= 10; request_nbr ++) {// create a string whose end is 0 string requeststring = "hello" + ""; byte [] request = requeststring. getbytes (); request [request. length-1] = 0; // send socket. send (request, zmq. noblock); // obtain the returned value byte [] Reply = socket. recv (0); // The output removes the string system at the end of 0. out. println ("replicated ed reply" + request_nbr + ": [" + new string (reply, 0, reply. length-1) + "]") ;}}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.