2013-03-26 90 views
0

我在android上遇到問題。我有一個應用程序詢問用戶本地IP地址(來自設備的接口)和另一個遠程地址。應用程序必須綁定到指定的本地地址並連接到遠程地址。很簡單,但事實上,綁定不起作用屁股我預計..綁定套接字Android

我加在清單文件以下權限:

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

的源代碼是以下之一:

public class MainActivity extends Activity 
{ 
String Tag = "TAG"; 
private int LOCAL_PORT = 4444; 
private int REMOTE_PORT = 80; 
private EditText LOCAL; 
private EditText REMOTE; 
private Button Connect; 
private TextView STATUS; 
private Context context = this; 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    LOCAL = (EditText) findViewById(R.id.editText1); 
    REMOTE = (EditText) findViewById(R.id.editText2); 
    Connect = (Button) findViewById(R.id.button1); 

    Connect.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) 
     { 
      BotleneckHandle WORK; 

      Toast.makeText(getApplicationContext(), "Proceeding to connect", 
       Toast.LENGTH_SHORT).show(); 
     /* 
      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(context, "Wrong parameters", 2000); 
      else 
     { 
      Toast.makeText(getApplicationContext(), "Proceeding to connect",Toast.LENGTH_SHORT).show(); 

     }*/ 

      WORK = new BotleneckHandle();  
      WORK.execute(); 
     } 
    }); 

    STATUS = (TextView) findViewById(R.id.textView1); 
} 


private class BotleneckHandle extends AsyncTask <Void , Void , String> 
{ 
    Socket Skt = null; 
    String ReturnStatemente = new String(); 
    SocketAddress localAddr = new InetSocketAddress(LOCAL.getText().toString(), LOCAL_PORT); 

    protected void onPreExecute(){ } 
    protected String doInBackground(Void... params) 
    { 

     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.makeText(getApplicationContext(), s,Toast.LENGTH_SHORT).show(); //Does not show anything due the fact that i didn't published it as an assync task update.. 

      //binding to a local address 
      Skt.bind(localAddr);   //cannot make the bind :-/ 

      //connecting to remote host 
      Skt=new Socket(REMOTE.getText().toString(), REMOTE_PORT); //if bind is comment, still does not work.. I bet 
      ReturnStatemente = "Connected"; 

     } 
     catch (UnknownHostException e) 
     { 
      e.printStackTrace(); 
      Toast.makeText(context, "Unknown remote host", 2000); 
      ReturnStatemente = "Not Connected"; 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace();   
      Toast.makeText(context, "Connection fail", 2000); 
      ReturnStatemente = "Not Connected"; 
     } 
     finally{try { 
      Skt.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }} 
     return ReturnStatemente; 

    } 

    protected void onPostExecute(String result) { STATUS.setText(" CONECTION STATUS == " + result); } 



    } 

}

我在做什麼錯誤的綁定?據我所知,當我尋找它的好...我錯過了什麼?

親切的問候

回答

1

你想你指定什麼之前調用一個變量綁定。所以它仍然是空的。這是行不通的。您需要先創建一個Socket實例,然後才能調用它的方法。

+0

哦男人..我怎麼可以做出這樣的錯誤..: -/.. – 2013-03-26 21:54:26