简体   繁体   English

适用于Android的多线程Web服务器

[英]Multithreaded Web Server for Android

I'm developing a multithreaded web server for an android app and I've some problems with a page that uses an external .css file, and a .js file, but only with Google Chrome! 我正在为Android应用程序开发多线程Web服务器,但使用外部.css文件和.js文件的页面存在一些问题,但仅适用于Google Chrome浏览器! With Firefox and Opera the page is rendered fine, with Google Chrome sometimes the .css is loaded, sometimes the .js, sometime both or neither. 使用Firefox和Opera时,页面呈现得很好,使用Google Chrome时,有时会加载.css,有时会加载.js,有时两者都加载或不加载。

This is my app's structure: 这是我的应用程序的结构:

WebServer.java WebServer.java

class WebServer implements Runnable{
protected boolean ON;

public void start(){
            if(!ON){
            ON=true;
            thread=new Thread(this,"WebServer");
            thread.start(); }}

public void run(){
while(ON){
listenSocket = new ServerSocket(port);
Socket connectionSocket = listenSocket.accept();
Thread t = new Thread(new Client(connectionSocket));
t.start();
listenSocket.close();}
}}

Client.java Client.java

class Client implements Runnable {
public void start(){
        thread=new Thread(this,"Client");
        thread.start();}

public void run(){
//parse the request and send a file
}
}

myApp.java myApp.java

public class myApp extends Activity{

onCreate(){
WebServer ws=new WebServer(8080);
}

onClick(){
...
ws.start();
}}

When I click a button on the activity, it call webserver.start(); 当我单击活动上的按钮时,它将调用webserver.start();。 In my opinion google chrome sends more requests concurrently and there's a problem with threads... Can you help me? 我认为google chrome会同时发送更多请求,并且线程有问题...您能帮我吗?

[EDIT] I had forgotten to write the loop in the run() method in the question [编辑]我忘了在问题的run()方法中编写循环

[EDIT 2] I just tried with an other pc, and there are problems also with firefox.. [编辑2]我刚尝试过另一台电脑,但是Firefox也有问题。

There is a general misunderstanding of the thread mechanism in your code. 您的代码中普遍存在对线程机制的误解。

A runnable has to override run. 可运行变量必须覆盖运行。 Not start. 无法启动。 The run() method of the runnable will be called when the nesting thread will be started. 当嵌套线程启动时,将调用runnable的run()方法。 In other words, the start method of your client will never be used, and hope fully, as it would create a thread inside a thread.. not very usefull. 换句话说,您的客户端的start方法将永远不会使用,并且会完全希望,因为它将在线程内创建一个线程。不是很有用。

Redesign your webser so that, : 重新设计您的Webser,以便:

  • it's start method starts a new nesting thread as you did 它的start方法像您一样启动一个新的嵌套线程
  • it's run method does the following 它的运行方法执行以下操作
    • your webserver binds to a port 您的网络服务器绑定到端口
    • in a loop : accept new connections and start new client thread for each. 循环:接受新连接并为每个连接启动新的客户端线程。
    • the loop could be controlled by a boolean flag that you could rise to stop the server (ON would fit, even if the name of this variable doesn't follow java naming conventions and is rather poor semanticly speaking) 可以由一个布尔标志来控制该循环,您可以上升该布尔标志来停止服务器(即使此变量的名称不遵循Java命名约定并且从语义上来讲也很差),ON仍然合适。

then each client would, in it's run (no more start method) : 那么每个客户端都将在运行中(不再需要start方法):

  • read data from socket input stream 从套接字输入流读取数据
  • reply on socket outputstream 回复套接字输出流
  • briefly, implement http protocole. 简要地,实现http协议。

You could find some java code to inspire you on the web , some examples are well documented . 您可以在网络上找到一些Java代码来激发您灵感, 一些示例已被充分记录在案 Also, you could consider using java.nio package that is maybe less effective for a single request but much more effective at handling massive multiple connections. 另外,您可以考虑使用java.nio包,该包对于单个请求可能无效,但是在处理大量的多个连接时效率更高。 But code is harder. 但是代码更难。

You should consider reading more about runnables and also consider reading some stuff about synchronized key word to ensure that your web server doesn't start twice a connection for the same client or get confused in case of simultaneous requests. 您应该考虑阅读有关可运行对象的更多信息,还应考虑阅读有关同步关键字的一些知识,以确保您的Web服务器不会为同一客户端启动两次连接或在同时请求的情况下感到困惑。

Regards, Stéphane 此致,Stéphane

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM