Public class SocketClient
{
Public boolean mClientFlag = false;
Private static Socket client;
Public SocketClient ()
{
Log. d ("WineStock", "WineStock SocketClient ()");
Client = new Socket ();
}
Public SocketClient (String HostName, int iPort)
{
Client = new Socket ();
SocketAddress socketAddress = new InetSocketAddress (HostName, iPort );
Try
{
Log. d ("WineStock", "WineStock SocketClient connect ");
Client. connect (socketAddress, 3000 );
}
Catch (IOException e ){
// TODO Auto-generated catch block
Log. d ("WineStock", "WineStock SocketClient IOException ");
} Catch (IllegalArgumentException e1 ){
// TODO: handle exception
Log. d ("WineStock", "WineStock SocketClient IllegalArgumentException ");
}
Return;
}
Public boolean SocketConnect (String HostName, int iPort)
{
SocketAddress socketAddress = new InetSocketAddress (HostName, iPort );
Try
{
Log. d ("WineStock", "WineStock SocketConnect connect ");
Client. connect (socketAddress, 3000 );
}
Catch (IOException e ){
// TODO Auto-generated catch block
Log. d ("WineStock", "WineStock SocketConnect IOException ");
} Catch (IllegalArgumentException e1 ){
// TODO: handle exception
Log. d ("WineStock", "WineStock SocketConnect IllegalArgumentException ");
}
Return true;
}
Public void SocketClose ()
{
Try {
Client. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
Log. d ("WineStock", "socket close error" + e. getMessage ());
}
}
// Establish a connection
Public static boolean SClient (SocketSendModel SendModel) throws UnknownHostException, IOException
{
Boolean bRet = false;
Try
{
If (client. isConnected ())
{
// Send the request
SendMessage (SendModel );
BRet = true;
}
}
Catch (UnknownHostException e)
{
Log. d ("WineStock", "socket SClienterror" + e. getMessage ());
}
Catch (IOException e1)
{
Log. d ("WineStock", "socket SClienterror" + e. getMessage ());
}
Return bRet;
}
// Send the request www.2cto.com
Private static void sendMessage (SocketSendModel model) throws IOException
{
// Boolean bRet = false;
Try
{
PrintWriter out = new PrintWriter (client. getOutputStream ());
Out. print (objToJsonString (model). replace ("\ n ",""));
Out. flush ();
// BRet = true;
}
Catch (IOException e)
{
}
Return;
}
// Receives server data
Public static String readMessage () throws IOException
{
String str = "";
Try
{
BufferedReader br = new BufferedReader (new InputStreamReader (client. getInputStream ()));
Str = br. readLine (). replace ("{", ""). replace ("}", ""). replace ("\"","");
}
Catch (IOException e)
{
}
Return str;
}
// Convert a Java Object to a Json Object
Private static String objToJsonString (Object obj ){
// Initialize the return value
String json = "str_empty ";
If (obj = null ){
Return json;
}
StringBuilder buff = new StringBuilder ();
Field fields [] = obj. getClass (). getFields ();
Try {
Buff. append ("[");
Buff. append ("{");
Int I = 0;
For (Field field: fields ){
If (I! = 0 ){
Buff. append (",");
}
Buff. append (field. getName ());
Buff. append (":");
Buff. append ("\"");
Buff. append (field. get (obj) = null? "": Field. get (obj ));
Buff. append ("\"");
I ++;
}
Buff. append ("}");
Buff. append ("]");
Json = buff. toString ();
} Catch (Exception e ){
Throw new RuntimeException ("cause:" + e. toString ());
}
Return json;
}
}
From weizhiai12's column