@MinDoc(copyright="Copyright 2005, 2014 A. Weinert", author="Albrecht Weinert", version="V.65", lastModified="3.06.2024", usage="start as Java application (-? for help)", purpose="a small IP client") public class ClientLil extends Object
import de.frame4j.net.ClientLil;
// ......
ClientLil eventClientLil = new ClientLil();
String eventHost;
int eventPort;
int timeout;
String eventText;
// .......
if (eventClientLil == null) try {
eventClientLil.setHost(eventHost);
eventClientLil.setPort(eventPort);
eventClientLil.setTimeOut(tiemOut);
eventClientLil.connect(true);
} catch (Exception e) {
log.println(" /// warning : event server " + eventHost
+ " : " + e);
}
// while (.......
String answ = null;
Exception ex = null;
try {
answ = eventClientLil.use(eventText);
/// ..... eventClientLil.use(......
eventClientLil.disconnect();
} catch (Exception e) { ex = e; }
log.println("\n /// eventserver < " + eventText
+ "\n /// answered > " + answ
+ "\n /// problems : " + ex + "\n");
As test client for this class is also an application.
For usage see:Modifier and Type | Field and Description |
---|---|
protected String |
encoding
Encoding for textual input and output.
|
protected String |
host
The server's (the host's) name.
|
protected InetAddress |
inetAddress
The host address.
|
int |
noOfAccIncMess
The number of messages (still) to be accepted from server.
|
protected int |
port
The server's (the host's) port.
|
Socket |
sock
The socket.
|
InputStream |
sockIn
The socket input stream.
|
OutputStream |
sockOut
The socket output stream.
|
BufferedReader |
sockReader
The socket input as Reader.
|
PrintWriter |
sockWriter
The socket output as Writer.
|
protected int |
timeOut
Maximum wait time for the server's (host's) responses.
|
Constructor and Description |
---|
ClientLil() |
Modifier and Type | Method and Description |
---|---|
Socket |
connect(boolean textIO)
Connecting.
|
void |
disconnect()
Disconnecting.
|
String |
getEncoding()
Encoding for textual input and output.
|
String |
getHost()
The server's (the host's) name.
|
String |
getHostAddress()
The host address (as String).
|
int |
getPort()
The server's (the host's) port.
|
int |
getTimeOut()
Maximum wait time for the server's (host's) responses.
|
static void |
main(String[] args)
The ClientLil (wee client) as application.
|
void |
setEncoding(CharSequence encoding)
Encoding for textual input and output.
|
void |
setHost(CharSequence host)
Set the server's or host's name (and address).
|
void |
setPort(int port)
The server's (the host's) port.
|
void |
setTimeOut(int timeOut)
Maximum wait time for the server's (host's) responses.
|
String |
toString()
The (main static) connection's settings as String.
|
String |
use(String send)
Use the connection — text.
|
String |
usePingPong(String send,
int maxLen)
Use the connection once or for the last time — text.
|
protected String host
protected int port
getPort()
protected int timeOut
getTimeOut()
,
setTimeOut(int)
protected String encoding
getEncoding()
,
setEncoding(java.lang.CharSequence)
public volatile int noOfAccIncMess
connect()
if smallerprotected InetAddress inetAddress
public volatile Socket sock
public volatile InputStream sockIn
public volatile OutputStream sockOut
public volatile BufferedReader sockReader
public volatile PrintWriter sockWriter
public final String getHost()
public final void setHost(CharSequence host) throws UnknownHostException
connecting
.host
- the serverUnknownHostException
- problems when resolving the serverpublic final int getPort()
public final void setPort(int port)
connecting
.port
- 0..65535 new port number; other values act as 7 (default)public final int getTimeOut()
setTimeOut(int)
.public final void setTimeOut(int timeOut)
connecting
.timeOut
- >= 0 : the new time out value in ms;
< 0 : defaults to 30sgetTimeOut()
public final String getEncoding()
setEncoding(java.lang.CharSequence)
.public final void setEncoding(CharSequence encoding)
connecting
. Allowed values are iso8859-1 (..19), utf-8
etc., see Java documentation. encoding
- will be stripped from enclosing white space; null or empty
will default to null.getEncoding()
public final String getHostAddress()
public Socket connect(boolean textIO) throws IOException
port
is made. The streams
and optionally the reader will be provided.disconnect()
in this
case.textIO
- true: make also the Reader sockReader
and
Writer sockWriter
IOException
- if the connecting to host
.port
failspublic String use(String send) throws IOException
port
is made by
connect(true)
.send
if not null will be written with a closing
line feed (println()) to the server. The response line will then be
waited for (readln()).disconnect()
in this
case.send
- text to be send to the serverIOException
- if the connecting to host
:port
failspublic String usePingPong(String send, int maxLen) throws IOException
use(String)
) made
by connect(true)
already, this method delegates to
use(send)
and disconnect()
.send
will by send (as default encoded
byte[] buffer) and a text of maxLen
bytes received. If
maxLen
is outside the range 192 .. 30000 it is taken as 4090.usePingPong(send, 192);
send
- text to be send to the servermaxLen
- maximum length in bytes of the answer (192 .. 30000;
default: 4090)IOException
- on problems with the socket connettion to
host
.port
use(String)
public void disconnect() throws IOException
connect()
will be
closed.IOException
- on closing problemspublic String toString()
public static void main(String[] args)
args
- use "-?" if in doubt