如果你记得贾瓦曾阻断I/O(java.net.Socket
、java.net.ServerSocket
)和通过t渠道拆开I/O(java.nio.channel.Shannel
)的话,这取决于你想要使用哪一种联系。
在阻挡I/O做法的过程中,每个客户都有自己的生产者/消费者操作。
- a thread that consumes answers that are queued locally and tries to dispatch them to the server
- the GUI that cares about placing items in the answer s queue
每当您的消费者面能够向服务器发出答案时,就将其从点上删除。
在服务器上,你可以共同处理答案:
- you have a thread that processes answers received from all clients (you can accumulate them in a buffer) and it puts itself in wait when the buffer is empty.
- every connection from a client has it s own thread that receives answers and places them in the process queue.
这种做法是安全的,但具有很大的份量。 你们可以想到,在答复时就开一个链接,将其送至服务器,然后关闭。
否则,你就可以简单地利用UDP(实际上更能解决这类问题),但是,如果你的服务器收到答复,你将不得不考虑向客户发回确认。
例如:
- student gives an answer: the answer is added to the client buffer
- client thread tries to send its buffer over UDP
- whenever a client receives an acknowledge for an answers it removes it from the buffer
- it tries periodically to send still unacknowledged answers
在服务器方面:
- whenever it receives an answer datagram from a client accumulate it on the process buffer
- you have a thread that processes all the answers on the buffer
- the server will send back an ackknowledge to the client to warn that answer has been received
但是,这仍然不能确定,因为光线上可能错失ack。
在任何情况下,均可使用<条码>。 反对输入Stream和ObjectOutputStream
在客户和服务器之间直接通过物体。 因此,你可以想像一个cap化物体。
class Answer
{
int studentId;
int questionId;
int answer;
}