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にはバックグラウンドで処理を実行し、その後ASYNCHTASKと呼ばれるメインUIを更新できるクラスがあるため、開始するためのコードをいくつか示します。もちろん、便利な方法を示すだけで、他の方法でスレッドを生成することもできます:

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

クライアントがレシーバーとして機能する必要がある場合は、この非同期動作のためにスレッドを実行しました。それ以外の場合、メイン スレッドで実行するとアプリケーションがフリーズします。

そこで、次のことを行います

クライアントのメイン メソッドでレシーバー スレッドを開始します

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 に答える