Package coffeedraw

Class Client

  • All Implemented Interfaces:
    java.lang.Runnable

    public class Client
    extends java.lang.Object
    A Client makes a connection to another computer (the server) and can send messages to the server and receive messages back. You will never create a Client directly. Instead, create an object that implements the ClientListener interface, and pass that object to Network.configure() so that it will be notified when the user has connected to a server and when messages come in from that server.

    The Client object is passed to all the ClientListener methods. You can use that object to send a reply to the server right away, using the sendMessage() method, or you can keep the Client in an instance variable so that you can send messages later when you need to. The Client also has methods to determine the name the user entered for their Client, and the name and address of the Server that they joined.
    Author:
    Noah Coetsee (noahcoder77@gmail.com)
    See Also:
    ClientListener, Network.configure(String, ServerListener, ClientListener)
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      java.lang.String getName()
      Get the client name.
      java.net.InetSocketAddress getServerAddress()
      Get the server address.
      java.lang.String getServerName()
      Get the server name.
      boolean isRunning()
      Check if the client is still connected.
      void run()
      Deprecated.
      for internal use only
      void sendMessage​(java.lang.String message)
      Send a message to the server.
      void shutDown()
      Shut down the client, disconnecting it from the server.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • sendMessage

        public void sendMessage​(java.lang.String message)
        Send a message to the server. This will result in the server listener getting a messageReceived() event.
        Parameters:
        message - The message to send.
      • getName

        public java.lang.String getName()
        Get the client name. This is the name that the user entered in the "Join network game" window.
        Returns:
        the client name
      • getServerName

        public java.lang.String getServerName()
        Get the server name. This is the name that the user selected from the list in the "Join network game" window.
        Returns:
        the server name
      • getServerAddress

        public java.net.InetSocketAddress getServerAddress()
        Get the server address.
        Returns:
        the server address
      • isRunning

        public boolean isRunning()
        Check if the client is still connected.
        Returns:
        true if the client is running, false otherwise.
      • shutDown

        public void shutDown()
        Shut down the client, disconnecting it from the server. The client listener will receive a clientDisconnected() message, and the server listener will receive a channelClosed() event. Quitting the program will automatically shut down the client.
      • run

        public void run()
        Deprecated.
        for internal use only
        Specified by:
        run in interface java.lang.Runnable