Here i put some code, connection establish but whenever client wants to sent some data to server data not sent if client dis-connect data as soon as reaches to server how to fix that problem.
what i want : if connection establish and connection not close.
public class Server extends AppCompatActivity {
private ServerSocket serverSocket;
Handler UIHandler;
Thread Thread1 = null;
EditText edText;
public static final int SERVERPORT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
getSupportActionBar().setTitle("Server");
edText = (EditText) findViewById(R.id.editText);
UIHandler = new Handler();
this.Thread1 = new Thread(new Thread1());
this.Thread1.start();
}
class Thread1 implements Runnable {
InetAddress addr;
@Override
public void run() {
Socket socket = null;
try {
addr = InetAddress.getByName(getLocalIpAddress());
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
Thread2 commThread = new Thread2(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String getLocalIpAddress() throws Exception {
String resultIpv6 = "";
String resultIpv4 = "";
for (Enumeration en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements(); ) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if (inetAddress instanceof Inet4Address) {
resultIpv4 = inetAddress.getHostAddress().toString();
} else if (inetAddress instanceof Inet6Address) {
resultIpv6 = inetAddress.getHostAddress().toString();
}
}
}
}
return ((resultIpv4.length() > 0) ? resultIpv4 : resultIpv6);
}
private class Thread2 implements Runnable {
Socket clientSocket;
BufferedReader input;
String read;
public Thread2(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
read = this.input.readLine();
if (read != null) {
Server.this.runOnUiThread(new Runnable() {
@Override
public void run() {
edText.setText(edText.getText().toString() + "Client says: " + read + "\n");
Log.i("hhFULL", edText.getText().toString());
}
});
} else {
Thread1 = new Thread(new Thread1());
Thread1.start();
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
This is probably because the client you're using is sending a message without a \n or \r in the end. Below code that reads a line from inputStream waits for a newline character infinitely:-
read = this.input.readLine();