在某些java代码中获取这些错误
http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html
Exception in thread "Thread-0" java.lang.RuntimeException: Cannot open port 8080
at servers.MultiThreadedServer.openServerSocket(MultiThreadedServer.java:61)
at servers.MultiThreadedServer.run(MultiThreadedServer.java:22)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:353)
at java.net.ServerSocket.bind(ServerSocket.java:336)
at java.net.ServerSocket.<init>(ServerSocket.java:202)
at java.net.ServerSocket.<init>(ServerSocket.java:114)
at servers.MultiThreadedServer.openServerSocket(MultiThreadedServer.java:59)
... 2 more
Exception in thread "main" Stopping Server
java.lang.NullPointerException
at servers.MultiThreadedServer.stop(MultiThreadedServer.java:51)
at servers.Dispatch.main(Dispatch.java:18)
我制作了这些文件
WorkerRunnable.java
package servers;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;
/**
*/
public class WorkerRunnable implements Runnable{
protected Socket clientSocket = null;
protected String serverText = null;
public WorkerRunnable(Socket clientSocket, String serverText) {
this.clientSocket = clientSocket;
this.serverText = serverText;
}
public void run() {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
long time = System.currentTimeMillis();
output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " +
this.serverText + " - " +
time +
"").getBytes());
output.close();
input.close();
System.out.println("Request processed: " + time);
} catch (IOException e) {
//report exception somewhere.
e.printStackTrace();
}
}
}
MultiThreadedServer.java
package servers;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public class MultiThreadedServer implements Runnable{
protected int serverPort = 8080;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread= null;
public MultiThreadedServer(int port){
this.serverPort = port;
}
public void run(){
synchronized(this){
this.runningThread = Thread.currentThread();
}
openServerSocket();
while(! isStopped()){
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
} catch (IOException e) {
if(isStopped()) {
System.out.println("Server Stopped.") ;
return;
}
throw new RuntimeException(
"Error accepting client connection", e);
}
new Thread(
new WorkerRunnable(
clientSocket, "Multithreaded Server")
).start();
}
System.out.println("Server Stopped.") ;
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop(){
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
throw new RuntimeException("Cannot open port 8080", e);
}
}
}
Dispatch.java
package servers;
public class Dispatch {
/**
* @param args
*/
public static void main(String[] args) {
MultiThreadedServer server = new MultiThreadedServer(9000);
new Thread(server).start();
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Stopping Server");
server.stop();
}
}
答案 0 :(得分:3)
端口8080
可能已被您计算机中的其他服务使用(它通常由Web服务器使用,如Tomcat)。尝试在另一个端口号中启动服务器。
答案 1 :(得分:2)
其他程序正在使用您的计算机的端口8080
终止使用端口8080的程序
您可以通过以下命令(Windows)找到该程序:
netstat -o -n -a | findstr 0.0:8080
示例输出:
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 8704
然后,您可以使用任务管理器使用PID 8704
找到该程序并终止它。
详情请参阅here。
使用其他端口
您可以更改
protected int serverPort = 8080;
到
protected int serverPort = 8888;
或者
MultiThreadedServer server = new MultiThreadedServer(8888);
因此使用端口8888
代替8080
。
答案 2 :(得分:2)
您的代码容易出错且误导。您将服务器端口指定为9000
,并在异常块中将其打印为8080
,并在类声明中初始化它为8080
。正在使用的实际端口是端口9000
,而不是8080
。尝试另一个端口 - 请尝试8080
。