Class TcpSocketTransportConnection

java.lang.Object
com.example.common.transport.tcp.TcpSocketTransportConnection
All Implemented Interfaces:
TransportConnection

public class TcpSocketTransportConnection extends Object implements TransportConnection
The TcpSocketTransportConnection class implements the TransportConnection interface for managing a TCP socket connection.
  • Constructor Details

    • TcpSocketTransportConnection

      public TcpSocketTransportConnection(Socket socket, String encoding) throws IOException
      Constructs a TcpSocketTransportConnection with an existing socket and encoding.
      Parameters:
      socket - the socket to use for the connection
      encoding - the encoding used for communication
      Throws:
      IOException - if an error occurs during initialization
    • TcpSocketTransportConnection

      public TcpSocketTransportConnection(String ip, int port, String encoding) throws IOException
      Constructs a TcpSocketTransportConnection with a new socket connected to the specified IP and port.
      Parameters:
      ip - the IP address to connect to
      port - the port number to connect to
      encoding - the encoding used for communication
      Throws:
      IOException - if an error occurs during initialization
  • Method Details

    • getIp

      public String getIp() throws IOException
      Retrieves the IP address of the connection.
      Specified by:
      getIp in interface TransportConnection
      Returns:
      The IP address of the connection.
      Throws:
      IOException - if an error occurs while retrieving the IP address.
    • receive

      public String receive() throws IOException
      Receive and buffered read stream of chars from server via tcp - socket connection.
      Specified by:
      receive in interface TransportConnection
      Returns:
      string with client message from console.
      Throws:
      IOException - if an error occurs while receiving the message.
    • send

      public void send(String message) throws IOException
      Send stream of chars (message) from client via tcp - socket connection
      Specified by:
      send in interface TransportConnection
      Parameters:
      message - from client
      Throws:
      IOException - if an error occurs while sending the message
    • close

      public void close() throws IOException
      Close connection and free the resources
      Specified by:
      close in interface TransportConnection
      Throws:
      IOException - if an error occurs while closing the connection