Package com. yanek. util. socket;
Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. ByteArrayOutputStream;
Import java. io. DataInputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java. io. PrintWriter;
Import java. io. UnsupportedEncodingException;
Import java.net. Socket;
Import java.net. URLEncoder;
Import org. apache. log4j. Logger;
/**
* The Socket Client sends messages to the server.
*
*
*/
Public class SocketClient {
Private static Logger logger = Logger. getLogger (SocketClient. class
. GetName ());
Private static final int MAX_TIMEOUT = 10;
Private SocketClient (){
}
/**
* Send messages to the server
*
* @ Param host
* Host or IP address
* @ Param port
* Port
* @ Param timeout
* Timeout, in seconds
* @ Param content
* Sent content
*/
Public static void send (String host, int port, int timeout, String content ){
Socket s = null;
PrintWriter out = null;
Try {
S = new Socket (host, port );
S
. SetSoTimeout (timeout> MAX_TIMEOUT? MAX_TIMEOUT
: Timeout) * 1000 );
Out = new PrintWriter (s. getOutputStream ());
Out. write (content );
Out. flush ();
} Catch (Exception e ){
Logger. error (e );
} Finally {
If (s! = Null)
Try {
S. close ();
} Catch (IOException e ){
}
If (out! = Null)
Out. close ();
S = null;
Out = null;
}
}
/**
* Send communication commands to SocketServer and obtain the reply data
*
* @ Param host name or IP address
* @ Param port
* @ Param timeout (seconds)
* @ Param content command content
* @ Return
*/
Public static String sendAndGetReply (String host, int port, int timeout,
String content ){
String encode = "UTF-8 ";
Socket s = null;
BufferedReader in = null;
PrintWriter out = null;
String line = null;
Try {
Content = urlencoder. encode (content, encode );
S = new socket (host, Port );
S
. Setsotimeout (timeout> max_timeout? Max_timeout
: Timeout) * 1000 );
In = new bufferedreader (New inputstreamreader (S. getinputstream ()));
Out = new printwriter (New bufferedwriter (New outputstreamwriter (S
. Getoutputstream (), true );
Out. println (content );
Line = in. Readline ();
} Catch (exception e ){
Logger. Error (E );
} Finally {
If (s! = NULL)
Try {
S. close ();
} Catch (IOException e ){
}
If (out! = Null)
Out. close ();
If (in! = Null)
Try {
In. close ();
} Catch (IOException e ){
}
S = null;
Out = null;
In = null;
}
Try {
Line = URLEncoder. encode (line, encode );
} Catch (UnsupportedEncodingException e ){
Logger. error (e );
}
Return line;
}
/**
* Send communication commands to SocketServer without synchronously replying messages
*
* @ Param host name or IP address
* @ Param port
* @ Param timeout (seconds)
* @ Param content command content
* @ Return
*/
Public static void sendAndNoReply (String host, int port, int timeout,
String content ){
String encode = "UTF-8 ";
Socket s = null;
PrintWriter out = null;
Try {
Content = URLEncoder. encode (content, encode );
S = new Socket (host, port );
S
. SetSoTimeout (timeout> MAX_TIMEOUT? MAX_TIMEOUT
: Timeout) * 1000 );
Out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (s
. GetOutputStream (), true );
Out. println (content );
} Catch (Exception e ){
Logger. error (e );
} Finally {
If (s! = Null)
Try {
S. close ();
} Catch (IOException e ){
}
If (out! = Null)
Out. close ();
S = null;
Out = NULL;
}
}
}