Package com.example.common.transport.tcp
Class TcpSocketTransportConnection
java.lang.Object
com.example.common.transport.tcp.TcpSocketTransportConnection
- All Implemented Interfaces:
TransportConnection
The TcpSocketTransportConnection class implements the TransportConnection interface for managing a TCP socket connection.
-
Constructor Summary
ConstructorsConstructorDescriptionTcpSocketTransportConnection
(String ip, int port, String encoding) Constructs a TcpSocketTransportConnection with a new socket connected to the specified IP and port.TcpSocketTransportConnection
(Socket socket, String encoding) Constructs a TcpSocketTransportConnection with an existing socket and encoding. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close connection and free the resourcesgetIp()
Retrieves the IP address of the connection.receive()
Receive and buffered read stream of chars from server via tcp - socket connection.void
Send stream of chars (message) from client via tcp - socket connection
-
Constructor Details
-
TcpSocketTransportConnection
Constructs a TcpSocketTransportConnection with an existing socket and encoding.- Parameters:
socket
- the socket to use for the connectionencoding
- the encoding used for communication- Throws:
IOException
- if an error occurs during initialization
-
TcpSocketTransportConnection
Constructs a TcpSocketTransportConnection with a new socket connected to the specified IP and port.- Parameters:
ip
- the IP address to connect toport
- the port number to connect toencoding
- the encoding used for communication- Throws:
IOException
- if an error occurs during initialization
-
-
Method Details
-
getIp
Retrieves the IP address of the connection.- Specified by:
getIp
in interfaceTransportConnection
- Returns:
- The IP address of the connection.
- Throws:
IOException
- if an error occurs while retrieving the IP address.
-
receive
Receive and buffered read stream of chars from server via tcp - socket connection.- Specified by:
receive
in interfaceTransportConnection
- Returns:
- string with client message from console.
- Throws:
IOException
- if an error occurs while receiving the message.
-
send
Send stream of chars (message) from client via tcp - socket connection- Specified by:
send
in interfaceTransportConnection
- Parameters:
message
- from client- Throws:
IOException
- if an error occurs while sending the message
-
close
Close connection and free the resources- Specified by:
close
in interfaceTransportConnection
- Throws:
IOException
- if an error occurs while closing the connection
-