Friday 15 March 2013

Bind Socket Android -


One problem I Android is I have an application which local IP address (device interface) and other remote address Asks the user for The application must be linked to the specified local address and connect to the remote address. Quite simple, but, in fact, the dam ass I hope ..

I do not work on adding the following permissions on the manifest file:

   & Lt; Usage-permission Android: name = "android.permission.ACCESS_NETWORK_STATE" />   

The source code is as follows:

  The public class increases the main activity activity {string tag = "tag"; Personal difference LOCAL_PORT = 4444; Private Inc REMOTE_PORT = 80; Private editing text is local; Private Editing Text Remote; Private Button Connect; Private textview status; Private context reference = this; Override public null on @reate (bundle saved instainstate) {super.naught (savedinstenstate); SetContentView (R.layout.activity_main); Local = (Edit Text) Find VVBID (RID Edit Text 1); Find Remote = (Editing Text) VVBIID (RID Editlet 2); Connect = (button) FindById (R.id.button1); Connect.setOnClickListener (New View.OnClickListener () {public void onClick (View v) {BotleneckHandle work; Toast.makeText (getApplicationContext (), "action", Toast.LENGTH_SHORT) .show (to connect); / * if (local .getText (). toString () == null || REMOTE.getText (). toString () == null || LOCAL.getText (). toString (). equals ( "") || REMOTE.getText ( ). toString (). equals ( "") || LOCAL.getText (). toString (). equals ( "") || REMOTE.getText (). toString (). equals ( "")) Toast.makeText ( reference, "wrong parameter", 2000); else {Toast.makeText (getApplicationContext (), "Connecting to proceed", Toast.LENGTH_SHORT) .show ();} * / function = new BotleneckHandle (); wORK. Execute ();}}); STATUS = (TextView) findViewById (R.id.textView1); } Private class BotleneckHandle AsyncTask and lt; Zero, zero, string & gt; {Socket Skeet = Faucet; String returnstatement = new string (); Socket Attendor LocalEditor = New Inset Socket Attendor (Local.Get Text). ToString (), LOCAL_PORT); Protected void onPreExecute () {} Safe string doInBackground (Zero ... parameter) {try {string s = new string ()). S + = "Local =" + LOCAL.getText () toString () + ":" + LOCAL_PORT + "remote =" + REMOTE.getText () toString () + ":". + REMOTE_PORT; // So far so good, confirmed by debug toast. Make Text (getApplicationContext (), S, Toast.lNGTH_SHORT). Show (); // Because of this fact, nothing has been shown that I have not published it as an asynchronous work update .. // binding for local address Skt.bind (localAddr); // can not force: - / / / remote host skt = connect to new socket (REMOTE.getText (.) ToString (), REMOTE_PORT); // If the tie is a comment, still does not work .. I bet RestStatemente = "connected"; } Hold (unknownhostexception e) {e.printStackTrace (); Toast.makeText (Reference, "Unknown Remote Host", 2000); Return Stattament = "Not Connected"; } Hold (IOException e) {e.printStackTrace (); Toast. Make Text (Reference, "Connection Failed", 2000); Return Stattament = "Not Connected"; } Finally {try {Skt.close (); } Grip (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace (); }} Return Return Static; } Secure zero (string result) on PasteXsect {STATUS.setText ("CONCATION STATUS ==" + result); }}   

}

Am I doing wrong on the dam? As far as I am watching, and as I have searched for its good .. Did I miss something?

Merciful

Before you specify it anything on a variable Binding is trying to call. So still its weakness is not going to work before you can call methods on it, you have to make an example of the socket.

No comments:

Post a Comment