Skip to content

Instantly share code, notes, and snippets.

@hintjens
Created November 11, 2012 06:32
Show Gist options
  • Save hintjens/4053950 to your computer and use it in GitHub Desktop.
Save hintjens/4053950 to your computer and use it in GitHub Desktop.
inetd test
#include "czmq.h"
void
s_log (FILE *output, const char *format, ...)
{
time_t curtime = time (NULL);
struct tm *loctime = localtime (&curtime);
char formatted [20];
strftime (formatted, 20, "%y-%m-%d %H:%M:%S ", loctime);
fprintf (output, "%s", formatted);
va_list argptr;
va_start (argptr, format);
vfprintf (output, format, argptr);
fprintf (output, "\n");
va_end (argptr);
fflush (output);
}
// The main thread simply starts several clients, and a server, and then
// waits for the server to finish.
int main (void)
{
// 10-byte ZMTP/2.0 signature
byte signature [] = { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F };
FILE *trace = fopen ("/tmp/trace.log", "a");
// Send signature to client
s_log (trace, "Send signature to client");
fwrite (signature, 1, sizeof (signature), stdout);
// Read request from client
byte buffer [100];
int bytes = fread (buffer, 1, sizeof (buffer), stdin);
s_log (trace, " -- %d/%d bytes read %02x %02x %02x ... %02x ",
bytes, sizeof (signature), buffer[0], buffer[1], buffer[2], buffer [9]);
if (bytes == sizeof (signature)
&& memcmp (buffer, signature, sizeof (signature)) == 0) {
s_log (trace, " -- valid signature");
// Send socket type and empty identity
buffer [0] = 0x04; // REP socket
buffer [1] = 0x00; // Final frame
buffer [2] = 0x00; // Identity size zero
fwrite (buffer, 1, 3, stdout);
// Read socket type and identity
bytes = fread (buffer, 1, sizeof (buffer), stdin);
s_log (trace, " -- %d bytes read for socket/identity", bytes);
// Read actual request
bytes = fread (buffer, 1, sizeof (buffer), stdin);
s_log (trace, " c -- %d bytes read for request", bytes);
}
fclose (trace);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment