Commit 1f037109 authored by Pieter Hintjens's avatar Pieter Hintjens

Merge pull request #647 from sebastien/master

Clarified zmq_socket.txt ZMQ_STREAM section, added HTTP server example
parents 7b02f1c9 11deee4e
......@@ -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]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment