I have created two android applications, a client and a server, utilizing TCP to communicate across devices. On the server, I have this code to listen for TCP communication:
private class StartServer implements Runnable {
public void run() {
getState(); // This just refreshes the server state
try {
serverSocket = new ServerSocket(5000);
appendLog("Server successfully listening ["+getLocalIpAddress() + ":5000]"); //appendLog function just uses runOnUiThread() to update the log textview
} catch (IOException e) {
appendLog("Could not listen on port: 5000");
//System.exit(-1);
}
BufferedReader in = null;
BufferedWriter out = null;
String input = null;
try {
clientSocket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
while (serverState) { //variable set by our getState() function
input = in.readLine();
appendLog("Received: " + input);
this.gotMessage(out, input);
}
in.close();
out.close();
} catch (IOException e) {
//appendLog(e.toString());
appendLog("Accept failed: 5000");
//System.exit(-1);
}
}
Which calls this function to actually parse the recieved messages:
private void gotMessage(BufferedWriter output, String msg) {
if (msg != null) {
String sString = msg;
int to_do = 0;
if (msg.matches("^(?i)(exit|quit|close)$")) {
appendLog("Exiting");
sString = "Goodbye";
to_do=1;
} else if (msg.matches("^(?i)(launch|run|open)\\s(.+)")) {
appendLog("Launching application");
sString = "Launching application";
} else if (msg.matches("^(?i)(turn off|server off|deactivate)$")) {
appendLog("Turning off server due to remote command.");
sString = "Turning off...";
to_do=2;
} else if (msg.matches("^(?i)(restart|reboot)$")) {
appendLog("Resetting server");
sString = "Rebooting now...";
to_do=3;
}
try {
output.write("S: " + sString);
output.newLine();
output.flush();
} catch (IOException e) {
appendLog("Cannot parse message");
}
switch(to_do) {
case 0:
break;
case 1:
System.exit(0);
break;
case 2:
serverOff();
break;
case 3:
serverOff();
serverOn();
break;
default:
break;
}
}
}
}
The thread itself for the server is started using Thread t = new Thread(new StartServer()); t.start()
Thread t = new Thread(new StartServer()); t.start()
. And to the best of my knowledge, this works fine. I can open up a terminal and telnet to the IP and port, and pass communication back and forth without error. But when I try to do the same from the client code below. I only get the first message, and anything else I pass in just dissappears into the void.
public void sendToServer(View v) {
try {
String ip = ((TextView)findViewById(R.id.ip_box)).getText().toString(); //User entered IP address
String to_send = ((EditText)findViewById(R.id.send_to_server)).getText().toString(); //User entered text to send
this.s = new Socket(ip, 5000);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
appendLog("Sending: " + to_send);
out.write(to_send);
out.newLine();
out.close();
} catch (Exception e) {
Log.d("error", e.toString());
}
}
Do you have line-end character in your to_send
string? if no, just add out.newLine()
to your client code:
appendLog("Sending: " + to_send);
out.write(to_send);
out.newLine();
You server code should look like this to support multiple clients:
// main server loop
while (serverIsActive) {
clientSocket = serverSocket.accept();
// spawn new thread for each client
ClientThread ct = new ClientThread(clientSocket);
ct.start();
}
ClientThread should work with client socket and have its own loop for reading lines. It should stop as soon as client closes socket:
class ClientThread {
...
public void run() {
....
while ((inputLine = in.readLine()) != null) {
// process client message
}
in.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.