我试图了解套接字在 Java 中是如何工作的,因此我想看看客户端-服务器实现。我发现了这个:Creating a Simple Java TCP/IP Server and Client Socket .
如果我传递localhost
或127.0.0.1
,服务器似乎可以正常工作,但是客户端将拒绝连接其中任何一个,并抛出连接被拒绝异常,虽然我在客户端之前启动了服务器。
服务器输出:
Running Server: Host=127.0.0.1 Port=5069
客户端输出:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:244)
at Client.<init>(Client.java:12)
at Client.main(Client.java:29)
我的Java代码:
public class Server {
private ServerSocket server;
public Server(String ipAddress) throws Exception {
if (ipAddress != null && !ipAddress.isEmpty()) {
// System.out.println(InetAddress.getByName(ipAddress));
this.server = new ServerSocket(0, 1, InetAddress.getByName(ipAddress));
} else {
this.server = new ServerSocket(0, 1, InetAddress.getLocalHost());
}
}
private void listen() throws Exception {
String data = null;
Socket client = this.server.accept();
String clientAddress = client.getInetAddress().getHostAddress();
System.out.println("\r\nNew connection from " + clientAddress);
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
while ((data = in.readLine()) != null) {
System.out.println("\r\nMessage from " + clientAddress + ": " + data);
}
}
public InetAddress getSocketAddress() {
return this.server.getInetAddress();
}
public int getPort() {
return this.server.getLocalPort();
}
public static void main(String[] args) throws Exception {
Server app = new Server("localhost");
System.out.println("\r\nRunning Server: " +
"Host=" + app.getSocketAddress().getHostAddress() +
" Port=" + 50696);
app.listen();
}
}
public class Client {
private Socket socket;
private Scanner scanner;
private Client(InetAddress serverAddress, int serverPort) throws Exception {
this.socket = new Socket(serverAddress, serverPort);
this.scanner = new Scanner(System.in);
}
private void start() throws IOException {
String input;
while (true) {
input = scanner.nextLine();
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
out.println(input);
out.flush();
}
}
public static void main(String[] args) throws Exception {
// System.out.println( InetAddress.getByName("localhost"));
Client client = new Client(
InetAddress.getByName("127.0.0.1"), 50696);
System.out.println("\r\nConnected to Server: " + client.socket.getInetAddress());
client.start();
}
}
最佳答案
如果您尝试连接到错误的端口,则连接将被拒绝(请参阅 this answer )。您的服务器未监听端口 5069
。显然,您将其设置为在此处监听端口 0
:
if (ipAddress != null && !ipAddress.isEmpty()) {
this.server = new ServerSocket(0, 1, InetAddress.getByName(ipAddress));
} else {
this.server = new ServerSocket(0, 1, InetAddress.getLocalHost());
}
查看您使用的构造函数的 Javadoc:
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
Parameters:
port - the port number, or 0 to use a port number that is automatically allocated.
backlog - requested maximum length of the queue of incoming connections.
bindAddr - the local InetAddress the server will bind to
第一个参数是端口号,您为两个构造函数传递 0
。
关于java - 客户端套接字连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53860745/