Reputation: 1
I made a simple ServerSocket application on my android device I use telnet to connect and it's works fine (sended messages are displayed in my logcat window). Probl;em is when i try to make a connection from php page (I'm also using PHP sockets). Android socket accepts a first connection and then is something strange - sometimes he read a messages sended from PHP and display them in logcat but usually it accepts only first request and trhen... nothing
ANDROID SIDE Code:
public class TestSocketActivity extends Activity {
private ServerSocket ss;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setSocket();
}
public void setSocket() {
try {
ss = new ServerSocket(7000);
System.out.println("Started");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Accept failed: 4444");
e.printStackTrace();
}
Socket clientSocket = null;
try {
clientSocket = ss.accept();
String s = clientSocket.getRemoteSocketAddress().toString();
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("SS error");
e.printStackTrace();
}
try {
BufferedReader in =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
Toast.makeText(getApplicationContext(),"test",1).show();
System.out.println("in: "+inputLine);
}
ss.close();
setSocket();
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("error");
e.printStackTrace();
}
}
}
And this is simple PHP request:
<?php
error_reporting(E_ALL);
$address = '192.168.0.180';
$port = 7000;
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_connect($socket, $address, $port);
$in = mktime();
socket_write($socket, $in, strlen($in));
socket_close($socket);
?>
... that's all Maybe there is some easier communication (I need to connect to android device from PHP everytime I want so PHP can't wait for a request from Android).
Thanks for help.
Upvotes: 0
Views: 3331
Reputation: 5410
You do realize that your Android ServerSocket should maintain an "infinite" loop that waits for incoming connections, processes a request by reading it until reaching the end of stream and then goes back to the state of waiting for another incoming request.
Do something like the following instead:
public void start() throws IOException {
serverSocket = new ServerSocket(port);
running = true;
while(running) {
Socket connectionSocket = null;
InputStream input = null;
OutputStream output = null;
try {
connectionSocket = serverSocket.accept();
input = connectionSocket.getInputStream();
output = connectionSocket.getOutputStream();
// At this point do whatever you need to do with these streams
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
if(input != null) {
input.close();
}
if(output != null) {
output.close();
}
if(connectionSocket != null) {
connectionSocket.close();
}
}
}
serverSocket.close();
serverSocket = null;
}
Let this method run in a separate thread so it doesn't block the UI thread. As soon as requested by the user or when your Activity stops, you should set the running variable to false so your server stops gracefully.
Upvotes: 1