尝试实现 Java 套接字示例时出现的问题

发布于 2024-12-01 00:33:21 字数 1336 浏览 2 评论 0原文

我试图在这里实现这个示例:读取和写入套接字< /a>

我将代码复制并粘贴到 NetBeans 中。我将端口名称“taranis”更改为“localhost”并尝试运行该示例,但出现错误:

运行:无法获取连接到本地主机的 I/O。 Java 结果:1 构建成功(总时间:1秒)

我还尝试用 localhost 替换笔记本电脑的实际主机名,但它给出了类似的错误。你能帮忙指出我做错了什么吗?

编辑:关于马克的建议,当我

System.err.println("Couldn't get I/O for " + "the connection to: localhost.");

e.printStackTrace();

替换时,我得到:

run:
java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        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 EchoClient.main(EchoClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

I am trying to implement this example here: Reading from and Writing to a Socket

I copied and pasted the code into NetBeans. I changed the port name "taranis" to "localhost" and tried to run the example, but I got the error:

run: Couldn't get I/O for the connection to: localhost. Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

I also tried to substitute localhost for my actual hostname of my laptop, but it gives the similar error. Can you help pinpoint what I am doing wrong?

Edit: In regards to Mark's recommendation, when I substitute

System.err.println("Couldn't get I/O for " + "the connection to: localhost.");

with

e.printStackTrace();

I get:

run:
java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        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 EchoClient.main(EchoClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2024-12-08 00:33:21

echo 服务未监听。为什么不自己写呢?运行下面的应用程序并将您的客户端更改为连接到同一端口 (8000)。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

    private static final int PORT = 8000;

    public static void main(String[] args) throws Exception {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
        }
        catch (IOException e) {
            System.err.println("Could not listen on port: " + PORT);
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        }
        catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.println("Echo server started");

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println("echoing: " + inputLine);
            out.println(inputLine);
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

顺便说一句,下一个示例(knock-knock 服务器)确实有效,并给出了使用“协议”类的一个很好的示例。

The echo service is not listening. Why not write your own? Run the application below and change your client to connect to the same port (8000).

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

    private static final int PORT = 8000;

    public static void main(String[] args) throws Exception {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
        }
        catch (IOException e) {
            System.err.println("Could not listen on port: " + PORT);
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        }
        catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.println("Echo server started");

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println("echoing: " + inputLine);
            out.println(inputLine);
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

Btw, the next example (knock-knock server) does work and gives a nice example of using a 'protocol' class.

萌能量女王 2024-12-08 00:33:21

我不认为 echo 服务默认运行,当我在 Win XP 客户端上尝试快速测试它时,它不起作用:

H:\>telnet localhost 7
Connecting To localhost...Could not open connection to the host, on port 7: 
Connect failed

H:\>

因此,为了使您的代码正常工作,您可以尝试将其指向具有 echo 的服务器服务正在运行。

I don't think the echo service is running by default, when I tried a quick test it on my Win XP client, it did not work:

H:\>telnet localhost 7
Connecting To localhost...Could not open connection to the host, on port 7: 
Connect failed

H:\>

So to make your code work, you could try pointing it to a server that has the echo service running.

时光磨忆 2024-12-08 00:33:21

为了将来参考,回显服务通常默认被禁用。我使用的是 Windows 7,为了启用它,我按照此处的说明操作:

http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html

示例工作正常之后对我来说。

对于 XP:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sag_tcpip_pro_simptcpinstall.mspx?mfr=true

For future reference, the echo service is commonly disabled by default. I'm using windows 7, to enable it I followed the instructions found here:

http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html

Example worked fine for me afterwards.

For XP:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sag_tcpip_pro_simptcpinstall.mspx?mfr=true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文