Socket Communication in Android

Source: Internet
Author: User
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;

}

}

Related Article

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.