Java服务器程序仅在本地主机上工作,而在客户端从另一台计算机连接时不工作
嗨,这里有人问了一个类似的问题: 然而,我有一个不同的错误。我是java的新手,正在阅读oracle的教程代码,尝试学习如何设置客户机/服务器Java服务器程序仅在本地主机上工作,而在客户端从另一台计算机连接时不工作,java,Java,嗨,这里有人问了一个类似的问题: 然而,我有一个不同的错误。我是java的新手,正在阅读oracle的教程代码,尝试学习如何设置客户机/服务器 /* * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are p
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.*;
import java.net.*;
public class KnockKnockClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println(
"Usage: java EchoClient <host name> <port number>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
/*
* Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.net.*;
import java.io.*;
public class KnockKnockServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java KnockKnockServer <port number>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine, outputLine;
// Initiate conversation with client
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
在“java KnockKnockClient localhost 4444”行上,它设置主机和端口#。localhost或127.0.0.1可以工作,但如果我将其替换为我的IP地址,则会出现以下错误:
无法获取连接到[我的IP地址]的I/O
我试着在关闭防火墙的情况下做这件事,但没有成功。我真正想做的是将KnockKnockServer程序放在我的家用计算机上,将knockclient程序放在任何其他计算机上,并且能够使用客户端程序连接到服务器,无论其他计算机在哪里。我做错了什么?谢谢
我添加了printStackTrace():
java.net.ConnectException:连接超时:连接
位于java.net.DualStackPlainSocketImpl.connect0(本机方法)
位于java.net.DualStackPlainSocketImpl.socketConnect(未知源)
位于java.net.AbstractPlainSocketImpl.doConnect(未知源)
位于java.net.AbstractPlainSocketImpl.connectToAddress(未知源)
位于java.net.AbstractPlainSocketImpl.connect(未知源)
位于java.net.PlainSocketImpl.connect(未知源)
位于java.net.socksocketimpl.connect(未知源)
位于java.net.Socket.connect(未知源)
位于java.net.Socket.connect(未知源)
位于java.net.Socket。(未知源)
位于java.net.Socket。(未知源)
位于knockclient.main(knockclient.java:48)
我猜这意味着没有建立连接。我尝试打开路由器设置上的端口,但仍然无法工作。我有一个cisco Linksys E2000路由器,可能是因为它不允许打开这些端口吗?看看KnockClient类的最后几行。它正在打印该错误,因为您发布的代码告诉它在出现异常时打印该错误。添加一行以打印出异常,以便获得更多信息。像这样:
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
e.printStackTrace();
System.exit(1);
}
可能您正在使用路由器,如果是,请确保您有开放的端口。取决于路由器的型号,方法有所不同,但通常必须进入路由器的配置(192.168.1.1)->虚拟服务器,您添加了ip和端口以释放。您是否尝试了链接帖子中答案中的步骤?您忽略了/*默默地吃着*,这告诉您到底出了什么问题<代码>e.printStackTrace()会准确地告诉您出了什么问题。更改无用的错误消息以打印实际的异常。几年前我向Sun抱怨在他们的教程中使用了那些愚蠢的错误信息。我想他们终于解决了其中的一些问题。显然不是全部。@JarrodRoberson复制品的选择非常差。他引用自己的例子是一个更好的选择:我知道这很古老,但我想用我的解决方案,以防它对其他人有帮助。我试图通过本地连接从笔记本电脑连接到台式电脑。问题是,我的路由器中有单独的无线和有线连接交换机,导致笔记本电脑无法连接到桌面,因为它们实际上位于单独的本地网络上。所以,我对它进行了测试,笔记本电脑或台式机连接工作正常。希望有帮助。我添加了端口转发并输入了本地ip地址,但它不起作用。我去看了看端口是否打开并且没有骰子
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at KnockKnockClient.main(knockknockclient.java:48)
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
e.printStackTrace();
System.exit(1);
}