Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用tcp套接字连接android模拟器_Android_Android Asynctask_Tcpsocket - Fatal编程技术网

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

使用tcp套接字连接android模拟器,android,android-asynctask,tcpsocket,Android,Android Asynctask,Tcpsocket,下面是我的代码。编辑器:Eclipse,平台:Windows 它是一个聊天应用程序,两个android模拟器通过tcp套接字连接 用户界面由发送按钮、文本视图和文本框组成 问题:只要我输入文本并点击send,应用程序就会崩溃 服务器端口是8000。 所以我的重定向是redir add tcp:8081:8000和redir add tcp:8082:8000 我不知道我的代码出了什么问题。请告诉我一些我需要改变的事情 public class HelloandroidActivity exten

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

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

用户界面由发送按钮、文本视图和文本框组成

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

服务器端口是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
公共类HelloandroidActivity扩展活动
{
/**在首次创建活动时调用*/
公共int服务器端口=8000;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最终EditText名称字段=(EditText)findViewById(R.id.editText1);
最终按钮button2=(按钮)findViewById(R.id.button1);
整数端口=8000;
新服务器().executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR,服务器端口);
button2.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
最终字符串名称=nameField.getText().toString();
最终文本视图电视=(文本视图)findViewById(R.id.textView1);
//tv.setText(名称);
字符串s=null;
新客户端().executeOnExecutor(异步任务.THREAD\u POOL\u EXECUTOR,s);
} 
});//结束onclicklis
}//结束一次创建
类服务器扩展异步任务
{
公共广播;
int r=0;
@凌驾
受保护字符串doInBackground(整数…服务器端口){
//i[0]=服务器端口;
整数[]sp=serverport;
BufferedReader in=null;
servers=null;
r=sp[0];
字符串cIn=“”;
试一试{
//byIpAsName=InetAddress.getByName(“10.2.2.15”);
s=新服务器套接字(r);
while(true)
{
套接字客户端=s.accept();
in=new BufferedReader(新的InputStreamReader(client.getInputStream());
String line=in.readLine();
cIn=null;
while(line!=null){cIn=cIn.concat(line);}
}//当
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
s、 close();
in.close();
} 
捕获(IOE异常){
e、 printStackTrace();
}
返回cIn;
}//端部内背景
//@抑制警告(“空”)
受保护的void onPostExecute(字符串…cIn)
{
}//事后执行
}//服务器类
公共类客户端扩展异步任务
{
PrintWriter out=null;
BufferedReader in=null;
字符串sIn=null;
//服务器s1=新服务器();
//int q=s1.r;
TelephonyManager tel=(TelephonyManager)getSystemService(Context.TELEPHONY_服务);
字符串portStr=tel.getLine1Number().substring(tel.getLine1Number().length()-4);
intq=Integer.parseInt(portStr);
插座;
@凌驾
受保护的字符串doInBackground(字符串…参数){
尝试
{
//q=8080;
InetAddress byIpAsName1=InetAddress.getByName(“10.0.2.2”);
插座=新插座(按iPasName1,q);
out=新的PrintWriter(socket.getOutputStream(),true);
in=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
String line=in.readLine();
sIn=null;
while(line!=null){sIn=sIn.concat(line);}
} 
捕获(IOE异常){
e、 printStackTrace();
}//抓住
归罪;
}//背景
受保护的void onPostExecute(字符串…sIn)
{
字符串c=null;
最终文本视图电视=(文本视图)findViewById(R.id.textView1);
c=c.concat(sIn[0]);
tv.setText(c);
}
}  
}//主类

从您的日志中,重要的是这一行:

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权限


将这一行添加到清单中,在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权限


将这一行添加到清单中,在

之外。您能发布错误的日志吗?@John,谢谢您的回复。。下面的链接是日志。你能发布错误的日志吗?@John,谢谢你的回复。。下面的链接是日志如果我的答案是正确的,不要忘记点击绿色复选标记接受它(你会得到代表)!此外,如果您认为某个答案或问题有帮助,也可以通过单击左侧的向上箭头向上投票。欢迎来到StackOverflow!嗨,约翰,这是新的日志,我不知道那里到底发生了什么,文件相当大。。如果你发现任何值得注意的东西,请查看并告诉我。让我知道。谢谢@Nanda,我没有访问google文档的权限,但是在任何情况下,最好是问一个新问题,然后将logcat复制/粘贴到代码块中。如果我的答案是正确的,请不要忘记通过单击绿色复选标记接受它(您将获得rep)!此外,如果您认为某个答案或问题有帮助,也可以通过单击左侧的向上箭头向上投票。欢迎来到StackOverflow!嗨,约翰,这是新的日志,我不知道那里到底发生了什么,文件相当大。。如果你发现任何值得注意的东西,请查看并告诉我。让我知道。谢谢@Nanda,我没有访问google文档的权限,但无论如何,最好是问一个新问题,然后将logcat复制/粘贴到代码块中。