在java中把一个小程序变成一个大程序。多线程?。角落
我正在做一个小项目,我必须通过手机和Arduino与Android应用程序进行通信 现在,我已经在Android和笔记本电脑之间建立了连接(用作服务器,我在这里存储了少量数据),当我从Android应用程序发送某些指令时,我可以更改文本文件的内容 我就是这样做的: 我有一个在java中把一个小程序变成一个大程序。多线程?。角落,java,multithreading,sockets,Java,Multithreading,Sockets,我正在做一个小项目,我必须通过手机和Arduino与Android应用程序进行通信 现在,我已经在Android和笔记本电脑之间建立了连接(用作服务器,我在这里存储了少量数据),当我从Android应用程序发送某些指令时,我可以更改文本文件的内容 我就是这样做的: 我有一个ServerSide类,它监听端口3000,从手机中读取我传输的文本,然后对不同消息的文本文件进行某些更改 守则: public class ServerSide { public static void main(Stri
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新手,一般来说是编程新手
现在,我希望我设法编写的代码成为更大的“服务器”的一部分。通过“服务器”,我理解一个“服务”的程序,执行一项服务。当它在我的笔记本电脑上运行时,它在我指定的端口上接收来自互联网的信息,根据我的消息更改文件中的内容,保持这些文件的更新,同时它使用这些文件“插入”我从手机发送的数据,然后根据消息发送到Arduino Shield。(这就是我想要实现的目标)
我想我错过的是:
我该如何编写这段我一直写到现在的代码,作为一个更大项目的一部分,完成所有这些工作?
我将项目分为三部分:
MainServer
的单独线程上与Android进行通信。我显然弄错了,因为它没有达到我期望的效果,所以下面是代码:
我创建了扩展线程的ServerSide
类,并有一个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()
方法的线程)
有人,请告诉我哪里弄错了 嗯,MainServer
类有两件事似乎有点不寻常。首先,使用新线程(new ServerSide())
创建线程将导致编译错误。有两种方法可以解决此问题:要么让ServerSide
实现Runnable
接口,而不是扩展Thread
,要么使用new ServerSide()
创建线程。其次,main
末尾的无限循环是无用的,可以删除。main方法在它自己的线程中运行,如果它完成了,所有其他线程都会继续运行,并且不需要让main保持活动状态。当main
完成时,程序确实会继续运行,这可能看起来很奇怪,但这就是将会发生的事情。这里一切正常,我的问题实际上是我的手机连接到了wi-fi,我在后院有点太远:)我打赌这是一个编译错误。如果我是对的,告诉我是什么不,问题似乎是另一个问题,我现在将开始新的研究。它只发生一次:如果我编译服务器端和MainServer,然后运行MainServer,它会按需要运行。但是,如果我关闭MainServer并重新启动它,它什么都不会做。我想我应该停止线程(?)线程似乎很困惑你。如果停止MainServer
,它启动的所有线程都应该停止。这也是我所期望的。。。目前,我不明白为什么它只有在编译完我之后第一次运行后才起作用。我不能帮你。1.我知道虽然循环是无用的,但我只是使用它,因为这应该在我的服务器处于活动状态时发生。现在,这不是一个令人感兴趣的问题。2.MainServer似乎可以工作,但只能工作一次。我编译了服务器端和MainServer,然后运行MainService(java-classpath.MainServer),它工作得很好。当我停止它并再次启动它时,它只是等待。我应该在出口处停止线程吗?怎么用?