Package client
Class Client
java.lang.Object
client.Client
The client class handles all IO interactions with the server.
This is done through a TCP Socket.
To ensure consistency between data packets Jade has also been implemented.
- Since:
- 2023
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the socket as well as the reader and writers interacting with it.Then prints the stack trace.void
connect()
Attempts to connect to the server.Returns the latest packet from the server that is not null.Gets a packet from the server.boolean
Checks whether the server is connected or not.void
Creates a new runnable thread and starts it in order to listen for messages from the server.void
sendJoin()
Sends a join message to the server (just username).void
The sendMessage method uses the console to take in messages from the user.Also sends the username of the username at the first to get initialized as a user on the server.void
sendJoinPacket
(Packet packet) Sends a packet to the server through the buffered writer.Also sends the username so the client can be initialised on the server.void
The send message function sends a message to the server.The user's message is first converted into a Packet then JadeObject then a serialized string.void
sendPacket
(Packet packet) Sends a packet to the server.Similarly to the sendMessage the packet is converted to a JadeObject then serialized to a string.void
setLatestPacket
(Packet packet) Sets the latest packet of the client.
-
Constructor Details
-
Client
The Client constructor takes in the below parameters and then initializes the buffered reader and writer.The JadeSerializer and JadeDeserializer are also initiliazed here.- Parameters:
socket
- server socketusername
- client username
-
-
Method Details
-
connect
public void connect()Attempts to connect to the server. -
sendJoin
public void sendJoin()Sends a join message to the server (just username). -
sendJoinMessage
public void sendJoinMessage()The sendMessage method uses the console to take in messages from the user.Also sends the username of the username at the first to get initialized as a user on the server. -
sendMessage
public void sendMessage()The send message function sends a message to the server.The user's message is first converted into a Packet then JadeObject then a serialized string. The serialized string is then sent using the bufferedWriter. Also note the first message sent from the bufferedWriter is the clients username, this is so the user can be initialized server side. -
sendJoinPacket
Sends a packet to the server through the buffered writer.Also sends the username so the client can be initialised on the server.- Parameters:
packet
- outgoing packet
-
sendPacket
Sends a packet to the server.Similarly to the sendMessage the packet is converted to a JadeObject then serialized to a string.- Parameters:
packet
- outgoing packet
-
listenForMessage
public void listenForMessage()Creates a new runnable thread and starts it in order to listen for messages from the server. Threaded so that the Client can both send and recieve simultaneously. Gets a packet from the getPacket() method and assigns latest packet based on its contents. -
getPacket
Gets a packet from the server.- Returns:
- Packet
-
close
public void close()Close the socket as well as the reader and writers interacting with it.Then prints the stack trace. -
isConnected
public boolean isConnected()Checks whether the server is connected or not.- Returns:
- boolean
-
getLatestPacket
Returns the latest packet from the server that is not null.- Returns:
- Packet
-
setLatestPacket
Sets the latest packet of the client.- Parameters:
packet
- latest recieved packet
-