简体   繁体   English

套接字客户端Android不显示收到的消息

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

i have a problem on my code i've created an app that send and recieve a message to/from server by clicking a 我的代码有问题,我创建了一个应用程序,该应用程序通过单击一个向服务器发送消息或从服务器接收消息

button 纽扣

sending a message work very good but when recieving it doesn't show me anything i want the app to show me message during the communication with the server sending and recieving and showing messages because i just noticed that during the communication the app can't display anything (toast or changing text of TextView/EditText or anything) expect just recieving data on variables without display but i need the app to do a lot of things during the communication with server (showing toast and opening dialog boxes etc..) 发送消息的工作非常好,但是在接收到消息后却什么也没显示给我,我希望应用程序在与服务器通信期间向我显示消息。发送和接收并显示消息,因为我只是注意到在通信过程中该应用程序无法显示任何东西(吐司或更改TextView / EditText的文本或其他任何东西)都期望只接收变量的数据而无显示,但我需要应用程序在与服务器通信期间做很多事情(显示吐司和打开对话框等。)

the code onclick : 代码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();




}

and here the sever code : 这里是服务器代码:

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();
}

Maybe message from your server returns with delay(not in realtime). 也许来自您服务器的消息会延迟返回(不是实时的)。 Try make loop: 尝试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