Package client

Class Client

java.lang.Object
client.Client

public class Client extends Object
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

    Constructors
    Constructor
    Description
    Client(Socket socket, String username)
    The Client constructor takes in the below parameters and then initializes the buffered reader and writer.The JadeSerializer and JadeDeserializer are also initiliazed here.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the socket as well as the reader and writers interacting with it.Then prints the stack trace.
    void
    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
    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
    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
    Sends a packet to the server.Similarly to the sendMessage the packet is converted to a JadeObject then serialized to a string.
    void
    Sets the latest packet of the client.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Client

      public Client(Socket socket, String username)
      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 socket
      username - 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

      public 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.
      Parameters:
      packet - outgoing packet
    • sendPacket

      public 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.
      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

      public Packet 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

      public Packet getLatestPacket()
      Returns the latest packet from the server that is not null.
      Returns:
      Packet
    • setLatestPacket

      public void setLatestPacket(Packet packet)
      Sets the latest packet of the client.
      Parameters:
      packet - latest recieved packet