爪哇 - 通过浏览器/ URL连接的ServerSocket [英] Java - connection to ServerSocket via browser/URL

查看:537
本文介绍了爪哇 - 通过浏览器/ URL连接的ServerSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个软件,我不能够使用插座中使用一个ServerSocket连接到一个Java应用程序的约束下,来的。

I'm writing a piece of software, and I'm under the restriction of not being able to use socket to connect to a java application using a ServerSocket.

我想我会用URL连接尝试,因为它可以定义连接到哪个端口

I thought I'd try with an URL connection, since it's possible to define which port to connect to

例如:

127.0.0.1:62666

我有我的服务器应用程序侦听连接,写输入到一个JTextArea中。当通过浏览器连接到服务器(127.0.0.1:62666),它输出:

I have my server app listening for connections and writing the input out to a jTextArea. When connecting to the server (127.0.0.1:62666) through a browser, it outputs:

GET / HTTP/1.1
GET /favicon.ico HTTP/1.1

我有另一个应用程序用于通过URL连接连接到ServerSocket的:

I have another app for connecting to the ServerSocket through an URL connection:

try{
        URL url = new URL("http://127.0.0.1:62666");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.connect();
        PrintWriter writer = new PrintWriter(connection.getOutputStream());
        writer.print("Hello");
        System.out.println("should have worked");
        writer.flush();
        writer.close();
    }catch(IOException e){
        e.printStackTrace();
    }

据仅供参考打印出应该有工作的消息,但它从来没有到服务器的JTextArea中写到什么。在code服务器应用程序是这样的:

It prints out the "should have worked" message fyi, but it never writes anything to the jTextArea of the server. The code for the server app looks like this:

try{

        ServerSocket serverSock = new ServerSocket(62666);

        while(doRun){
            Socket sock = serverSock.accept();
            BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            PrintWriter writer = new PrintWriter(sock.getOutputStream());

            InfoReader.gui.writeToTextArea(reader.readLine() + " From IP: " + sock.getInetAddress() + "\n");
            writer.println("Testing123");

            writer.close();

            reader.close();

        }
    }catch(IOException e){
        e.printStackTrace();
    }

请注意:通过浏览器连接时,它显示文本是testing123

Note: when connecting through the browser it displays the text "Testing123".

所以我不知道如何做到这一点我想或者读取将ServerSocket通过访问URL的方式,这样我就可以通过一个URL来访问它,而它传递的参数(在URL)。

So I'm wondering how to do this the way I'm trying or perhaps read the URL that the ServerSocket was accessed through, so I could access it through a URL while passing it arguments (in the URL).

希望这是有道理的:)

谢谢,
迈克。

Thanks, Mike.

推荐答案

我无法弄清楚到底是怎么回事。有一些好笑的输出流。添加

I can't figure out exactly what's up. There's something funny about that OutputStream. Add a

((HttpURLConnection) connection).getResponseCode();

地方连接()和前的close(),你应该看到不同的东西,如果没有什么你期望的那样。

somewhere after connect() and before close(), and you should see something different, if not what you expect.

也许不是试图用HTTP作为一个黑客,你应该只是去完整的HTTP。从客户端使用HTTP像你已经是,并成立了一个嵌入式HTTP服务器的服务器上。有几个从那里可供选择,从字面上只需要几行获得运行:的灰熊简单的框架,或的码头,例如

Perhaps instead of trying to use HTTP as a hack, you should just go full HTTP. Use HTTP from the client like you already are, and set up an embedded HTTP server on the server. There are several to choose from out there that literally take just a few lines to get running: Grizzly, Simple Framework, or Jetty, for instance.

这篇关于爪哇 - 通过浏览器/ URL连接的ServerSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆