[zeromq-dev] Using closed socket with JZMQ
motoplux
motoplux at gmail.com
Wed May 16 16:00:10 CEST 2012
JZMQ dev suggest me to post here, since he has not had time to look into yet.
Hi everyone,
I'm recently playing with Zmq (v2.2.0) and its Java binder (v1.0.0),
and I run into a problem.
On Windows, I was getting a crash as an unhandled exception in javaw.exe.
This was due to my fault, because I was trying to send data through an
already closed ZMQ socket,
but it takes me lot of time to find the problem because the crash
wasn't giving me
any details on what was going on (the call stack location reported the
runtime library msvcr).
So I wrote two little spike both using the java binder and the plain
zmq api, and, surprise, I wasn't getting
any excetpion with the plain zmq. Just the zqm_send it's returing -1
when I try to send data to a closed socket.
Furthermore, I tested the java spike code on Linux too, and the
behaviour is different again: the
program ends with
java: Socket.cpp:543: void* get_socket(JNIEnv, _jobject, int):
Assertion `s' failed.
Here the two spikes I used
**** JZMQ spike****
package tests;
import org.zeromq.ZMQ;
public class TestZMQSocketClosed {
public static void main(String[] args) {
ZMQ.Context msgContext = ZMQ.context(1);
System.out.println("Connecting to hello world server\n");
ZMQ.Socket clientSocket = msgContext.socket(ZMQ.REQ);
clientSocket.connect("tcp://localhost:5600");
String request = "Hello";
System.out.println("Sending Hello on Open Socket\n");
boolean sent = clientSocket.send( request.getBytes(), 0);
System.out.println("send: " + sent + "\n");
clientSocket.setLinger(0);
clientSocket.close();
System.out.println("Sending Hello on Closed Socket\n");
sent = clientSocket.send( request.getBytes(), 0);
System.out.println("send: " + sent + "\n");
}
}
**** ZMQ spike ****
#include "zmq.h"
#include <string.h>
#include <stdio.h>
int main (void)
{
void *context = zmq_init (1);
printf ("Connecting to hello world server\n");
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5600");
zmq_msg_t request;
zmq_msg_init_size (&request, 5);
memcpy (zmq_msg_data (&request), "Hello", 5);
printf ("Sending Hello on Open Socket\n");
int send = zmq_send (requester, &request, ZMQ_NOBLOCK);
zmq_msg_close (&request);
printf("Send : %d\n", send);
int linger = 0;
zmq_setsockopt (requester, ZMQ_LINGER, &linger, sizeof linger);
zmq_close (requester);
zmq_msg_init_size (&request, 5);
memcpy (zmq_msg_data (&request), "Hello", 5);
printf ("Sending Hello on Closed Socket\n");
send = zmq_send (requester, &request, ZMQ_NOBLOCK);
zmq_msg_close (&request);
printf("Send : %d\n", send);
zmq_term (context);
return 0;
}
It seems this kind of error is not correctly managed, both on Windows
and on Linux, what do you think?
More information about the zeromq-dev
mailing list