We are developing an android application.It basically involves client server communication.Client is android phone and server is java.Client and server communication takes place.It is expected that client sends a message to the server and server responds back to the client.But in reality client is able to send the message to the server but server is unable or sends an incorrect response.I don't understand why is it happening. Here is the client code(android):
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
public TextView accept;
private Button button;
private String message;
private static BufferedReader bufferedReader;
public static InputStreamReader inputStreamReader;
String response;
DataInputStream dis;
static String Extra_message = "hkrw.clientside";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textField = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
this.accept = (TextView) findViewById(R.id.accept);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = textField.getText().toString();
textField.setText("");
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
accept.setText(response);
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
DataOutputStream dataOutputStream = null;
DataInputStream smalldataInputStream = null;
try {
client = new Socket("192.168.1.6", 4446);
printwriter = new PrintWriter(client.getOutputStream(), true);
inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
printwriter.write(message);
// smalldataInputStream = new DataInputStream(client.getInputStream());
printwriter.flush();
printwriter.close();
//dis= new DataInputStream(client.getInputStream());
//response=dis.readUTF();
response = bufferedReader.toString();
client.getInputStream();
// client.shutdownInput();
// client.shutdownOutput();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the code of the new activity which I have created in client side.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class respond extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_respond);
Intent intent=getIntent();
String message = intent.getStringExtra(MainActivity.Extra_message);
TextView response;
response= (TextView) findViewById(R.id.textView1);
response.setText(message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_respond, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the server code in java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.DataOutputStream;
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
private static PrintWriter printwriter ;
static DataOutputStream dos;
public static void main(String[] args) {
String w="Hello World";
try {
serverSocket = new ServerSocket(4446); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4446");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
dos=new DataOutputStream(clientSocket.getOutputStream());
dos.writeUTF(w);
clientSocket.close();
System.out.println(dos);
inputStreamReader.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}