public class Connection
extends java.lang.Object
Represents an individual connection to the server. Connections
are created by EventQueue
s.
When a new connection is created, a ConnectEvent
will be added to
the EventQueue
that created it.
Each connection starts its own thread to listen for input. Any input
received will be reported via an InputEvent
in the
EventQueue
that created this connection.
Messages can be sent to the connection using the
write
method.
A connection can be closed via the close
method, or it
can be terminated by the client. In either case, a DisconnectEvent
will be added to the EventQueue
that created this connection.
If you want to extend this class, you can override
EventQueue.createNewConnection
or
MultiSocketServer.createNewConnection
to return subclasses of this class.
Connection assumes that messages are segmented by the new line
(\n) character. In other words, input will not be reported until a
new line character is read, and a new line character will be appended to
every message that is sent. You can change this behavior by overriding the
readInput
and writeOutput
methods.
Modifier and Type | Field and Description |
---|---|
protected java.io.BufferedReader |
reader
Used to read input from the socket.
|
protected java.net.Socket |
socket
The socket from which this Connection reads and to which this
connection writes.
|
protected java.io.BufferedWriter |
writer
Used to write output to the socket.
|
Constructor and Description |
---|
Connection(EventQueue eventQueue,
java.net.Socket socket)
Creates a new Connection using an
InputStreamReader to
read from the socket and an OutputStreamWriter to write to the
socket. |
Connection(EventQueue eventQueue,
java.net.Socket socket,
java.io.Reader reader,
java.io.Writer writer)
Creates a new Connection using the specified reader and
writer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Disconnects this Connection from the server.
|
protected java.lang.String |
readInput()
Reads a single message of input from the socket.
|
void |
write(java.lang.String output)
Writes a message to the socket using
writeOutput . |
protected void |
writeOutput(java.lang.String output)
Writes a message to the socket.
|
protected final java.net.Socket socket
protected final java.io.BufferedReader reader
protected final java.io.BufferedWriter writer
public Connection(EventQueue eventQueue, java.net.Socket socket, java.io.Reader reader, java.io.Writer writer)
Creates a new Connection using the specified reader and writer.
Note that the reader will be wrapped in a BufferedReader
if
it is not already. The same is true for the writer.
eventQueue
- the EventQueue
that accepted the socket and is creating this connectionsocket
- the socket from which this Connection will read and to which this Connection will writereader
- the reader used to read input from the socketwriter
- the writer used to write output to the socketpublic Connection(EventQueue eventQueue, java.net.Socket socket) throws java.io.IOException
InputStreamReader
to
read from the socket and an OutputStreamWriter
to write to the
socket.eventQueue
- socket
- java.io.IOException
protected java.lang.String readInput() throws java.io.IOException
BufferedReader.readLine()
). You can change this behavior by
overriding this method.java.io.IOException
- if an I/O error occurs while readingpublic final void write(java.lang.String output)
writeOutput
. If an exception is thrown
during writing, it is ignored. Thus, it is always safe to use this
method to send messages (even if the socket is closed), but it
cannot be guaranteed that the message will be received by the client.output
- the message to sendprotected void writeOutput(java.lang.String output) throws java.io.IOException
output
- the message to sendjava.io.IOException
- if an I/O error occurs when sending the messagepublic final void close()
Disconnects this Connection from the server. After this
method is called, the Connection will no longer listen for
input or be able to send output to the socket, however any
InputEvent
s that were added to the EventQueue
before
this method was called will not be removed from the queue.
Whether the Connection is closed via this method or closed
by the client, a DisconnectEvent
will always be added to the
EventQueue
that created this connection.