Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.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
Android 客户及;服务器在同一套接字上读写_Android_Sockets_Tcp - Fatal编程技术网

Android 客户及;服务器在同一套接字上读写

Android 客户及;服务器在同一套接字上读写,android,sockets,tcp,Android,Sockets,Tcp,我有一个android客户端和一个java服务器,它们使用相同的套接字互相发送字符串,但实际上不起作用,我不知道为什么 无论如何,我只需要使用一个插座 以下是客户端和服务器的代码: public class CliActivity extends Activity implements OnClickListener { Button b; TextView tv; BufferedReader in = null; PrintStream out = null;

我有一个android客户端和一个java服务器,它们使用相同的套接字互相发送字符串,但实际上不起作用,我不知道为什么

无论如何,我只需要使用一个插座

以下是客户端和服务器的代码:

public class CliActivity extends Activity implements OnClickListener {  
   Button b;
   TextView tv;
   BufferedReader in = null;
   PrintStream out = null;
   Socket client = null;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      b =(Button)findViewById(R.id.button1);
      tv =(TextView)findViewById(R.id.textView1);

      b.setOnClickListener(this);
   }

   public void onClick(View v) {        
      try {
         client = new Socket("192.168.0.3",9000);
         out = new PrintStream(client.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(client.getInputStream()));
         out.write(("How are you?").getBytes());
         out.close();
         String message = in.readLine();
         tv.setText(message);           
      } catch (UnknownHostException e) {
           e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } 
   }
}
服务器:

public class server {   
   public static void main(String[] args) {

      BufferedReader in = null;
      PrintStream out = null;
      Socket client = null; 
         try {
            ServerSocket server = new ServerSocket(9000);
            client = server.accept();
            out = new PrintStream(client.getOutputStream(), true);
            in = new BufferedReader(new     InputStreamReader(client.getInputStream()));
            String message = in.readLine();
            System.out.println(message);
            out.write(("fine thanx").getBytes());
            out.close();            
        } catch (IOException e) {
            e.printStackTrace();
        }       
   }
}

有什么建议吗?

两台控制台中都有错误吗?是的,但这取决于输入、输出缓冲区的关闭情况……我认为问题就在那里。我试着把这两件事分开做,首先是服务器从电话接收,然后是电话从服务器接收,所有的事情都是这样(电话客户端和pc服务器),一切都很完美。但套接字是全双工的,所以我必须能够在同一个连接中执行这两个操作。我想我应该知道如何正确地使用输入和输出流的闭包没有一个可以帮助我?很高兴听到这个消息,很抱歉我不能提供更多帮助。你应该把这条评论放在答案框中,并将其标记为未来观众的答案。将你的解决方案作为答案。