0

我正在做一个 web 项目,我有一个名为 Connection 的类,这个类通过 TCP/IP 与另一个设备建立连接,当有一个 http get 请求时,我实例化一个对象“connection_o”并启动一个线程来保持像这样通信:“connection_o.start”所以建立了连接,在下一个http请求中我必须发送一条消息,但是当我再次执行“doGet”以避免空指针异常时,我需要再次实例化对象,但我不能,因为我需要使用在运行之前使用的相同实例,在我的测试中,连接继续工作,但我无法访问我已经创建的线程。所以我需要某种静态类或一种方法来使用已经运行的线程。

这是套接字的代码

   import java.io.*;
import java.net.*;
public class Provider extends Thread{

    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    Provider(){}


    public void run()
    {
        try{
            //1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);
            //2. Wait for connection
            System.out.println("Waiting for connection");
            connection = providerSocket.accept();
            System.out.println("Connection received from " + connection.getInetAddress().getHostName());
            //3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");
            //4. The two parts communicate via the input and output streams
            do{
                try{
                    message = (String)in.readObject();
                //  System.out.println("client>" + message);



                    if (message.equals("cambio la variable"))
                        System.out.println("Abriendo Puerta");
                    //  sendMessage("bye");
                }
                catch(ClassNotFoundException classnot){
                    System.err.println("Data received in unknown format");
                }
            }while(!message.equals("bye"));
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
        finally{
            //4: Closing connection
            try{
                in.close();
                out.close();
                providerSocket.close();
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        }
    }
    public void sendMessage(String msg)
    {
        try{
            out.writeObject(msg);
            out.flush();
        //  System.out.println("server>" + msg);
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

这是 doGet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException {
        String s = request.getParameter("s");
        //Routine to send blink to RPi
    if (s.equals("Start")){
      Provider c = new Provider();
      c.Inicio();
    }

    if (s.equals("Send")){

          c.sendMessage("Blink");
        }

提前致谢

4

2 回答 2

0

我假设的问题是,HTTP 是一个无状态协议。这意味着默认情况下,服务器对客户端一无所知。

这就是Sessions到位的地方。在您的初始请求中,您必须为发出请求的客户端初始化会话。从那时起,您就可以根据请求“识别”客户端(例如,恢复客户端状态)。

您还必须在会话上下文中保存“连接”。您可能要考虑使用所谓的“连接池”,因为处理所有已建立的连接并正确关闭它们可能非常复杂(例如会话超时等)

我建议您阅读有关 HTTP 协议、会话和(例如数据库)连接处理的基础知识。

于 2013-08-21T15:55:18.153 回答
0

也许你可以保留

Provider c = new Provider();

作为 servlet 类的实例变量并更改代码:

if (s.equals("Start")){
      c.Inicio();
    }

注意:在这种情况下,您需要注意线程安全。因为 nowc.Inicio()将被多个请求线程一次调用。

于 2013-08-21T17:56:15.147 回答