Java 如何编写一个客户端套接字,它将在已经创建的后台进行侦听?

Java 如何编写一个客户端套接字,它将在已经创建的后台进行侦听?,java,android,sockets,client,Java,Android,Sockets,Client,首先,我是个新手,我有服务器/客户端套接字,服务器正在发送/接收数据,但我的客户端目前只在try/catch块中发送数据,我需要编写另一个方法来捕获传入的数据? 并将其保留在我原来的try/ctach*之外 任何帮助都会很好 public void onClick(View v) { setMyIp(ipaddress.getText().toString()); // myComs.sending_data(getMyIp()

首先,我是个新手,我有服务器/客户端套接字,服务器正在发送/接收数据,但我的客户端目前只在try/catch块中发送数据,我需要编写另一个方法来捕获传入的数据? 并将其保留在我原来的try/ctach*之外

任何帮助都会很好

public void onClick(View v)
         {
             setMyIp(ipaddress.getText().toString());
            // myComs.sending_data(getMyIp() , "Got connected");
            try
             {
                 InetAddress inet = InetAddress.getByName(getMyIp());
                 Socket s = new Socket(inet, 2000);
                 OutputStream o = s.getOutputStream();
                 PrintWriter p = new PrintWriter(o);
                 InputStream in = s.getInputStream();


                 p.println("You are connected");
                 p.flush();

                 readContacts();
                 //inboxConversations();
                 //outboxConversations();
                 readSms();


             }
             catch (UnknownHostException e) 
             {
                ipaddress.setText("Unknown host");
                   e.printStackTrace();
             }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         }


     });
这是我的代码,我需要在按钮外创建一个单独的侦听器

谢谢



您必须保持套接字在循环中打开,不断侦听任何要写入的数据。当然,在单独的线程/进程上执行此操作。实际上,在我看来,服务器端和客户端都应该在一个单独的线程上。您可以查找JavaNIO,但这里有一些代码只是为了让您开始,因为android有一个类,它可以在后台做一些事情,然后更新主UI,称为ASYNCHTASK。当然,您可以通过其他方式生成线程,这只是一种方便的方式: (注意,我还没有运行过这个程序,我只是编写了我将如何执行它,以便将其作为伪代码)

class ClientTask扩展AsyncTask//或任何您想要传入的内容
{
公共静态字符串ip=“10.0.2.1”;
公共静态int端口=5061;
插座;
公共数据输入流dis;
公共数据输出流dos;
公共字符串消息;
@凌驾
受保护的Void doInBackground(Void…参数){
/*设置套接字并打开一个流进行读取*/
试一试{
插座=新插座(ip,端口);
dis=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
}捕获(例外e){
i(“AsyncTank类”,“套接字在打开时遇到一些问题”);
}
/*这是你想要的东西,可以让你整天听插座*/
while(socket.isConnected()){
字符串mymessage=dis.readLine();//读取行块
/*对您的消息做些什么*/
publishProgress(mymessage);//将更新发布到主UI线程
}
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(字符串…消息){
Toast.makeText(this,messages,Toast.LENGTH_LONG).show();//宣布传入套接字的更新
}
}

--------如果在套接字因任何原因关闭后不需要更新主UI,只需执行相同的操作,但使用runnable或Thread类来创建和侦听

如果您需要一个客户机作为接收器,那么您已经为这种异步行为运行了一个线程,否则,如果您在主线程上运行,它将冻结您的应用程序

那么做下面的事情

在客户端的主方法中,启动一个接收方线程

Thread receiver_thread=new Thread(new Reciever());
reciever_thread.start();
这是接受者类

public class Receiver implements Runnable  
{
    public void run()  
    {
        While(true)
        {
            //check here if there is some data in your input stream so what              
               //you receive when server sends something to you

           //so that's it here  do your work with the incoming data
           //suggestion make an inner class to your main ui class so that data that comes here can be printed to those ui components.
         }
      }
}
希望它能帮助你

听起来像是你想要的。
public class Receiver implements Runnable  
{
    public void run()  
    {
        While(true)
        {
            //check here if there is some data in your input stream so what              
               //you receive when server sends something to you

           //so that's it here  do your work with the incoming data
           //suggestion make an inner class to your main ui class so that data that comes here can be printed to those ui components.
         }
      }
}