Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll just give a rough gist and improve on it later, but here goes:</p> <hr> <h2>Overview:</h2> <p>So you store some details on the server, and then when the client connects it sends a username and password and authenticates against these details. You store the endpoint <code>Socket</code> in a map which is <code>String username -&gt; Socket socket</code>. Also store the reverse, <code>Socket socket -&gt; String username</code>. Use that to locate the username when you receive something from the socket. Even if you use socket channels, you can still get the socket from the channel using channel methods.</p> <hr> <h2>For managing these connections with NIO:</h2> <p>What I didn't talk about was how you'd receive the username / password data in the first place. For this, I advise framing. Read the data size, and then read the type of data, which should be two integers. You can write objects directly to the socket stream and process them in the same order on the other end.</p> <p>You should use a selector with selector keys, and store received data in buffers attached to these selection keys. Then you just keep selecting and adding received data to the buffer if the socket is readable, while trying to process the buffer. If the buffer has useful data after a write to it from a read (i.e., you received the size of the data as well as the full data length), you need to remove the data and compact the buffer -- i.e., move the buffer position up by data length size (size of int) + data type size (size of int) + data length and then blit the entire lot down to 0 and set the position to the end of the current received data. Use <code>buffer.compact()</code> for this.</p> <p>I'd really advise something like <a href="http://code.google.com/p/kryonet/" rel="nofollow">KryoNet</a> to do the hard work. Network programming is hard. But this might be for an assignment, so...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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