0

我正在做一个小项目,我必须与手机上的 Android 应用程序和 Arduino 进行通信。

现在,我在 Android 和笔记本电脑之间建立了连接(用作服务器,我在这里存储了少量数据),当我从 Android 应用程序发送某些指令时,我可以更改文本文件的内容。

我就是这样做的:

我有一个ServerSide监听端口 3000 的类,我阅读了从手机流式传输的文本,然后我在文本文件中针对不同的消息进行了某些更改。

编码:

public class ServerSide {

public static void main(String[] args) throws IOException {

    while (true) {

        ServerSocket serverSocket = null;

        // check if client is trying to connect
        try {
            serverSocket = new ServerSocket(3000);
        } catch (IOException e) {
            System.err.println("Cannot communicate on this port");
            System.exit(1);
        }

        Socket clientSocket = null;

        // move to another socket
        try {
            clientSocket = serverSocket.accept();

        } catch (IOException e) {
            System.err.println("Accept failed");
            System.exit(1);
        }

        // stream that will be sent to client. "true" is for creating from
        // existing
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
                true);
        // stream that comes from the client
        BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        String recivedData, sendData;

        ServerProtocol communicationProtocol = new ServerProtocol();

        while ((recivedData = in.readLine()) != null) {

            sendData = communicationProtocol.process(recivedData);
            out.println(sendData);
            System.out.println("The text should now be written in file");
            System.out.println(sendData);
        }

        in.close();
        out.close();
        clientSocket.close();
        serverSocket.close();

    }

}

}

ServerProtocol.process()是更新文件的方法

顺便说一句,这是一个很好的程序版本,它暗示通过套接字进行连接(如果有人需要这方面的信息,在将来)。

一切都很好,我可以在发送更新后立即看到我的更新,服务器启动并运行,等待消息。

我忘了提一下,总的来说,我是 Java 新手,也是编程新手。

现在,我希望我设法编写的这段代码成为更大“服务器”的一部分。通过“服务器”,我理解“服务”的程序,执行服务。当它在我的笔记本电脑上运行时,它会在我指定的端口上获取来自 Internet 的信息,根据我的消息更改文件中的内容,保持这些文件更新,同时它使用这些文件来“解释”我发送的数据从电话,然后将相应的消息发送到 Arduino Shield。(这就是我想要达到的)

我想我想念的是以下内容:

我如何使我编写的这段代码成为一个更大项目的一部分,这一切都可以做到?

我设法将项目分成三个部分:

  1. 通讯笔记本电脑 - 安卓
  2. 不断的数据更新
  3. 通讯笔记本电脑 - Arduino

我做了一些研究,我遇到了线程。所以我想在一个单独的线程上与 Android 进行通信MainServer。我显然弄错了,因为它没有做我期望它做的事情,所以这里是代码:

我创建了ServerSide扩展 Thread 的类,并且有一个run()在我启动线程时应该调用的方法。它的行为与上面的一样,但执行代码位于一个run()方法中:

public class ServerSide extends Thread {

@Override
public void run() {

    while (true) {

        ServerSocket serverSocket = null;

        // check if client is trying to connect
        try {
            serverSocket = new ServerSocket(3000);
        } catch (IOException e) {
            System.err.println("Cannot communicate on this port");
            System.exit(1);
        }

        Socket clientSocket = null;

        // move to another socket
        try {
            clientSocket = serverSocket.accept();

        } catch (IOException e) {
            System.err.println("Accept failed");
            System.exit(1);
        }

        // stream that will be sent to client. "true" is for creating from
        // existing
        PrintWriter out = null;
        try {
            out = new PrintWriter(clientSocket.getOutputStream(), true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // stream that comes from the client
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String recivedData, sendData;
        recivedData = null;
        sendData = null;

        ServerProtocol communicationProtocol = new ServerProtocol();

        try {
            while ((recivedData = in.readLine()) != null) {

                try {
                    sendData = communicationProtocol.process(recivedData);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                out.println(sendData);
                System.out
                        .println("The text should now be written in file");
                System.out.println(sendData);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.close();
        try {
            clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

然后,我有MainServer

public class MainServer {

public static void main(String[] args) throws IOException {

    System.out.println("Started");
    Thread myThread = new Thread(new ServerSide());
    myThread.start();
    System.out.println("Started2");

    while (true);
    }
    }

它应该什么都不做,只是启动新线程。我希望这个新线程的行为就像ServerSide上面的旧线程(带有main()方法的线程)。

有人,请告诉我我哪里错了!?!

4

2 回答 2

1

好吧,这门课有两件事看起来有点不寻常MainServer。首先,创建线程new Thread(new ServerSide())会导致编译错误。有两种方法可以解决这个问题:要么ServerSide实现Runnable接口而不是扩展Thread,要么使用new ServerSide(). 其次,末尾的无限循环main没用,可以去掉。main 方法在自己的线程中运行,如果它完成,所有其他线程继续运行,不需要保持 main 活着。完成后程序确实会继续运行main,这可能看起来很奇怪,但这就是会发生的事情。

于 2013-08-24T22:44:25.770 回答
0

这里一切正常,我的问题实际上是我的手机连接到无线网络,我在后院有点太远了:)

于 2013-08-26T21:30:36.653 回答