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