File tree Expand file tree Collapse file tree 4 files changed +40
-1
lines changed Expand file tree Collapse file tree 4 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ path = "examples/zguide/msreader/main.rs"
23
23
name = " msgsend"
24
24
path = " examples/msgsend/main.rs"
25
25
26
+ [[example ]]
27
+ name = " stream-logserver"
28
+ path = " examples/stream/logserver.rs"
29
+
26
30
[[example ]]
27
31
name = " tasksink"
28
32
path = " examples/zguide/tasksink/main.rs"
Original file line number Diff line number Diff line change
1
+ // Very basic example to listen tcp socket from zmq using STREAM sockets
2
+ // You can use telnet to send messages and they will be output to console
3
+ // ZMQ_STREAM socket will prepend socket identity on message, that's why we use recv_multipart here
4
+
5
+ use std:: str;
6
+ extern crate zmq;
7
+
8
+
9
+ fn main ( ) {
10
+ println ! ( "Hello, world!" ) ;
11
+
12
+ let ctx = zmq:: Context :: new ( ) ;
13
+
14
+ let socket = ctx. socket ( zmq:: STREAM ) . unwrap ( ) ;
15
+ socket. bind ( "tcp://*:8888" ) . unwrap ( ) ;
16
+ loop {
17
+ let data = socket. recv_multipart ( 0 ) . unwrap ( ) ;
18
+ println ! ( "Identity: {:?} Message : {}" , data[ 0 ] , str :: from_utf8( & data[ 1 ] ) . unwrap( ) ) ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ pub enum SocketType {
54
54
PUSH = 8 ,
55
55
XPUB = 9 ,
56
56
XSUB = 10 ,
57
+ STREAM = 11 ,
57
58
}
58
59
59
60
impl Copy for SocketType { }
@@ -707,6 +708,7 @@ impl Socket {
707
708
8 => SocketType :: PUSH ,
708
709
9 => SocketType :: XPUB ,
709
710
10 => SocketType :: XSUB ,
711
+ 11 => SocketType :: STREAM ,
710
712
_ => panic ! ( "socket type is out of range!" )
711
713
}
712
714
} )
Original file line number Diff line number Diff line change 1
1
extern crate zmq;
2
2
use zmq:: * ;
3
+ use std:: net:: TcpStream ;
3
4
4
5
fn create_socketpair ( ) -> ( Socket , Socket ) {
5
6
let ctx = Context :: default ( ) ;
@@ -151,14 +152,26 @@ fn test_get_socket_type() {
151
152
SocketType :: PULL ,
152
153
SocketType :: PUSH ,
153
154
SocketType :: XPUB ,
154
- SocketType :: XSUB
155
+ SocketType :: XSUB ,
156
+ SocketType :: STREAM ,
155
157
] ;
156
158
for sock_type in socket_types. drain ( ..) {
157
159
let sock = ctx. socket ( sock_type) . unwrap ( ) ;
158
160
assert_eq ! ( sock. get_socket_type( ) . unwrap( ) , sock_type) ;
159
161
}
160
162
}
161
163
164
+ #[ test]
165
+ fn test_create_stream_socket ( ) {
166
+ let ctx = Context :: new ( ) ;
167
+ let sock = ctx. socket ( STREAM ) . unwrap ( ) ;
168
+ assert ! ( sock. bind( "tcp://127.0.0.1:*" ) . is_ok( ) ) ;
169
+ let ep = sock. get_last_endpoint ( ) . unwrap ( ) . unwrap ( ) ;
170
+ let tcp = "tcp://" ;
171
+ assert ! ( ep. starts_with( tcp) ) ;
172
+ assert ! ( TcpStream :: connect( & ep[ tcp. len( ) ..] ) . is_ok( ) ) ;
173
+ }
174
+
162
175
#[ test]
163
176
fn test_getset_maxmsgsize ( ) {
164
177
let ctx = Context :: new ( ) ;
You can’t perform that action at this time.
0 commit comments