This project is a basic chat application implemented in Java. It consists of a client and server that communicate over a network. The client provides a graphical user interface (GUI) for users to send and receive messages, while the server handles multiple client connections and message routing.
- Text-based messaging between clients
- Simple GUI for client-side interaction
- Server-side handling of multiple client connections
A stream is a sequence of data used for reading from or writing to data sources (files, memory, network).
- For text-based data (e.g.,
.txt
files) - Classes end with:
Reader
,Writer
- Example:
InputStreamReader
,BufferedReader
- For binary data (e.g., images, videos)
- Classes end with:
InputStream
,OutputStream
- Example:
FileInputStream
,ObjectOutputStream
InputStreamReader
: Converts byte stream → character streamOutputStreamWriter
: Converts character stream → byte stream
Instead of reading/writing one byte/char at a time, use buffered streams:
BufferedReader
/BufferedWriter
(character)BufferedInputStream
/BufferedOutputStream
(byte)
Improves performance by processing blocks of data.
- Buffered output doesn’t write immediately.
- Data is sent when buffer is full.
- Use
.flush()
to force data out now.
Use .flush()
when:
- You need immediate output
- Before closing the stream
- In real-time communication
The content of the buffer, when full, gets flushed to the InputStreamReader
, which then gets flushed to the underlying InputStream
.
The Scanner
class is used to take input from the console.
System.in
is an InputStream
that is connected to keyboard input.
BufferedWriter.newLine()
adds \n
because nextLine()
doesn't return a line separator.
The accept()
method of the ServerSocket
class waits for a client connection. Once connected, a Socket
object is returned that can be used to communicate with the client.
When dealing with multiple users, you will need a specific socket for every connection; that's what accept()
returns.
So, the server socket is just for listening for connections.