Advertisement
Sebuahhobi98

android_socket

May 26th, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.  
  3.     private TextView mTextViewReplyFromServer;
  4.     private EditText mEditTextSendMessage;
  5.  
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.  
  11.         Button buttonSend = (Button) findViewById(R.id.btn_send);
  12.  
  13.         mEditTextSendMessage = (EditText) findViewById(R.id.edt_send_message);
  14.         mTextViewReplyFromServer = (TextView) findViewById(R.id.tv_reply_from_server);
  15.  
  16.         buttonSend.setOnClickListener(this);
  17.     }
  18.  
  19.     @Override
  20.     public void onClick(View v) {
  21.  
  22.         switch (v.getId()) {
  23.  
  24.             case R.id.btn_send:
  25.                 sendMessage(mEditTextSendMessage.getText().toString());
  26.                 break;
  27.         }
  28.     }
  29.  
  30.     private void sendMessage(final String msg) {
  31.  
  32.         final Handler handler = new Handler();
  33.         Thread thread = new Thread(new Runnable() {
  34.             @Override
  35.             public void run() {
  36.  
  37.                 try {
  38.                     //Replace below IP with the IP of that device in which server socket open.
  39.                     //If you change port then change the port number in the server side code also.
  40.                     Socket s = new Socket("ws://10.20.3.104/ws", 80);
  41.  
  42.                     OutputStream out = s.getOutputStream();
  43.  
  44.                     PrintWriter output = new PrintWriter(out);
  45.  
  46.                     output.println(msg);
  47.                     output.flush();
  48.                     BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
  49.                     final String st = input.readLine();
  50.  
  51.                     handler.post(new Runnable() {
  52.                         @Override
  53.                         public void run() {
  54.  
  55.                             String s = mTextViewReplyFromServer.getText().toString();
  56.                             if (st.trim().length() != 0)
  57.                                 mTextViewReplyFromServer.setText(s + "\nFrom Server : " + st);
  58.                         }
  59.                     });
  60.  
  61.                     output.close();
  62.                     out.close();
  63.                     s.close();
  64.                 } catch (IOException e) {
  65.                     e.printStackTrace();
  66.                 }
  67.             }
  68.         });
  69.  
  70.         thread.start();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement