gpt4 book ai didi

java.net.ConnectException : Connection refused: connect occured 异常

转载 作者:可可西里 更新时间:2023-11-01 02:50:48 25 4
gpt4 key购买 nike

我试图在 Windows 命令行中运行代码并收到异常:

D:\dasi\java\javaLab>java ServerClient
java.net.ConnectException: Connection refused: connect
java.lang.StringIndexOutOfBoundsException: String index out of range: -2

D:\dasi\java\javaLab>

在另一个命令行 windw 中:

D:\dasi\java\javaLab>java SocketClient
java.net.ConnectException: Connection timed out: connect

D:\dasi\java\javaLab>

服务器代码:

import java.io.*;
import java.net.*;
public class ServerClient {
public ServerClient(int port) {
Server server = new Server(port);
server.start();
Client client = new Client(port);
client.start();
}
public static void main(String[] args) {
ServerClient s1 = new ServerClient(7777);
}
}

class Server extends Thread {
int port;
ServerSocket server;
Socket socket;
DataOutputStream outStream = null;
DataInputStream inStream = null;
public Server(int poort) {
try {
this.port = port;
server = new ServerSocket(port);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public void run() {
try {
socket = server.accept();
outStream = new DataOutputStream(socket.getOutputStream());
inStream = new DataInputStream(socket.getInputStream());
System.out.println("server is ok, please continue!");
while(true) {
String str = inStream.readUTF();
System.out.println("The server receive String:"+str);
outStream.writeUTF(str);
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}

class Client extends Thread {
int port;
Socket socket;
DataInputStream inStream = null;
DataOutputStream outStream = null;
public Client(int port) {
try {
this.port = port;
socket = new Socket(InetAddress.getLocalHost(),port);
inStream = new DataInputStream(socket.getInputStream());
outStream = new DataOutputStream(socket.getOutputStream());
System.out.println("client is ok, please continue!");
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public void run() {
try {
while(true) {
byte[] b = new byte[1024];
String str = "";
int m = System.in.read(b);
str = new String(b,0,0,m-1);
outStream.writeUTF(str);
str = inStream.readUTF();
System.out.println("The client receive String:"+str);
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}

客户端代码:

import java.net.*;
import java.io.*;
public class SocketClient {
Socket s = null;
DataInputStream inStream = null;
DataOutputStream outStream = null;
public SocketClient() {
try {
init();
waitData();
}
catch(Exception e) {
System.out.println(e.toString());
}
}
void init() throws Exception {
s=new Socket("10.15.43.216",8765);
inStream = new DataInputStream(s.getInputStream());
outStream = new DataOutputStream(s.getOutputStream());
s.setSoTimeout(3000);
}
void waitData() {
while(true) {
try {
String str = inStream.readUTF();
System.out.println("Client accept:" +str);
str = Integer.toString(Integer.parseInt(str)+1);
outStream.writeUTF(str);
}
catch(Exception e) {
System.out.println(e.toString());
break;
}
}
}
public static void main(String[] args) {
new SocketClient();
}
}

我想知道我的代码是否有任何错误,或者是否是我的计算机端口导致了问题。因为当我检查我的计算机端口时,我没有看到 7777。当我发出命令 netstat -nao | findstr 7777,它什么也没返回。

D:\dasi\java\javaLab>netstat -nao | findstr 7777

D:\dasi\java\javaLab>

如果是端口问题,那7777端口怎么打开。我是这里的新手,请帮忙。非常感谢!

最佳答案

替换

 public Server(int poort) {
try {
this.port = port;
...
}
}

 public Server(int poort) {
try {
this.port = port;
...
}
}

或者更确切地说,端口的默认值为零,那么您的 serverSocket 将绑定(bind)到 0 端口而不是 7777。

至于这段代码:

public ServerClient(int port) {
Server server = new Server(port);
server.start();
Client client = new Client(port);
client.start();
}

恐怕这很容易给你带来麻烦,因为我们无法确保服务器线程会在客户端线程之前执行,如果客户端线程 strong> 在服务器未运行时先执行会出错。而且你已经在另一个 java 文件中有一个客户端,所以我不明白为什么你在这里有一个客户端。也许你可以删除它们,代码可以是这样的:

public ServerClient(int port) {
Server server = new Server(port);
server.start();
}

至于客户端代码您的服务器套接字是 7777,因此您应该在 init 方法 中连接 7777 端口而不是 8765,也许代码可以是这样的:

void init() throws Exception {
s=new Socket(server name,7777);
...
}

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

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