eclipse 连接到 Tomcat 上的套接字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6396167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:27:53  来源:igfitidea点击:

Connecting to socket on Tomcat?

javaeclipsesocketstomcat

提问by Sbram

I'm trying to connect from a standalone applet to a servlet running on tomcat:

我正在尝试从独立小程序连接到在 tomcat 上运行的 servlet:

Servlet

小服务程序

public void init(ServletConfig config) throws ServletException { 
    super.init(config);
    // Start a daemon thread 
    try { 
        daemonThread = new Daemon(this);
        daemonThread.start();
    }
    catch (Exception e) { 
    }
}

protected int getSocketPort() { 
    return 8080;
}

public void handleClient(Socket client){ 
    new ScribbleThread(this, client).start();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    StringBuffer sb = new StringBuffer();
    sb.append("<html><body bgcolor=pink text=red>");
    sb.append("<h1 align=center>RUNNING</h1><hr>");
    sb.append("</body></html>");
    out.println(sb);
    out.close();
}
}

Servlet's init()creates this:

Servlet 的init()创建是这样的:

class Daemon extends Thread { 
private ServerSocket serverSocket;
private SocketServlet servlet;

public Daemon(SocketServlet servlet) { 
this.servlet = servlet;
}

public void run() { 
    try { 
        // Create a server socket to accept connections 
        serverSocket = new ServerSocket(servlet.getSocketPort());
    }
    catch (Exception e) { 
        return;
    }
    try { 
        while (true) { 
            try { 
                servlet.handleClient(serverSocket.accept());
            }
            catch (IOException ioe) { 
            }
        }
    }

I have this deployed by eclipse to TomCat. My question is what address does my applet need to make the socket to? When i visit http://localhost:8080/scrabServ/connectI see the 'RUNNING' message from the doGet()so is that where it needs to point?

我已经通过 Eclipse 将它部署到 TomCat。我的问题是我的小程序需要将套接字连接到哪个地址?当i visit http://localhost:8080/scrabServ/connect我看到来自“正在运行”的消息时doGet(),它需要指向哪里?

Applet:

小程序:

public static String testConnection(){
    InputStream in = null; 
    try { 
        // Make socket connection to servlet
        String servlet = new String("localhost/scrabServ/connect");
        Socket socket = new Socket(servlet, 8080); 

this gives me:

这给了我:

Exception in testConnection()java.net.UnknownHostException: localhost/scrabServ/connect
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at scribble.Scribble.testConnection(Scribble.java:41)
at scribble.Scribble.main(Scribble.java:28)

and points to the new Socket(servlet, 8080)line.

并指向new Socket(servlet, 8080)线。

采纳答案by Marcelo

You would have only to open a socket to "localhost", 8080and then issue a GET scrabServ/connectcommand. You can't open a socket to a specific URL.

您只需打开一个套接字"localhost", 8080,然后发出一个GET scrabServ/connect命令。您无法打开特定 URL 的套接字。

To communicate with a servlet you do it via request parameters, basically issuing a GET command such as: http://www.jmarshall.com/easy/http/http_footnotes.html#getsubmit

要与 servlet 通信,您可以通过请求参数进行通信,基本上是发出 GET 命令,例如:http: //www.jmarshall.com/easy/http/http_footnotes.html#getsubmit

Maybe you should use URLConnectioninstead. If you detail what exactly you want to do, maybe I can have a better idea of how to help you, maybe a HTTP server is not even needed for what you want to do.

也许您应该改用URLConnection。如果您详细说明您想要做什么,也许我可以更好地了解如何帮助您,也许您想要做的事情甚至不需要 HTTP 服务器。