Nilesh Indalkar Nilesh Indalkar - 2 months ago 6
Android Question

Server is not able to receive the text from client java

I am creating an app in which client can send some text to server on button click.I want to print that text on console server side,but whenever client side button is pressed nothing happens.

and after sometimes i got this line on console

android.support.v7.widget.AppCompatEditText@b2253390
.
I am not getting why this is happening ?

SERVER CODE :

public static ServerSocket server = null;
public static Socket client = null;

public static void main(String []arg)throws IOException{
try {
server = new ServerSocket(8002);
System.out.println("Server Started...............");
while(true){
client = server.accept();
DataInputStream in = new DataInputStream(client.getInputStream());
String msg = in.readLine();
System.out.println("and: "+msg);
}
}catch(IOException r){
System.out.println("error :"+r.getMessage());
}
}


CLIENT CODE:

public void send(){
send.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Socket cs = null;
try {
cs = new Socket("192.168.1.100", 8002);
DataOutputStream out = new DataOutputStream(cs.getOutputStream());
out.writeBytes(text.toString());
} catch (IOException e) {
Toast.makeText(KeyboardActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
);
}

Answer

It is because text is an EditText.

I think you actually meant,

out.writeBytes(text.getText().toString());

Instead of,

out.writeBytes(text.toString());