Package org.jgroups.blocks.cs
Class TcpClient
- java.lang.Object
-
- org.jgroups.blocks.cs.BaseServer
-
- org.jgroups.blocks.cs.TcpBaseServer
-
- org.jgroups.blocks.cs.TcpClient
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,Client
,ConnectionListener
public class TcpClient extends TcpBaseServer implements Client, ConnectionListener
- Since:
- 3.6.5
- Author:
- Bela Ban
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.jgroups.blocks.cs.BaseServer
BaseServer.Reaper
-
-
Field Summary
Fields Modifier and Type Field Description protected TcpConnection
conn
protected Address
remote_addr
-
Fields inherited from class org.jgroups.blocks.cs.TcpBaseServer
peer_addr_read_timeout
-
Fields inherited from class org.jgroups.blocks.cs.BaseServer
client_bind_addr, client_bind_port, conn_expire_time, conn_listeners, conns, defer_client_binding, factory, linger, local_addr, log, max_length, reaper, reaperInterval, receiver, recv_buf_size, running, send_buf_size, sock_conn_timeout, sock_creation_lock, socket_factory, tcp_nodelay, time_service, use_peer_connections
-
-
Constructor Summary
Constructors Modifier Constructor Description TcpClient(java.net.InetAddress bind_addr, int bind_port, java.net.InetAddress server_addr, int server_port)
Creates an instance of anTcpClient
that acts as a client: no server socket is created and no acceptor is started to listen for incoming connections.TcpClient(IpAddress bind_addr, IpAddress server_addr)
Creates an instance of anTcpClient
that acts as a client: no server socket is created and no acceptor is started to listen for incoming connections.protected
TcpClient(ThreadFactory thread_factory, SocketFactory socket_factory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connectionClosed(Connection conn)
void
connectionEstablished(Connection conn)
boolean
isConnected()
boolean
isOpen()
Address
remoteAddress()
TcpClient
remoteAddress(IpAddress addr)
Sets the address of the server.void
send(byte[] data, int offset, int length)
Sends data to the remote server.void
send(java.nio.ByteBuffer data)
Sends data to the remote server.void
send(Address dest, byte[] data, int offset, int length)
void
send(Address dest, java.nio.ByteBuffer data)
void
start()
Starts accepting connections.void
stop()
Stops listening for connections and handling traffic.java.lang.String
toString()
-
Methods inherited from class org.jgroups.blocks.cs.TcpBaseServer
createConnection, peerAddressReadTimeout, peerAddressReadTimeout
-
Methods inherited from class org.jgroups.blocks.cs.BaseServer
addConnection, addConnectionListener, clearConnections, clientBindAddress, clientBindAddress, clientBindPort, clientBindPort, close, closeConnection, connectionEstablishedTo, connExpireTime, connExpireTimeout, deferClientBinding, deferClientBinding, explanation, getConnection, getMaxLength, getNumConnections, getNumOpenConnections, hasConnection, linger, linger, localAddress, localAddress, log, log, notifyConnectionClosed, notifyConnectionEstablished, printConnections, reaperInterval, reaperInterval, receive, receive, receive, receiveBufferSize, receiveBufferSize, receiver, receiver, removeConnectionIfPresent, removeConnectionListener, replaceConnection, retainAll, running, sendBufferSize, sendBufferSize, sendToAll, sendToAll, setMaxLength, socketConnectionTimeout, socketConnectionTimeout, socketFactory, socketFactory, tcpNodelay, tcpNodelay, timeService, timeService, usePeerConnections, usePeerConnections, validateArgs
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jgroups.blocks.cs.Client
localAddress
-
-
-
-
Field Detail
-
remote_addr
protected Address remote_addr
-
conn
protected TcpConnection conn
-
-
Constructor Detail
-
TcpClient
public TcpClient(IpAddress bind_addr, IpAddress server_addr)
Creates an instance of anTcpClient
that acts as a client: no server socket is created and no acceptor is started to listen for incoming connections. Instead, a client socket is created (bound to bind_addr/bind_port) and connected to server_addr/server_port. This is used to send messages to the remote server and receive messages from it. Note that there is only a single TCP connection established between the client and server.- Parameters:
bind_addr
- The address to which the local socket should bind to. Can be null, then the OS picks the addressserver_addr
- The address of the server to connect to- Throws:
java.lang.Exception
- If the creation failed
-
TcpClient
public TcpClient(java.net.InetAddress bind_addr, int bind_port, java.net.InetAddress server_addr, int server_port)
Creates an instance of anTcpClient
that acts as a client: no server socket is created and no acceptor is started to listen for incoming connections. Instead, a client socket is created (bound to bind_addr/bind_port) and connected to server_addr/server_port. This is used to send messages to the remote server and receive messages from it. Note that there is only a single TCP connection established between the client and server.- Parameters:
bind_addr
- The address to which the local socket should bind to. Can be null, then the OS picks the addressbind_port
- The local port. Can be 0, then the OS picks the port.server_addr
- The address of the server to connect toserver_port
- The port of the server to connect to.- Throws:
java.lang.Exception
- If the creation failed
-
TcpClient
protected TcpClient(ThreadFactory thread_factory, SocketFactory socket_factory)
-
-
Method Detail
-
remoteAddress
public Address remoteAddress()
- Specified by:
remoteAddress
in interfaceClient
-
remoteAddress
public TcpClient remoteAddress(IpAddress addr)
Sets the address of the server. Has no effect when already connected.
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in interfaceClient
-
start
public void start() throws java.lang.Exception
Description copied from class:BaseServer
Starts accepting connections. Typically, socket handler or selectors thread are started here.- Overrides:
start
in classBaseServer
- Throws:
java.lang.Exception
-
stop
public void stop()
Description copied from class:BaseServer
Stops listening for connections and handling traffic. Typically, socket handler or selector threads are stopped, and server sockets or channels are closed.- Overrides:
stop
in classBaseServer
-
send
public void send(Address dest, byte[] data, int offset, int length) throws java.lang.Exception
- Overrides:
send
in classBaseServer
- Throws:
java.lang.Exception
-
send
public void send(Address dest, java.nio.ByteBuffer data) throws java.lang.Exception
- Overrides:
send
in classBaseServer
- Throws:
java.lang.Exception
-
send
public void send(byte[] data, int offset, int length) throws java.lang.Exception
Description copied from interface:Client
Sends data to the remote server. The server's address must have been set before.
-
send
public void send(java.nio.ByteBuffer data) throws java.lang.Exception
Description copied from interface:Client
Sends data to the remote server. The server's address must have been set before.
-
connectionClosed
public void connectionClosed(Connection conn)
- Specified by:
connectionClosed
in interfaceConnectionListener
- Overrides:
connectionClosed
in classBaseServer
-
connectionEstablished
public void connectionEstablished(Connection conn)
- Specified by:
connectionEstablished
in interfaceConnectionListener
- Overrides:
connectionEstablished
in classBaseServer
-
toString
public java.lang.String toString()
- Overrides:
toString
in classBaseServer
-
-