Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Created July 10, 2012 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kballenegger/3085793 to your computer and use it in GitHub Desktop.
Save kballenegger/3085793 to your computer and use it in GitHub Desktop.
[eye ~/Desktop•dotfiles]$ cat HelloWorldClient.java
//
// Hello World client in Java
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//
// Naveen Chawla <naveen.chwl@gmail.com>
//
import org.zeromq.ZMQ;
public class HelloWorldClient{
public static void main(String[] args){
// Prepare our context and socket
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REQ);
System.out.println("Connecting to hello world server...");
socket.connect ("tcp://localhost:5555");
// Do 10 requests, waiting each time for a response
for(int request_nbr = 0; request_nbr != 10; request_nbr++) {
// Create a "Hello" message.
// Ensure that the last byte of our "Hello" message is 0 because
// our "Hello World" server is expecting a 0-terminated string:
String requestString = "Hello" + " ";
byte[] request = requestString.getBytes();
request[request.length-1]=0; //Sets the last byte to 0
// Send the message
System.out.println("Sending request " + request_nbr + "...");
socket.send(request, 0);
// Get the reply.
byte[] reply = socket.recv(0);
// When displaying reply as a String, omit the last byte because
// our "Hello World" server has sent us a 0-terminated string:
System.out.println("Received reply " + request_nbr + ": [" + new String(reply,0,reply.length-1) + "]");
}
}
}
[eye ~/Desktop•dotfiles]$ javc -cp .:/Users/kenneth/Dropbox/dev/caffeine/status/jvm/lib/jzmq-2.0.7-20100502.112537-2.jar HelloWorldClient.java t
[eye ~/Desktop•dotfiles]$ java -cp .:/Users/kenneth/Dropbox/dev/caffeine/status/jvm/lib/jzmq-2.0.7-20100502.112537-2.jar -Djava.library.path=/usr/local/lib HelloWorldClient
Connecting to hello world server...
Sending request 0...
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.zeromq.ZMQ$Socket.recv(J)[B
at org.zeromq.ZMQ$Socket.recv(Native Method)
at HelloWorldClient.main(HelloWorldClient.java:32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment