Class TCPClient

  • All Implemented Interfaces:
    Initializable, Closeable, AutoCloseable, Runnable

    public class TCPClient
    extends Object
    implements Closeable, Initializable, Runnable

    A TCP client for the JTalker application. This class defines everything you would need to get a server and a client to talk, including the socket programming and the method that JTalker uses to exit and enter. There are two things of note. The Runnable code in this class is used as the sending portion of the client; the listening part of the client is defined within the init method. The listening Thread is started as soon as the init method is called; the sending portion is only initiated after the run method is called within a thread. This means that for every client, there are two threads running.

    Since:
    JTalker 0.0.1
    Version:
    0.0.1
    Author:
    C. William Oswald
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes all of the associated streams and sockets associated with a client.
      String getHost()
      Returns the host we want to or are already connected to.
      String getID()
      Returns the unique identifier we will or are using on the host.
      int getPort()
      Returns the port number our socket will or has binded to.
      void init()
      Initializes the client by constructing the socket, creating its input and output streams, waits to be connected, receives a boolean value from the server, sends our unique identifier to the server, and starts the listening thread.
      boolean isRunning()
      Returns the boolean value that represents whether the client is running.
      void run()
      Runs the sending thread if we have been initialized.
      void setHost​(String host)
      Sets the host we want to or are already connected to.
      void setID​(String identifier)
      Sets the unique identifier we will or are using on the host.
      void setPort​(int port)
      Sets the port number our socket will or has binded to.
    • Constructor Detail

      • TCPClient

        public TCPClient​(InputStream in,
                         PrintStream out,
                         String identifier,
                         String host,
                         int port)
        Constructs a TCPClient with the given input, output, client identifier, host, and port number. No parameter can be null, and the identifier cannot be the exit message , which is "EXIT" (using the equalsIgnoreCase method). The port must not only be a valid port, but it also must be within the port range specified by the min and max port numbers ( 0 and 65535).
        Parameters:
        in - The input stream to use when sending data over a socket.
        out - The output stream to send data to when the client receives it.
        identifier - The unique identifier to be used by the server.
        host - The host we are connecting to.
        port - The port number we are binding the TCP socket to.
        Throws:
        IllegalArgumentException - If any parameter is null, or if the port is outside of the given bounds, or if the identifier is equal to the exit message.
      • TCPClient

        public TCPClient​(String identifier,
                         String host,
                         int port)
        Constructs a TCP client with System.in as input, System.out as output, and the rest of the parameters. This will call the large constructor with the in and out fields as mentioned before.
        Parameters:
        identifier - The unique identifier to be used by the server.
        host - The host we are connecting to.
        port - The port number we are binding the TCP socket to.
        See Also:
        TCPClient(InputStream, PrintStream, String, String, int)
    • Method Detail

      • init

        public void init()
        Initializes the client by constructing the socket, creating its input and output streams, waits to be connected, receives a boolean value from the server, sends our unique identifier to the server, and starts the listening thread. Within the listening thread is the logic used by the client to detect when connection has been lost, and within the socket/stream creation is the logic used to detect when an unknown host or an I/O error occurs. In both cases, the client will stop by printint out a message. No exception is thrown, so if a client does not connect within this method, a new client must be created. Now the init method may be called again as long as the information is correct.
        Specified by:
        init in interface Initializable
      • run

        public void run()
        Runs the sending thread if we have been initialized. Note that the running method will only work if the init method has been called, and there is a guard in place just in case the init method hasn't been called. This method will automatically call the close method after the exit message has been received.
        Specified by:
        run in interface Runnable
        See Also:
        ServerOutputStream.writeUTF(String)
      • close

        public void close()
                   throws IOException
        Closes all of the associated streams and sockets associated with a client. This will make sure we are not running, close the BufferedReader we created from the in parameter, the socket output stream, the socket input stream, and the socket itself. It will not close the in and out parameters that were given to the TCPClient when constructing the object. This means that there is a potential for a memory leak if a client is initialized over and over again; because the BufferedReader is not closed, it is not explicitly dealt with. It is up to the programmer to deal with this issue and close the underlying in resource, and to handle any other memory leaks that may occur.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - If and I/O error occurs
        See Also:
        TCPClient(InputStream, PrintStream, String, String, int)
      • getHost

        public String getHost()
        Returns the host we want to or are already connected to.
        Returns:
        The host.
      • getID

        public String getID()
        Returns the unique identifier we will or are using on the host.
        Returns:
        The unique identifier.
      • getPort

        public int getPort()
        Returns the port number our socket will or has binded to.
        Returns:
        The port number.
      • isRunning

        public boolean isRunning()
        Returns the boolean value that represents whether the client is running.
        Returns:
        true when the client is running, false otherwise.
      • setHost

        public void setHost​(String host)
        Sets the host we want to or are already connected to. Note that this method will not change the host if we are currently connected to a host.
        Parameters:
        host - The host we are connecting to.
      • setID

        public void setID​(String identifier)
        Sets the unique identifier we will or are using on the host. Note that this method will not change the identifier if we are currently connected to a host.
        Parameters:
        identifier - The unique identifier to be used by the server.
      • setPort

        public void setPort​(int port)
        Sets the port number our socket will or has binded to. Note that this method will not change the port if we are currently connected to a host.
        Parameters:
        port - The port number we are binding the TCP socket to.