android - 绑定(bind)套接字安卓

标签 android sockets bind ipv4

我在安卓上有问题。我有一个应用程序要求用户提供本地 IP 地址(来自设备的界面)和另一个远程地址。应用程序必须绑定(bind)到指定的本地地址并连接到远程地址。很简单,但实际上,绑定(bind)并没有像我预期的那样工作。

我在 list 文件上添加了以下权限:

<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);    }



  } 

我在绑定(bind)时做错了什么?据我所知,当我寻找它的好处时..我错过了什么吗?

亲切的问候

最佳答案

您试图在为变量赋值之前调用绑定(bind)变量。所以它仍然是空的。那是行不通的。您需要先创建一个 Socket 实例,然后才能对其调用方法。

关于android - 绑定(bind)套接字安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15646894/

相关文章:

java - Android EditText - 显示顶部图标时文本移动

sockets - 使用 80、443 之类的 TCP 的套接字是否令人难过?

linux-kernel - 如果copy_to_user()失败,将返回值传递给setsockopts()调用者吗?

c++ - boost::asio::async_read 绑定(bind)编译错误

c++ - boost::bind 成员函数 - 部分应用程序链接

android - 在android : 4. 3的MediaMuxer或ffmpeg中的视频上添加水印位图

java - 同步数据到服务器android

c++ - 如何使用 std::bind 将成员函数设置为回调

java - 更改层次结构父级

c++ - 当客户端连接很少且间隔很远时,服务器监听的明智方法是什么(在Linux上使用套接字编程)