Tools for sending messages to the server using Socket

Source: Internet
Author: User

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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.