android - 使用 tcp 套接字连接 android 模拟器

标签 android android-asynctask tcpsocket

以下是我的代码。编辑器:Eclipse,平台:Windows。

它是一个聊天应用程序,其中 2 个 android 模拟器通过 tcp 套接字连接。

UI 由发送按钮、 TextView 和文本框组成。

问题:只要我输入文本并点击发送,应用程序就会崩溃。

服务器端口是8000。 所以我的重定向是 redir add tcp:8081:8000 和 redir add tcp:8082:8000。

我不知道我的代码有什么问题。请建议我需要更改的内容。

public class HelloandroidActivity extends Activity 
{
/** Called when the activity is first created. */
public int serverport=8000;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   final EditText nameField = (EditText) findViewById(R.id.editText1);             
   final Button button2 = (Button) findViewById(R.id.button1);  
   Integer severport=8000;
   new Server().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,severport);
   button2.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
          final String name = nameField.getText().toString();
          final TextView tv = (TextView) findViewById(R.id.textView1);
          //tv.setText(name);

          String s=null;

        new Client().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,s);
     } 
          });// end onclicklis  

     }//end oncreate



 class Server extends AsyncTask <Integer, String, String>
{
public InetAddress byIpAsName ;
int r=0;
@Override
protected String doInBackground(Integer... serverport)  {
    //i[0]=serverport;
    Integer[] sp=serverport;
    BufferedReader in=null;
    ServerSocket s=null;
     r=sp[0];
    String cIn="";
    try {
        //byIpAsName = InetAddress.getByName("10.2.2.15");
        s=new ServerSocket(r);
    while(true)
    {
        Socket client = s.accept();
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String line=in.readLine();
        cIn=null;
        while(line!=null){cIn=cIn.concat(line);}

    }//while    

    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        s.close();
        in.close();
        } 
    catch (IOException e) {
        e.printStackTrace();
    }
    return cIn;


}//end inBackground
//@SuppressWarnings("null")
protected void onPostExecute(String... cIn)
{

  }//onpost execute

  }//server class
    public class Client extends AsyncTask<String, String, String>
    {
     PrintWriter out = null;
    BufferedReader in=null;
    String sIn=null;
     //Server s1=new Server();
    //int q=s1.r;
    TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() - 4);
    int q = Integer.parseInt(portStr);
   Socket socket;
   @Override
   protected String doInBackground(String... params) {
   try 
   {
     //q=8080;
     InetAddress byIpAsName1=InetAddress.getByName("10.0.2.2");
     socket = new Socket(byIpAsName1, q);
     out = new PrintWriter(socket.getOutputStream(), true);
     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     String line=in.readLine();
      sIn=null;
     while(line!=null){sIn=sIn.concat(line);}
    } 
    catch (IOException e) {
    e.printStackTrace();
    }//catch
   return sIn;
    }//in background
    protected void onPostExecute(String... sIn)
    {
    String c=null;
    final TextView tv = (TextView) findViewById(R.id.textView1);
   c=c.concat(sIn[0]);
   tv.setText(c);
    }
     }  

  }//main class

最佳答案

在你的 logcat 中,重要的是这一行:

03-16 23:12:23.434: E/AndroidRuntime(571): java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10040 nor current process has android.permission.READ_PHONE_STATE.

这表示为了运行您的代码,您需要 android manifest.xml 中的 READ_PHONE_STATE 权限。

将此行添加到 list 中,在 <application> 之外标记但在 <manifest> 内标签。

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

如果这不能解决问题,问题可能与 this answer 有关.

关于android - 使用 tcp 套接字连接 android 模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745288/

相关文章:

java - JNI 返回日期

java - UI 线程等待其他线程执行 - 不工作

android - ListView : Android 中第一行的默认选择

java - 如何同步两个异步任务?

c++ - 我的 Linux 应用程序端口在停止时被另一个服务占用

当程序被 kill 命令停止时关闭 tcp 套接字

c++ - Tcp Socket recv 函数不起作用 c++

android - 如何从 Google Play 控制台中提供的堆栈跟踪中调试 Flutter 应用程序的崩溃?

Android TextView 停止换行文本

android - 无法从 View 转换为相对布局