This is the first time i’m asking a question here at stack overflow so bear with me.
I’ll try to keep this brief.
I’m writing a simple client socket android application.
The server I’m connecting to has a chat robot named Alice.
I have managed to connect to the server and I receive the message “Hello from alice” but then the thread seems to stop because I’m not receiving any more messages.
Here is some code:
@Override
public void run() {
try
{
while (true)
{
String _input = _rd.readLine();
if (_input != null)
{
_field.append("Alice : "+ _input+"\n");
}
else
{
_field.append("null");
}
sleep(50);
}
} catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
As you can see I have attempted to troubleshoot my code by appending “null” even if I don’t receive a message, however this is neither shown in my EditText.
I call .start() from my main Activity class when the user presses a button.
@Override
public void onClick(View arg0) {
setContentView(R.layout.second);
InitiateSecondFrame();
_cs = new ClientSocket();
String _check = _cs.EstablishConnection(_host.getText().toString(),
Integer.parseInt(_port.getText().toString()),
(EditText)findViewById(R.id.historyField));
_cs.start();
Toast.makeText(getApplicationContext(), _check, 100).show();
SaveHostPort(_host.getText().toString(), _port.getText().toString());
}
});
The ClientSocket class is extended by Thread and implements Runnable, I’ve also tried just extending or implementing.
I do not get any error message at all.
I hope the information I have given is enough for you to realize what’s wrong.
Remember I’m new at multithreading and sockets so I might have missed something fundamental.
is your problem here. It will block and wait forever for input from the remote system, and will only return the content received when it detects a newline character from the remote system.