我怎么能处理我的插座

时间:2016-04-21 17:26:01

标签: android serversocket

当我运行它时,我的代码有问题,它连接到服务器并发送消息,但之后停止。任何建议

 public void onClick(View arg0)
           {  serverIP=Ip.getText().toString();
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       try {
                           clientSocket= new Socket(serverIP,serverPort);//making the socket connection
                           printWriter=new PrintWriter(clientSocket.getOutputStream());
                           String connect="connected to server";

                           printWriter.write(connect);
                           printWriter.flush();
                           printWriter.close();
                           clientSocket.close();


                           Toast.makeText(getApplicationContext(),"Connected to:"+serverIP+" on port:"+serverPort,Toast.LENGTH_LONG).show();
                       } catch (UnknownHostException e) {
                           e.printStackTrace();
                       }
                       catch(IOException e)
                       {
                           e.printStackTrace();
                       }
                   }
               }).start();

           }
       });

enter image description here

1 个答案:

答案 0 :(得分:0)

您无法在runnable的run()中调用Toast()。