| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/R_zmq_sendrecv.r |
| 3 |
\name{Send Receive Functions} |
| 4 |
\alias{Send Receive Functions} |
| 5 |
\alias{zmq.send} |
| 6 |
\alias{zmq.recv} |
| 7 |
\title{Send Receive Functions} |
| 8 |
\usage{ |
| 9 |
zmq.send(socket, buf, flags = ZMQ.SR()$BLOCK) |
| 10 |
|
| 11 |
zmq.recv( |
| 12 |
socket, |
| 13 |
len = 1024L, |
| 14 |
flags = ZMQ.SR()$BLOCK, |
| 15 |
buf.type = c("char", "raw") |
| 16 |
) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{socket}{a ZMQ socket} |
| 20 |
|
| 21 |
\item{buf}{a buffer to be sent} |
| 22 |
|
| 23 |
\item{flags}{a flag for the method using by \code{zmq_send} and |
| 24 |
\code{zmq_recv}} |
| 25 |
|
| 26 |
\item{len}{a length of buffer to be received, default 1024 bytes} |
| 27 |
|
| 28 |
\item{buf.type}{buffer type to be received} |
| 29 |
} |
| 30 |
\value{ |
| 31 |
\code{zmq.send()} returns number of bytes (invisible) in the sent |
| 32 |
message if successful, otherwise returns -1 (invisible) and sets |
| 33 |
\code{errno} to the error value, see ZeroMQ manual for details. |
| 34 |
|
| 35 |
\code{zmq.recv()} returns a list (\code{ret}) containing the received buffer |
| 36 |
\code{ret$buf} and the length of received buffer (\code{ret$len} which is |
| 37 |
less or equal to the input \code{len}) if successful, otherwise returns -1 |
| 38 |
and sets \code{errno} to the error value, see ZeroMQ manual for details. |
| 39 |
} |
| 40 |
\description{ |
| 41 |
Send and receive functions |
| 42 |
} |
| 43 |
\details{ |
| 44 |
\code{zmq.send()} is a high level R function calling ZMQ C API |
| 45 |
\code{zmq_send()} sending \code{buf} out. |
| 46 |
|
| 47 |
\code{zmq.recv()} is a high level R function calling ZMQ C API |
| 48 |
\code{zmq_recv()} receiving buffers of length \code{len} according to the |
| 49 |
\code{buf.type}. |
| 50 |
|
| 51 |
\code{flags} see \code{\link{ZMQ.SR}()} for detail options of send and |
| 52 |
receive functions. |
| 53 |
|
| 54 |
\code{buf.type} currently supports \code{char} and \code{raw} which are both |
| 55 |
in R object format. |
| 56 |
} |
| 57 |
\examples{ |
| 58 |
\dontrun{ |
| 59 |
### Using request-reply pattern. |
| 60 |
|
| 61 |
### At the server, run next in background or the other window. |
| 62 |
library(pbdZMQ, quietly = TRUE) |
| 63 |
|
| 64 |
context <- zmq.ctx.new() |
| 65 |
responder <- zmq.socket(context, ZMQ.ST()$REP) |
| 66 |
zmq.bind(responder, "tcp://*:5555") |
| 67 |
for(i.res in 1:5){ |
| 68 |
buf <- zmq.recv(responder, 10L) |
| 69 |
cat(buf$buf, "\n") |
| 70 |
Sys.sleep(0.5) |
| 71 |
zmq.send(responder, "World") |
| 72 |
} |
| 73 |
zmq.close(responder) |
| 74 |
zmq.ctx.destroy(context) |
| 75 |
|
| 76 |
|
| 77 |
### At a client, run next in foreground. |
| 78 |
library(pbdZMQ, quietly = TRUE) |
| 79 |
|
| 80 |
context <- zmq.ctx.new() |
| 81 |
requester <- zmq.socket(context, ZMQ.ST()$REQ) |
| 82 |
zmq.connect(requester, "tcp://localhost:5555") |
| 83 |
for(i.req in 1:5){ |
| 84 |
cat("Sending Hello ", i.req, "\n") |
| 85 |
zmq.send(requester, "Hello") |
| 86 |
buf <- zmq.recv(requester, 10L) |
| 87 |
cat("Received World ", i.req, "\n") |
| 88 |
} |
| 89 |
zmq.close(requester) |
| 90 |
zmq.ctx.destroy(context) |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
\references{ |
| 95 |
ZeroMQ/4.1.0 API Reference: |
| 96 |
\url{https://libzmq.readthedocs.io/en/zeromq4-1/} |
| 97 |
|
| 98 |
Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 99 |
} |
| 100 |
\seealso{ |
| 101 |
\code{\link{zmq.msg.send}()}, \code{\link{zmq.msg.recv}()}. |
| 102 |
} |
| 103 |
\author{ |
| 104 |
Wei-Chen Chen \email{[email protected]}. |
| 105 |
} |
| 106 |
\keyword{programming} |