gpt4 book ai didi

java.net.ConnectException : Connection refused

转载 作者:行者123 更新时间:2023-12-02 00:46:56 24 4
gpt4 key购买 nike

我正在尝试实现 TCP 连接,从服务器端一切正常,但当我运行客户端程序(从客户端计算机)时,出现以下错误:

java.net.ConnectException: Connection refused
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:432)
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 TCPClient.main(TCPClient.java:13)

我尝试更改套接字号,以防其正在使用中,但无济于事,有谁知道导致此错误的原因以及如何修复它。

服务器代码:

//TCPServer.java

import java.io.*;
import java.net.*;

class TCPServer {
public static void main(String argv[]) throws Exception {
String fromclient;
String toclient;

ServerSocket Server = new ServerSocket(5000);

System.out.println("TCPServer Waiting for client on port 5000");

while (true) {
Socket connected = Server.accept();
System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
+ ":" + connected.getPort() + " IS CONNECTED ");

BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(System.in));

BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connected.getInputStream()));

PrintWriter outToClient = new PrintWriter(
connected.getOutputStream(), true);

while (true) {

System.out.println("SEND(Type Q or q to Quit):");
toclient = inFromUser.readLine();

if (toclient.equals("q") || toclient.equals("Q")) {
outToClient.println(toclient);
connected.close();
break;
} else {
outToClient.println(toclient);
}

fromclient = inFromClient.readLine();

if (fromclient.equals("q") || fromclient.equals("Q")) {
connected.close();
break;
} else {
System.out.println("RECIEVED:" + fromclient);
}

}

}
}
}

客户端代码:

//TCPClient.java

import java.io.*;
import java.net.*;

class TCPClient {
public static void main(String argv[]) throws Exception {
String FromServer;
String ToServer;

Socket clientSocket = new Socket("localhost", 5000);

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));

PrintWriter outToServer = new PrintWriter(
clientSocket.getOutputStream(), true);

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));

while (true) {

FromServer = inFromServer.readLine();

if (FromServer.equals("q") || FromServer.equals("Q")) {
clientSocket.close();
break;
} else {
System.out.println("RECIEVED:" + FromServer);
System.out.println("SEND(Type Q or q to Quit):");

ToServer = inFromUser.readLine();

if (ToServer.equals("Q") || ToServer.equals("q")) {
outToServer.println(ToServer);
clientSocket.close();
break;
} else {
outToServer.println(ToServer);
}
}
}
}
}

最佳答案

此异常意味着您尝试连接的 IP/端口上没有监听服务:

  • 您尝试连接到错误的 IP/主机或端口。
  • 您尚未启动服务器。
  • 您的服务器未监听连接。
  • 在 Windows 服务器上,监听积压队列已满。

关于java.net.ConnectException : Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57889224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com