0

首先,我对此很陌生,我有服务器/客户端套接字在运行,服务器正在发送/接收数据,但我的客户端目前仅在 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();
            }

         }


     });

这是我的代码,我需要在按钮之外创建一个单独的侦听器任何想法

谢谢


4

2 回答 2

1

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

class ClientTask extends AsyncTask<Void, Void, Void> // or whatever you want to pass in
{

        public static String ip = "10.0.2.1";
        public static int port = 5061;
        Socket socket;
        public DataInputStream dis;
        public DataOutputStream dos;
        public String message;

        @Override
        protected Void doInBackground(Void... params) {


/* set up our socket and open a stream to read */

                 try {
                socket = new Socket(ip, port);
        dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));           

            } catch (Exception e) {
                Log.i("AsyncTank class", "Socket has some trouble opening");
            }

/*heres the stuff your looking for to listen to a socket all day long*/

while(socket.isConnected()){
                String mymessage=dis.readLine(); //readline blocks
/* do something with your message */    
publishProgress(mymessage); //publishes update to main UI thread            
                        }
                }

            }
            return null;
        }



@Override
    protected void onProgressUpdate(String... messages) {

     Toast.makeText(this, messages,Toast.LENGTH_LONG).show(); //announce updates on incoming socket

}


}

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

于 2013-04-20T22:06:58.307 回答
0

如果您需要客户端充当接收者,则您已经为此异步行为运行了一个线程,否则如果您在主线程上执行此操作,它将冻结您的应用程序。

所以做以下事情

在您的客户端的 main 方法中启动一个接收器线程

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.
         }
      }
}

希望对你有帮助

于 2013-04-21T06:47:53.597 回答