0

我在我的 android 设备上制作了一个简单的 ServerSocket 应用程序,我使用 telnet 进行连接,它工作正常(发送的消息显示在我的 logcat 窗口中)。问题;当我尝试从 php 页面建立连接时(我也在使用 PHP 套接字)。Android 套接字接受第一个连接,然后有点奇怪 - 有时他会读取从 PHP 发送的消息并将它们显示在 logcat 中,但通常它只接受第一个请求,然后......什么都没有

安卓端代码:

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的请求)。

感谢帮助。

4

1 回答 1

1

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

改为执行以下操作:

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 停止时,您应该将 running 变量设置为 false,以便您的服务器正常停止。

于 2012-06-25T21:29:14.143 回答