Client/Server Example: Editable Message Sent From Server

Posted on

In previouse instance of “Bi-directional communication betwixt Client too Server, using ServerSocket, Socket, DataInputStream too DataOutputStream” too “Prevent programme blocked past times DataInputStream.readUTF()“, the server e’er post a fixed welcome message to client. It’s modified to add together a EditText for user to motion into championship to live sent.


Server side code:

package com.example.androidserversocket;  import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Enumeration;  import android.os.Bundle; import android.app.Activity; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView;  populace course of written report MainActivity extends Activity {    EditText textTitle;   TextView info, infoip, msg;  String message = "";  ServerSocket serverSocket;    CheckBox optWelcome;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   information = (TextView) findViewById(R.id.info);   infoip = (TextView) findViewById(R.id.infoip);   msg = (TextView) findViewById(R.id.msg);      optWelcome = (CheckBox)findViewById(R.id.optwelcome);    infoip.setText(getIpAddress());    Thread socketServerThread = novel Thread(new SocketServerThread());   socketServerThread.start();      textTitle = (EditText)findViewById(R.id.title);  }   @Override  protected void onDestroy() {   super.onDestroy();    if (serverSocket != null) {    endeavour {     serverSocket.close();    } grab (IOException e) {     // TODO Auto-generated grab block     e.printStackTrace();    }   }  }   mortal course of written report SocketServerThread extends Thread {    static terminal int SocketServerPORT = 8080;   int count = 0;    @Override   populace void run() {    Socket socket = null;    DataInputStream dataInputStream = null;    DataOutputStream dataOutputStream = null;     endeavour {     serverSocket = novel ServerSocket(SocketServerPORT);     MainActivity.this.runOnUiThread(new Runnable() {       @Override      populace void run() {       info.setText("I'm waiting here: "         + serverSocket.getLocalPort());      }     });      piece (true) {      socket = serverSocket.accept();      dataInputStream = novel DataInputStream(        socket.getInputStream());      dataOutputStream = novel DataOutputStream(        socket.getOutputStream());       String messageFromClient = "";            //Check available() earlier readUTF(),      //to preclude programme blocked if dataInputStream is empty      if(dataInputStream.available()>0){       messageFromClient = dataInputStream.readUTF();      }            count++;      message += "#" + count + " from " + socket.getInetAddress()        + ":" + socket.getPort() + "\n"        + "Msg from client: " + messageFromClient + "\n";       MainActivity.this.runOnUiThread(new Runnable() {        @Override       populace void run() {        msg.setText(message);       }      });            if(optWelcome.isChecked()){        String msgReply = count + ": " + textTitle.getText().toString();       dataOutputStream.writeUTF(msgReply);      }      }    } grab (IOException e) {     // TODO Auto-generated grab block     e.printStackTrace();     terminal String errMsg = e.toString();     MainActivity.this.runOnUiThread(new Runnable() {       @Override      populace void run() {       msg.setText(errMsg);      }     });         } lastly {     if (socket != null) {      endeavour {       socket.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }      if (dataInputStream != null) {      endeavour {       dataInputStream.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }      if (dataOutputStream != null) {      endeavour {       dataOutputStream.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }    }   }   }   mortal String getIpAddress() {   String ip = "";   endeavour {    Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface      .getNetworkInterfaces();    piece (enumNetworkInterfaces.hasMoreElements()) {     NetworkInterface networkInterface = enumNetworkInterfaces       .nextElement();     Enumeration<InetAddress> enumInetAddress = networkInterface       .getInetAddresses();     piece (enumInetAddress.hasMoreElements()) {      InetAddress inetAddress = enumInetAddress.nextElement();       if (inetAddress.isSiteLocalAddress()) {       ip += "SiteLocalAddress: "         + inetAddress.getHostAddress() + "\n";      }      }     }    } grab (SocketException e) {    // TODO Auto-generated grab block    e.printStackTrace();    ip += "Something Wrong! " + e.toString() + "\n";   }    supply ip;  } } 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />          <EditText         android:id="@+id/title"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <TextView         android:id="@+id/info"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/infoip"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />          <CheckBox         android:id="@+id/optwelcome"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:checked="true"         android:text="Send Welcome when connect" />      <ScrollView         android:layout_width="match_parent"         android:layout_height="match_parent" >          <TextView             android:id="@+id/msg"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />     </ScrollView>  </LinearLayout> 

Client side, mention to previous instance “Prevent programme blocked past times DataInputStream.readUTF()“.

directional communication betwixt Client too Server Client/Server example: editable message sent from serverDownload the files.