Blog: 2025-02-23: Difference between revisions

From razwiki
Jump to navigation Jump to search
(Created page with "Fun with the gcc compiler and linker <pre> hack $ gcc-14 -L /usr/local/lib/ -I /usr/local/include/ -Wall -Werror -l zmq hwserver.c -o hwserver hack $ ./hwserver </pre>")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
hack $ ./hwserver
hack $ ./hwserver
</pre>
</pre>


Some sockets with the zmq

<pre>
-- hwclient.lua
local zmq = require "zmq"

local context = zmq.init(1)

local socket = context:socket(zmq.REQ)
socket:connect("tcp://localhost:5555")

for n=1, 10 do
print("Sending Hello " .. n .. "...")
socket:send("HIIII -lua")
local reply = socket:recv()
print("Received " .. n .. " [" .. reply .. "]")
end

socket:close()
context:term()
</pre>

<pre>
// hwclient.c
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main() {
printf("Connecting to hello world server...\n");
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:5555");

int request_nbr;
char outgoing[8];
for (request_nbr = 0; request_nbr < 10; request_nbr++) {
char buffer[10];
printf("Sending Hello %d...\n", request_nbr);
sprintf(outgoing, "Hello %d", request_nbr);
zmq_send(requester, outgoing, 10, 0);
zmq_recv(requester, buffer, 10, 0);
printf("Received World %d\n", request_nbr);
}

zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
</pre>

<pre>
// hwserver.c
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main() {
void *context = zmq_ctx_new();
void *responder = zmq_socket(context, ZMQ_REP);

int rc = zmq_bind(responder, "tcp://*:5555");
assert (rc == 0);

while (1) {
char buffer[10];
zmq_recv(responder, buffer, 10, 0);
printf("received %s\n", buffer);
sleep(1);
zmq_send(responder, "world", 5, 0);
}

return 0;
}
</pre>

and in <code>~/.profile</code>:
export LUA_CPATH="/usr/local/lib/lua/5.4/?.so;$HOME/.luarocks/lib/lua/5.4/?.so"

Links:
* https://github.com/booksbyus/zguide/tree/master/examples
* https://zguide.zeromq.org/docs/chapter1/

Latest revision as of 23:39, 23 February 2025

Fun with the gcc compiler and linker

hack $ gcc-14 -L /usr/local/lib/ -I /usr/local/include/ -Wall -Werror -l zmq hwserver.c -o hwserver
hack $ ./hwserver


Some sockets with the zmq

-- hwclient.lua
local zmq = require "zmq"

local context = zmq.init(1)

local socket = context:socket(zmq.REQ)
socket:connect("tcp://localhost:5555")

for n=1, 10 do
  print("Sending Hello " .. n .. "...")
  socket:send("HIIII -lua")
  local reply = socket:recv()
  print("Received " .. n .. " [" .. reply .. "]")
end

socket:close()
context:term()
// hwclient.c
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main() {
  printf("Connecting to hello world server...\n");
  void *context = zmq_ctx_new();
  void *requester = zmq_socket(context, ZMQ_REQ);
  zmq_connect(requester, "tcp://localhost:5555");

  int request_nbr;
  char outgoing[8];
  for (request_nbr = 0; request_nbr < 10; request_nbr++) {
    char buffer[10];
    printf("Sending Hello %d...\n", request_nbr);
    sprintf(outgoing, "Hello %d", request_nbr);
    zmq_send(requester, outgoing, 10, 0);
    zmq_recv(requester, buffer, 10, 0);
    printf("Received World %d\n", request_nbr);
  }

  zmq_close(requester);
  zmq_ctx_destroy(context);
  return 0;
}
// hwserver.c
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main() {
  void *context = zmq_ctx_new();
  void *responder = zmq_socket(context, ZMQ_REP);

  int rc = zmq_bind(responder, "tcp://*:5555");
  assert (rc == 0);

  while (1) {
    char buffer[10];
    zmq_recv(responder, buffer, 10, 0);
    printf("received %s\n", buffer);
    sleep(1);
    zmq_send(responder, "world", 5, 0);
  }

  return 0;
}

and in ~/.profile:

export LUA_CPATH="/usr/local/lib/lua/5.4/?.so;$HOME/.luarocks/lib/lua/5.4/?.so"

Links: