question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

simple server/client example

See original GitHub issue

is there some simple client/server example? i did not find one. i am currently trying to get the basics working.

import org.zeromq.ZContext;
import org.zeromq.ZMQ;

public class Server {

    ZContext context = new ZContext(1);
    ZMQ.Socket socket = context.createSocket(ZMQ.PAIR);

    public static void main(String args[]) {
        Server server = new Server();
        server.start();

    }

    public void start() {

        // Socket to talk to clients
        socket.bind("tcp://*:10101");
        byte[] recv = socket.recv();
        System.out.println("Received " + new String(recv));
    }

    public void stop() {
        socket.close();
        context.close();
    }
}

import org.zeromq.ZContext;
import org.zeromq.ZMQ;

public class Client {

    ZContext context = new ZContext(1);
    ZMQ.Socket socket = context.createSocket(ZMQ.PAIR);

    public void sendMessage() {
        socket.connect("tcp://127.0.0.1:10101");
//        byte[] recv = socket.recv();
//        if (recv != null) {
//            System.out.println("received: " + new String(recv));
//        }
        boolean b =  socket.send("hi");
        System.out.println("Sent with return: " + b);
    }

    public static void main(String args[]) {
        Client client = new Client();
        client.sendMessage();
    }

}

Can someone tell me why this is not working? The client sends the data and “b” is true, so i assume it is sent. But the server never receives it.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
damnmscommented, Sep 9, 2018

thanks, i understood that 😉

1reaction
fredoboulocommented, Sep 8, 2018

Hi, I noticed you did not close the context of the client and did not set linger of the socket to -1.

The meaning of send() true is more like “Ok, I queued the message and will make best efforts to actually send it”.

As the client is stopped straight after queuing the message, this message might not have processed by the I/O thread.

Closing the context with linger -1 will block until all pending messages have been sent to a peer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple client/server application in C - GeeksforGeeks
Overview : Create a simple client/server application in C using the concept of socket programming. Where server send some message to the ...
Read more >
Python Socket Programming - Server, Client Example
Today we will look into python socket programming example. We will create python socket server and client applications.
Read more >
Client - Server Programming: A Simple Server
As in our last example, the basic structure for a client program or applet is as follows: /* Opening up the connection to...
Read more >
C Socket Programming: Simple Server and Client - GitHub
Server : Takes request from the clients, perform required processing, and send it to the client · Create a socket with the socket()...
Read more >
Socket Programming in Python (Guide) - Real Python
The obvious example is the Internet, which you connect to via your ISP. In this tutorial, you'll create: A simple socket server and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found