Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private TextView mTextViewReplyFromServer;
- private EditText mEditTextSendMessage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button buttonSend = (Button) findViewById(R.id.btn_send);
- mEditTextSendMessage = (EditText) findViewById(R.id.edt_send_message);
- mTextViewReplyFromServer = (TextView) findViewById(R.id.tv_reply_from_server);
- buttonSend.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_send:
- sendMessage(mEditTextSendMessage.getText().toString());
- break;
- }
- }
- private void sendMessage(final String msg) {
- final Handler handler = new Handler();
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- //Replace below IP with the IP of that device in which server socket open.
- //If you change port then change the port number in the server side code also.
- Socket s = new Socket("ws://10.20.3.104/ws", 80);
- OutputStream out = s.getOutputStream();
- PrintWriter output = new PrintWriter(out);
- output.println(msg);
- output.flush();
- BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
- final String st = input.readLine();
- handler.post(new Runnable() {
- @Override
- public void run() {
- String s = mTextViewReplyFromServer.getText().toString();
- if (st.trim().length() != 0)
- mTextViewReplyFromServer.setText(s + "\nFrom Server : " + st);
- }
- });
- output.close();
- out.close();
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- thread.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement