簡體   English   中英

套接字客戶端Android不顯示收到的消息

[英]Socket client android doesn't show recieved message

我的代碼有問題,我創建了一個應用程序,該應用程序通過單擊一個向服務器發送消息或從服務器接收消息

紐扣

發送消息的工作非常好,但是在接收到消息后卻什么也沒顯示給我,我希望應用程序在與服務器通信期間向我顯示消息。發送和接收並顯示消息,因為我只是注意到在通信過程中該應用程序無法顯示任何東西(吐司或更改TextView / EditText的文本或其他任何東西)都期望只接收變量的數據而無顯示,但我需要應用程序在與服務器通信期間做很多事情(顯示吐司和打開對話框等。)

代碼onclick:

EditText nom ; //nom= (EditText) findViewById(R.id.user); is in Oncreate methode

String name,check;
String check;
 public void click(View view) {


    name=nom.getText().toString();



    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {

            Socket s;


            try {

                s = new Socket(ipadresse,Integer.parseInt(por)); 
//those 2 (ip,por) are in methode (oncreate)

                DataOutputStream dos=new DataOutputStream(s.getOutputStream());



                //emission just du nom (un exemple)
                dos.writeUTF(String.valueOf(name));
                //sending message

                //recieving a message :



                DataInputStream dis=new DataInputStream(s.getInputStream());
                check=dis.readUTF();


                //i've tried this command but not work :/ 
                //BufferedReader read=new BufferedReader(
                //new InputStreamReader(s.getInputStream()));

                //check= read.readLine();

                Log.i("message", "message recieved : "+check);

//logcat show me that the message recieved but toast doesn't 
//( in fact i didn't want toast i want to show a dialog box 
//but as a simple example i used toast)

                Toast.makeText(login.this, check, Toast.LENGTH_SHORT).show();



                dos.flush();

                //dis.close();
                dos.close();
                s.close();



            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    });


    t.start();




}

這里是服務器代碼:

public static void main(String[] args) {

    Thread t;


        t = new Thread(new Runnable() {
        @Override
        public void run() {
           try {
                    ServerSocket s = new ServerSocket(8080);
                    System.out.println("connecting  ... ");
                    System.out.println("-------------------");


                        while(true) 
                        {
                            Socket s1= s.accept(); 

                           DataInputStream dis=new DataInputStream(s1.getInputStream());

                           DataOutputStream dos=new DataOutputStream(s1.getOutputStream());


                            String message;





                              message=dis.readUTF();

                           System.out.println(message);   


                           //this message does't displayed in the app (i've edited the program just to simplify it)    
                           dos.writeUTF("hello");



                            dis.close();
                            s1.close();
                        }


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        });
    t.start();
}

也許來自您服務器的消息會延遲返回(不是實時的)。 嘗試make循環:

boolean getmessage = false;
    while(!getmessage){
        try{
            check=dis.readUTF();
        }
        catch(Exception e){
            Log.d("dis", e.getMessage());
        }
        if(check != null){
            getmessage = true;
            makeToast("23");
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM