Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <ol> <li>How does one create a persistant Server &lt;-> Client connection,as described above.</li> </ol> </blockquote> <p>You've got several choices: you can write a threaded server, where a single 'process' with mostly shared memory executes multiple threads of execution, and each thread has some thread-local storage for e.g. <code>client</code>. Or, you could write a multi-process server, where each client connection gets its own independent process via <code>fork(2)</code> (which pretty much means it runs on Unix-only, as Windows attempts to implement <code>fork(2)</code> never work well). Or, you could write an event-loop based state machine, and use <code>select(2)</code> or <code>poll(2)</code> or <code>epoll(4)</code> to manage which clients are readable, writable, and have errors.</p> <p>Choosing between them might seem difficult, <a href="http://www.kegel.com/c10k.html" rel="nofollow">but there are guidelines available</a>. I'd like to suggest that -small- systems, where you're only expected to have 20-30 simultaneous clients, can be very well handled via threaded or forked servers. If you wish to go beyond that number of simultaneous clients, you should use a tool like <a href="https://github.com/eventmachine/eventmachine/wiki" rel="nofollow">EventMachine</a> or <a href="http://monkey.org/~provos/libevent/" rel="nofollow">libevent</a> to write your server. (Which may or may not be easier to maintain anyway.)</p> <blockquote> <p>2) Is there a better way of keeping a server open, without using loop [because I feel like I've commited murder by creating an endless loop]</p> </blockquote> <p>Endless loops are quite fine :) but sometimes it makes sense to offer a way to restart servers or terminate them via <code>signal(7)</code>s or specialized command interfaces.</p> <blockquote> <p>3) Why does it stay suspended like that without client.close in the server? Does the server buffer the messages before it sends them?</p> </blockquote> <p>Ah, <strong>here</strong> is the question you really needed -- TCP will try to <em>buffer</em> the results of multiple packets from the server, and due to the potential need to fragment too-large packets mid-route, you, as a client, have <em>no guarantees</em> that any kind of datagram boundaries are preserved.</p> <p>Chances are <em>very</em> good that your server's TCP/IP stack waited a tiny bit before sending the first packet to see if more data is coming soon. (It often does.) It probably coalesced the <em>two</em> calls to <code>client.print()</code> into a single TCP packet. Your client calls <code>client.read()</code> <em>twice</em>, but there is probably only enough data for <em>one</em> read. If you wanted your TCP stream to send the first packet <em>immediately</em>, you could look into setting the <code>TCP_NODELAY</code> socket option to disable this small delay. (But it might not help all on its own; you might need to use it in conjunction with <code>TCP_CORK</code> to try to "bottle up" all the writes until you explicitly flush them. This is probably an awful idea.)</p> <p>A better option would be to re-write your client to be aware of the <em>protocol</em> between your server and client; it needs to read input into a buffer, parse the buffer to find 'complete messages', consume those messages, and then return to its main event loop.</p> <p>Maybe your protocol is to read / write ASCII or UTF-8 terminated with <code>"\n"</code> characters; in that case, you can change <code>print</code> to <code>puts</code> or add the <code>\n</code> in the right places, and change <code>read()</code> to <code>readline()</code>. (This would let the standard IO library handle your protocol for you.) Another approach would be to send data with length+data pairs, and await the correct length of data.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload