Previous: , Up: The Communications Library   [Index]


13.13.3 Communications Samples

A trivial time-of-day server (writes on each connection the time of day):

file f;

tcpip_listen(f, 40000, 0);

while (1) {
    file i, o;
    date d;

    accept(i, o, f, 0);
    d_now(d);
    f_form(o, "/f2/:/f2/:/f2/", d_d_hour(d), d_h_minute(d), d_m_second(d));
}

A client, getting the time of day from the server above:

file i, o;
text s;

tcpip_connect(i, o, "127.0.0.1", 40000, 0);
f_line(i, s);
o_plan(s, "\n");

A server reading what concurrent clients are transmitting (it makes use of one ‘dispatch’ controller):

void
dc(dispatch p, file i)
{
    integer c, s;

    c = f_pick(i);
    if (c ^ -1) {
	s = f_stored(i);
	o_form("%c\n", c);
	while (s) {
	    c = f_pick(i);
	    o_form("%c\n", c);
	    s -= 1;
	}
    }

    if (c == -1) {
	w_resign(p, i);
    }
}


void
pc(dispatch p, file f)
{
    file i, o;

    accept(i, o, f, 0);
    w_watch(p, i, dc, p, i);
}


integer
main(void)
{
    dispatch p;
    file f;

    tcpip_listen(f, 40000, 0);
    w_watch(p, f, pc, p, f);
    w_press(p);

    return 0;
}

See The Control Library.