I am working on project where i need to connect android app to server as of now i am to connect and able to send message from client to server but not server to client.
I tried with BufferedReader and PrintWriter but not help i am using socket programming.
Don't tag this question with duplicate as i saw lots of solution but those for one way client to server not to server to client as well.
I tried many solution but doesn't help any of them so i am guessing there will be something about android app to received message from server that is i am missing.
Need help badly i am out of option so post this question help me guys...
My server code is this
public class SimpleTextServer {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static OutputStreamWriter outputStreamWriter;
private static BufferedReader bufferedReader;
private static BufferedWriter bufferedWriter;
private static String message;
private static PrintWriter printwriter;
public static void main(String[] args) throws IOException, ClassNotFoundException {
try {
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
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);
printwriter = new PrintWriter(clientSocket.getOutputStream(), true);
printwriter.write("u r dere"+message+"\n"); // write the message to output stream
printwriter.flush();
inputStreamReader.close();
bufferedReader.close();
printwriter.close()
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
My android client code is this
public class SlimpleTextClientActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private BufferedReader inFromUser;
//private static ObjectInputStream printreader;
private static BufferedReader bufferedReader;
private static InputStreamReader inputStreamReader;
private EditText textField;
private Button button;
private String messsage;
private String message;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slimple_text_client);
Calendar c1 = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String strdate1 = sdf1.format(c1.getTime());
messsage=strdate1;
textField = (EditText) findViewById(R.id.editText1); // reference to the text field
button = (Button) findViewById(R.id.button1); // reference to the send button
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = messsage+textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("10.129.26.120", 4444); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
/*
inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
bufferedReader.ready();*/
//message = bufferedReader.readLine();
// System.out.println(message);
// TextView txtdate1 = (TextView) findViewById(R.id.textView1);
// txtdate1.setText(message);
printwriter.close();
bufferedReader.close();
client.close(); // closing the connection
} 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.slimple_text_client, menu);
return true;
}
}
0 comments:
Post a Comment