PHP(客户端)到Android(服务器)套接字通信

时间:2012-06-25 21:00:44

标签: php android sockets serversocket

我在我的Android设备上创建了一个简单的ServerSocket应用程序我使用telnet进行连接并且它工作正常(发送的消息显示在我的logcat窗口中)。 Probl; em是当我尝试从php页面建立连接时(我也使用PHP套接字)。 Android套接字接受第一个连接然后是奇怪的东西 - 有时他读取从PHP发送的消息并在logcat中显示它们但通常它只接受第一个请求和trhen ...没有

ANDROID SIDE代码:

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();
    }




}
}

这是一个简单的PHP请求:

<?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);
?>

......就是这样 也许有一些更容易的沟通(我需要每次都从PHP连接到Android设备,所以PHP不能等待Android的请求)。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您确实意识到您的Android ServerSocket应该维护一个等待传入连接的“无限”循环,通过读取请求来处理请求,直到到达流的末尾,然后返回等待另一个传入请求的状态。 / p>

执行以下操作:

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;
}

让此方法在单独的线程中运行,因此它不会阻止UI线程。一旦用户请求或您的Activity停止,您应该将运行变量设置为false,以便服务器正常停止。