mirror of
https://github.com/zeromq/libzmq.git
synced 2025-01-31 14:39:55 +01:00
Merge pull request #647 from sebastien/master
Clarified zmq_socket.txt ZMQ_STREAM section, added HTTP server example
This commit is contained in:
commit
1f03710980
@ -347,12 +347,15 @@ routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error.
|
||||
To open a connection to a server, use the zmq_connect call, and then fetch the
|
||||
socket identity using the ZMQ_IDENTITY zmq_getsockopt call.
|
||||
|
||||
To close a specific client connection, as a server, send a zero-length message
|
||||
following the identity frame.
|
||||
To close a specific client connection, as a server, send the identity frame
|
||||
followed by a zero-length message (see EXAMPLE section).
|
||||
|
||||
The ZMQ_MSGMORE flag is ignored on data frames. You must send one identity frame
|
||||
followed by one data frame.
|
||||
|
||||
Also, please note that omitting the ZMQ_MSGMORE flag will prevent sending further
|
||||
data (from any client) on the same socket.
|
||||
|
||||
[horizontal]
|
||||
.Summary of ZMQ_STREAM characteristics
|
||||
Compatible peer sockets:: none.
|
||||
@ -381,6 +384,44 @@ The limit on the total number of open 0MQ sockets has been reached.
|
||||
*ETERM*::
|
||||
The context specified was terminated.
|
||||
|
||||
EXAMPLE
|
||||
-------
|
||||
.Creating a simple HTTP server using ZMQ_STREAM
|
||||
----
|
||||
void *ctx = zmq_ctx_new ();
|
||||
assert (ctx);
|
||||
/* Create ZMQ_STREAM socket */
|
||||
void *socket = zmq_socket (ctx, ZMQ_STREAM);
|
||||
assert (socket);
|
||||
int rc = zmq_bind (socket, "tcp://*:8080");
|
||||
assert (rc == 0);
|
||||
/* Data structure to hold the ZMQ_STREAM ID */
|
||||
uint8_t id [256];
|
||||
size_t id_size = 256;
|
||||
while (1) {
|
||||
/* Get HTTP request; ID frame and then request */
|
||||
id_size = zmq_recv (server, id, 256, 0);
|
||||
assert (id_size > 0);
|
||||
/* Prepares the response */
|
||||
char http_response [] =
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"\r\n"
|
||||
"Hello, World!";
|
||||
/* Sends the ID frame followed by the response */
|
||||
zmq_send (socket, id, id_size, ZMQ_SNDMORE);
|
||||
zmq_send (socket, http_response, strlen (http_response), ZMQ_SNDMORE);
|
||||
/* Closes the connection by sending the ID frame followed by a zero response */
|
||||
zmq_send (socket, id, id_size, ZMQ_SNDMORE);
|
||||
zmq_send (socket, 0, 0, ZMQ_SNDMORE);
|
||||
/* NOTE: If we don't use ZMQ_SNDMORE, then we won't be able to send more */
|
||||
/* message to any client */
|
||||
}
|
||||
zmq_close (socket);
|
||||
zmq_ctx_destroy (ctx);
|
||||
----
|
||||
|
||||
|
||||
SEE ALSO
|
||||
--------
|
||||
linkzmq:zmq_init[3]
|
||||
|
Loading…
x
Reference in New Issue
Block a user