我用java编写了一个简单的客户端服务器应用程序。当我尝试在服务器上运行客户端时,出现以下异常。线程“主”java.net.BindException 中的异常:地址已在使用中:java.net.DualStackPlainSocketImpl.bind0 处的 JVM_Bind(本机方法)java.net.AbstractPlainSocketImpl.bind 处的 java.net.DualStackPlainSocketImpl.socketBind(未知来源) (Unknown Source) at java.net.PlainSocketImpl.bind(Unknown Source) at java.net.ServerSocket.bind(Unknown Source) at java.net.ServerSocket.(Unknown Source) at java.net.ServerSocket.(Unknown Source)在 DateServer.main(DateServer.java:8)
我已经添加了所有的 jar 文件,检查了如何删除这个异常来执行我的代码。
import java.util.Scanner;
import java.net.Socket;
class DateClient {
public static void main(String[] args) throws java.io.IOException {
String host = "localhost";
int port = Integer.parseInt("415");
Socket server = new Socket(host, port);
Scanner scan = new Scanner(server.getInputStream());
System.out.println(scan.nextLine());
}
}
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
class DateServer {
public static void main(String[] args) throws java.io.IOException {
ServerSocket s = new ServerSocket(415);
while (true) {
Socket incoming = s.accept();
System.out.println(incoming);
PrintWriter toClient = new PrintWriter(incoming.getOutputStream());
toClient.println(new Date());
toClient.flush();
incoming.close();
}
}
}