尝试实现 Java 套接字示例时出现的问题
我试图在这里实现这个示例:读取和写入套接字< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
echo 服务未监听。为什么不自己写呢?运行下面的应用程序并将您的客户端更改为连接到同一端口 (8000)。
顺便说一句,下一个示例(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).
Btw, the next example (knock-knock server) does work and gives a nice example of using a 'protocol' class.
我不认为 echo 服务默认运行,当我在 Win XP 客户端上尝试快速测试它时,它不起作用:
因此,为了使您的代码正常工作,您可以尝试将其指向具有 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:
So to make your code work, you could try pointing it to a server that has the echo service running.
为了将来参考,回显服务通常默认被禁用。我使用的是 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